How to copy one file contents to another file in Linux

I‘m a new Linux system user. I don’t know much about the Ubuntu terminal. I have a file named data.txt. How do I copy data.txt file contents to another file called data.backup.txt in CentOS Linux? How do I duplicate file in a Linux-based computer before I make any modification to the original file?

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.

ADVERTISEMENTS

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

How to copy one file contents to another in Linux

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,

  1. -a : Archive mode i.e. copy all files and directories recursively
  2. -v : Verbose mode
  3. -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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one
  • Shaker Raju Aug 12, 2015 @ 12:54

    Another option is
    cat original.file > backup.file
    ls -l *.file

  • Layer8 user May 13, 2017 @ 1:02

    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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.