Introduction – You need to use the cp command which is used to copy files and directories. The copies become independent of the originals. Any subsequent change in one will not affect the other. This page shows how to use cp command to copy one file contents to another at bash shell.
Copy one file contents to another file in Linux
The cp commands basic syntax is:
cp file_name new_file_name
cp [options] file_name new_file_name
cp original_name new_name
Please note that when a copy is made of a file, the copy must have a different name than the original. For example, the following is a valid example:
cp file1 file2
However, the following would fail:
cp nixcraft.txt nixcraft.txt
Sample outputs:
cp: 'nixcraft.txt' and 'nixcraft.txt' are the same file
However, a file named nixcraft.txt could be copied with the same name into another directory:
cp -v nixcraft.txt /tmp/
File names are case sensitive too. It means following example should work:
cp nixcraft.txt NIXCRAFT.txt
ls -l nixcraft.txt NIXCRAFT.txt
Sample outputs:
-rw-rw-r--. 1 vivek vivek 6 Jan 20 18:12 nixcraft.txt -rw-rw-r--. 1 vivek vivek 6 Jan 20 18:12 NIXCRAFT.txt
Linux copy file to another file
Let us create a new file in Linux named foo.txt:
echo "This is a test" > foo.txt
Next copy foo.txt as bar.txt, run:
cp foo.txt bar.txt
Verify it with help of ls command:
ls -l foo.txt
ls -l bar.txt
To see cp command progress and verbose output, pass the -v command option to cp:
cp -v foo.txt bar.txt
Update the original foo.txt:
echo "Another line" >> foo.txt
Use the cat command to see both files:
cat foo.txt
cat bar.txt
Copy content of one file to another file
Say you want to copy all files from /home/vivek/project/ to /home/vivek/backups/, run:
cp -av /home/vivek/project/ /home/vivek/backups/
Now, we can make changes in /home/vivek/project/ directory.
Where,
- -a : Archive mode i.e. copy all files and directories recursively
- -v : Verbose mode
- -r : Recursive mode in Linux for cp command
Conclusion
Writing contents of a file to another file is easy in Linux. Simply use:
cp -v filename newname
For more information see cp command man page by typing the following [nixmd name=”man”]:
man cp
🐧 2 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Another option is
cat original.file > backup.file
ls -l *.file
I paste the contents of one file into another file using cat in Linux:
cat my_file > new_file
I paste and append the contents of one file into another file using cat too on Linux:
cat my_file >> new_file