How to copy one file contents to another file in Linux

last updated in Categories , , , , ,

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.

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

2 comment

  1. 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

    Still, have a question? Get help on our forum!