How to copy one file contents to another file in Linux

See all GNU/Linux related FAQ
I‘m a new Linux system user and don’t know much about the Ubuntu terminal. So, I have a file named data.txt. How do I copy data.txt file contents to another file called data.backup.txt in RHEL or CentOS Linux? How do I duplicate files in a Linux-based computer before I modify the original file?

You need to use the cp command. It 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. Let us see how to copy one file contents to another file in Linux using the CLI.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux terminal
Category File Management
OS compatibility AlmaLinux Alpine Arch Debian Fedora Linux Mint openSUSE Pop!_OS RHEL Rocky Stream SUSE Ubuntu WSL
Est. reading time 3 minutes

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
Here is what cp would report:

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.

  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


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 man command/help command:
man cp
cp --help

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

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. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.