Copy Command cp in Linux/Unix

Copy command cp in Linux is used to copy the contents of a file or directory into another file or directory. Here, I will talk about how you can use copy command cp to copy any files or directory in Linux or Unix-based distros.

Copy command cp is one of the most important commands in the shell scripting.

Table of Contents

Basic syntax in copy command cp in Linux

To copy file1‘s content into file2, execute the following command. If file2 exists, its contents are replaced.

[ajay@legion ~]$ cp file1 file2

Example: To replace the contents of replace.txt with that of example.txt, run the following command:

[ajay@legion ~]$ cp ~/example.txt ~/Downloads/replace.txt

If you use a directory name at the place of file2, file1 will be copied to that directory. For example, the following command copies the file ~/example.txt to the directory ~/Downloads

[ajay@legion ~]$ cp ~/example.txt ~/Downloads

📝 Note: To refer to the current directory, use . in bash. This applies to all commands, not just the cp command. For example, to copy ~/example.txt into the current directory viz. ~, execute the following:

[ajay@legion ~]$ pwd
/home/ajay

[ajay@legion ~]$ cp ~/Downloads/10th.pdf .

Options in the cp command

How to get verbose output in the cp command

To get a verbose output from the cp, use -v or --verbose – it shows what it is doing. Example:

[ajay@legion ~]$ cp -v ~/Downloads/10th.pdf .
'/home/ajay/Downloads/10th.pdf' -> './10th.pdf'

The interactive mode in the cp command

To get interactive output, use -i or --interactive flag: if you write ‘y’ it copies, and for anything else, it does not. Example:

[ajay@legion ~]$ cp -v ~/Downloads/10th.pdf .
'/home/ajay/Downloads/10th.pdf' -> './10th.pdf'

[ajay@legion ~]$ cp -i ~/Downloads/10th.pdf .
cp: overwrite './10th.pdf'? y

📝 Note: You can use both -v and -i together. For the above example:

[ajay@legion ~]$ cp -vi ~/Downloads/10th.pdf .
'/home/ajay/Downloads/10th.pdf' -> './10th.pdf'

[ajay@legion ~]$ cp -vi ~/Downloads/10th.pdf .
cp: overwrite './10th.pdf'? y
'/home/ajay/Downloads/10th.pdf' -> './10th.pdf'

🙂 Note: Both -v and -i options are available in the commands mv and rm as well.

The cp command copies the symlinks like any other file. But it does not create copies of the actual target file.

For example, when I copy a symlink file ~/.Xdefaults, only a symlink is created at another place.

[ajay@legion ~]$ ls -al ~/.Xdefaults
lrwxrwxrwx 1 ajay ajay 22 Mar  4  2021 /home/ajay/.Xdefaults -> /home/ajay/.Xresources
[ajay@legion ~]$ cp ~/.Xdefaults /tmp

[ajay@legion ~]$ ls -al /tmp/.Xdefaults         
lrwxrwxrwx 1 ajay ajay 22 Mar 25 17:47 /tmp/.Xdefaults -> /home/ajay/.Xresources

To copy the actual target file, you need to use the flag -L or --dereference. For, ~/.Xdefaults, the actual target file is ~/.Xresources.

[ajay@legion ~]$ cp -L ~/.Xdefaults /tmp/file2.txt

How to copy a directory in Linux/Unix using the cp command

You can also copy one directory into another.

For example, The following commands create a form directory into /tmp/to and copies all content of /tmp/from.

[ajay@legion ~]$ cp -r /tmp/from/ /tmp/to/

Please note that I am using the flag -r or –recursive in the above command. That is to tell the cp command to copy all the contents of the from directory recursively. All directories, files, and everything else inside the directory form will be copied. This -r flag is available with many basic linux commands such as mv, rm.

If you don’t want to create the directory from inside the target directory /tmp/to, use * just after the /. It is a special glob in bash. And don’t use quotes around it.

Example:

[ajay@legion ~]$ du --all /tmp/from /tmp/to
4    /tmp/from/dir1/file.txt
4    /tmp/from/dir1
4    /tmp/from
0    /tmp/to

[ajay@legion ~]$ cp -r /tmp/from/* /tmp/to/

[ajay@legion ~]$ du --all /tmp/from /tmp/to
4    /tmp/from/dir1/file.txt
4    /tmp/from/dir1
4    /tmp/from
4    /tmp/to/dir1/file.txt
4    /tmp/to/dir1
4    /tmp/to

📝 Note: The command du in the above example is used to list the contents of one or more directories.

Example 2: cp copies all contents of the given ...default folder into the new ...default folder without creating <oldrandomnumber>.default in the new directory.

[ajay@legion ~]$  cp -R /to/backup/folder/.thunderbird/<oldrandomnumber>.default/* ~/.thunderbird/<newrandomnumber>.default/

Conclusion

That was a basic introduction to the cp command. If you want to learn more, read its manual pages man cp, info cp and the official cp documentation. To get a more rigorous copy command, use rsync which preserves all metadata of a file (file permissions, ownerships, extended attributes, etc.).

Leave a Comment

Your email address will not be published. Required fields are marked *