How to Copy Files and Folders on Linux Using the cp Command

All levels of Linux Administrator cannot leave a day without using the cp (copy) command, is this true?

According to my personal experience with Linux I can say yes.

If I have less work I use at least 15-20 times a day.

This number is slightly higher if the work is higher.

If you are looking for remote file copy utility, try the rsync command or scp command or pscp command.

What is cp Command

The cp command is one of the basic Linux commands for copying files and directories from one location to another.

When copying files from source to destination, the source file name does not change, but we can change the target file name if we need to.

Common Syntax for cp Command:

cp [Option] [Source] [Destination]

Common Options for cp Command:

+----------------------------------------------------------+
| Option |               Description	                   |
+----------------------------------------------------------+
|   -v   | Verbose mode (Show Progress)                    |
|  -r/R  | Copy Directories Recursively                    |
|   -n   | Don't overwrite an existing file                |
|   -d   | Copy a link file                                |
|   -i   | Prompt before overwrite                         |
|   -p   | Preserve the specified attributes               |
|   -b   | Make a backup of each existing destination file |
+----------------------------------------------------------+

To demonstrate this in details, we are going to performing a copy operation between user1 and user1.

1) How to Copy a File from One Location to Another in Linux Using the cp Command

To copy a file from one location to another, use the following command. This will copy the source file to the destination with the same name.

# cp /home/user1/cp-demo.txt /home/user2

I have copied the source file “/home/user1/cp-demo.txt” to the target “/home/user2” directory.

You can check whether the specified file has been copied to the target directory using the ls command. Yes, it is, I could see it.

# ls -lh /home/user2
total 4.0K
-rw-r--r--. 1 root root 22 Dec  9 02:15 cp-demo.txt

2) How to Copy a File from One Location to Another With a Different Name on Linux Using the cp Command

To copy a file from one place to another with a different name enter the new name for a file in the target location.

The following command format copies the source file to destination location with a different name.

# cp /home/user1/cp-demo.txt /home/user2/cp-demo11.txt

Use the ls command to verify this.

# ls -lh /home/user2
total 8.0K
-rw-r--r--. 1 root root 22 Dec  9 03:06 cp-demo11.txt
-rw-r--r--. 1 root root 22 Dec  9 02:15 cp-demo.txt

The output clearly shows that the specified file is copied from the source “/home/user1/cp-demo.txt” to the target “/home/user2/cp-demo11.txt” directory with a different name.

3) How to Copy Multiple Files from One Location to Another in Linux Using the cp Command

The following command copies multiple files from one location to another.

In this example, we are going to copy three files named “cp-demo.txt, cp-demo-1.txt and cp-demo-9.txt”.

No option is required to perform this action, and all the files must be entered with the space.

# cp /home/user1 cp-demo.txt cp-demo-1.txt cp-demo-9.txt /home/user2/
cp: omitting directory ‘/home/user1’
cp: overwrite ‘/home/user2/cp-demo.txt’? y

Use the ls command to check this. Yes, i can see them in the target directory.

# ls -lh /home/user2/
total 16K
-rw-r--r--. 1 root root 22 Dec  9 03:06 cp-demo11.txt
-rw-r--r--. 1 root root 22 Dec  9 03:11 cp-demo-1.txt
-rw-r--r--. 1 root root 22 Dec  9 03:11 cp-demo-9.txt
-rw-r--r--. 1 root root 22 Dec  9 03:11 cp-demo.txt

4) How to Copy a Directory Recursively from One Location to Another in Linux Using the cp Command

If you want to copy a directory recursively from one location to another using the cp command, use the -r/R option with the cp command.

It copies the folder including the subdirectories and their files to the target directory.

The folder name remains same.

# cp -r /home/user1/cp-demo-folder/ /home/user2/

Use the ls command to check this. Yes, i can see them in the target directory.

# ls -ld /home/user2/cp-demo-folder
drwxr-xr-x. 3 root root 72 Dec  9 03:17 /home/user2/cp-demo-folder

# ls -lh /home/user2/cp-demo-folder/
total 8.0K
-rw-r--r--. 1 root root 22 Dec  9 03:17 cp-demo-2.txt
-rw-r--r--. 1 root root 22 Dec  9 03:17 cp-demo-3.txt
drwxr-xr-x. 2 root root 48 Dec  9 03:17 cp-demo-folder-1

5) How to Recursively Copy Multiple Folders on Linux Using the cp Command

It’s like the one above and it allows you to copy multiple folders at once.

To do so, use the following cp command format. In this example, we copy the public_html & public_ftp folders to the target directory named /home/2daygeek/cp-test.

# cp -r /home/mageshm/public_ftp/ /home/mageshm/public_html/ /home/2daygeek/cp-test

This can be checked using the ls command.

# ls -lh /home/2daygeek/cp-test
total 12M
drwxr-xr-x. 3 root     root     4.0K Jan 24 11:12 links/
drwxr-xr-x. 2 root     root     4.0K Jan 24 11:19 public_ftp/
drwxr-xr-x. 3 root     root     4.0K Jan 24 11:19 public_html/
-rw-r--r--. 1 root     root      12M Jan 24 11:07 test10.zip
-rw-r--r--. 1 root     root       61 Jan 24 11:07 test2-bzip2.txt
-rw-r--r--. 1 root     root       61 Jan 24 11:07 test3-bzip2.txt
-rw-r--r--. 1 root     root       60 Jan 24 11:04 test-bzip2-new.txt
-rw-r--r--. 1 root     root       60 Jan 24 10:53 test-bzip2.txt

6) How To Copy Specific Format Files in Linux Using the cp Command

If you want to copy specific extension files on Linux using the cp command, use “wildcard (*)” with the specified file extension to do that.

In this example, we are going to copy the list of files containing the *.sh extension into the target directory. Similarly, you can copy any file extensions such as .jpg, .png, .txt, .php, and .html.

# cp /home/user1/*.sh /home/user2/

This can be checked using the ls command.

# ls -lh /home/user2/*.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 /home/user2/service-1.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 /home/user2/service-2.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 /home/user2/service-3.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 /home/user2/service-4.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 /home/user2/service.sh

7) How to Copy All Files from One Location to Another in Linux Using “wildcard (*)”

If you have a list of files and you want to copy them all to another location, use the following cp command format.

This excludes the directory by default, and the “-r” option must be included in this command to copy them.

# cp /home/user1/* /home/user2/
cp: overwrite ‘/home/user2/cp-demo-1.txt’? n
cp: overwrite ‘/home/user2/cp-demo-9.txt’? n
cp: omitting directory ‘/home/user1/cp-demo-folder’
cp: omitting directory ‘/home/user1/cp-demo-folder-9’
cp: overwrite ‘/home/user2/cp-demo.txt’? n
cp: overwrite ‘/home/user2/service-1.sh’? n
cp: overwrite ‘/home/user2/service-2.sh’? n
cp: overwrite ‘/home/user2/service-3.sh’? n
cp: overwrite ‘/home/user2/service-4.sh’? n
cp: overwrite ‘/home/user2/service.sh’? n

The same can be checked by using the ls command.

# ls -lh /home/user2/
total 44K
-rw-r--r--. 1 root root 22 Dec  9 03:06 cp-demo11.txt
-rw-r--r--. 1 root root 22 Dec  9 03:11 cp-demo-1.txt
-rw-r--r--. 1 root root 22 Dec  9 03:11 cp-demo-9.txt
drwxr-xr-x. 3 root root 72 Dec  9 03:17 cp-demo-folder
-rw-r--r--. 1 root root 22 Dec  9 03:11 cp-demo.txt
-rw-r--r--. 1 root root 25 Dec  9 03:35 passwd-up.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 service-1.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 service-2.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 service-3.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 service-4.sh
-rw-r--r--. 1 root root 23 Dec  9 03:25 service.sh
-rw-r--r--. 1 root root 25 Dec  9 03:35 user-add.sh

8) How to Copy All Files and Folders from One Location to Another in Linux Using “wildcard (*)”

Most of you can use the mv (move) command for this operation, but this will move all the files to the target directory instead of the copy, which is not what you expected.

To do so, use the following cp command format. This is similar to the one above, but it will copy files and folders to the target directory at the same time.

Also, this combination could also copy soft-link/symbolic link files.

# cp -R /home/user1/* /home/user2/
cp: overwrite ‘/home/user2/cp-demo-1.txt’? y
cp: overwrite ‘/home/user2/cp-demo-9.txt’? y
cp: overwrite ‘/home/user2/cp-demo-folder/cp-demo-folder-1/cp-demo-4.txt’? y
cp: overwrite ‘/home/user2/cp-demo-folder/cp-demo-folder-1/cp-demo-5.txt’? y
cp: overwrite ‘/home/user2/cp-demo-folder/cp-demo-2.txt’? y
cp: overwrite ‘/home/user2/cp-demo-folder/cp-demo-3.txt’? y
cp: overwrite ‘/home/user2/cp-demo.txt’? y
cp: overwrite ‘/home/user2/passwd-up.sh’? y
cp: overwrite ‘/home/user2/service-1.sh’? y
cp: overwrite ‘/home/user2/service-2.sh’? y
cp: overwrite ‘/home/user2/service-3.sh’? y
cp: overwrite ‘/home/user2/service-4.sh’? y
cp: overwrite ‘/home/user2/service.sh’? y
cp: overwrite ‘/home/user2/user-add.sh’? y

The same can be checked by using the ls command.

# ls -lh /home/user2/
total 40K
-rw-r--r--. 1 root root 22 Dec  9 03:06 cp-demo11.txt
-rw-r--r--. 1 root root 22 Dec  9 04:01 cp-demo-1.txt
-rw-r--r--. 1 root root 22 Dec  9 04:01 cp-demo-9.txt
drwxr-xr-x. 3 root root 72 Dec  9 03:17 cp-demo-folder
drwxr-xr-x. 2 root root 46 Dec  9 04:01 cp-demo-folder-9
-rw-r--r--. 1 root root 22 Dec  9 04:01 cp-demo.txt
-rw-r--r--. 1 root root 25 Dec  9 04:01 passwd-up.sh
-rw-r--r--. 1 root root 23 Dec  9 04:01 service-1.sh
-rw-r--r--. 1 root root 23 Dec  9 04:01 service-2.sh
-rw-r--r--. 1 root root 23 Dec  9 04:01 service-3.sh
-rw-r--r--. 1 root root 23 Dec  9 04:01 service-4.sh
-rw-r--r--. 1 root root 23 Dec  9 04:01 service.sh
lrwxrwxrwx. 1 root root 20 Dec  9 04:01 user-add.sh -> /home/u1/user-add.sh

9) How to Copy All Files, Including Hidden Files (“.” Dot Files) in Linux Using the cp Command

This command is the same as above, but you need to add “dot (.)” in addition to copy all the files, including hidden files.

It copies all types of files, folders, link-files and hidden or dot files from source to destination in the same name recursively.

# cp -R /home/user1/.* /home/user2/all-files/
cp: will not create hard link ‘/home/user2/all-files/user1’ to directory ‘/home/user2/all-files/.’
cp: cannot copy a directory, ‘/home/user1/..’, into itself, ‘/home/user2/all-files/’
cp: overwrite ‘/home/user2/all-files/.bash_history’? y
cp: overwrite ‘/home/user2/all-files/.bash_logout’? y
cp: overwrite ‘/home/user2/all-files/.bash_profile’? y
cp: overwrite ‘/home/user2/all-files/.bashrc’? y
cp: overwrite ‘/home/user2/all-files/.cache/abrt/lastnotification’? y

You can verify this by using the ls command with the “-a” option.

# ls -la /home/user2/all-files/
total 68
drwxr-xr-x. 10 root  root  4096 Dec  9 04:03 .
drwx------.  8 user2 user2 4096 Dec  9 04:03 ..
-rw-------.  1 root  root   419 Dec  9 04:04 .bash_history
-rw-r--r--.  1 root  root    18 Dec  9 04:04 .bash_logout
-rw-r--r--.  1 root  root   193 Dec  9 04:04 .bash_profile
-rw-r--r--.  1 root  root   231 Dec  9 04:04 .bashrc
drwxr-xr-x.  3 root  root    18 Dec  9 04:03 .cache
drwx------.  3 root  root    78 Dec  9 04:03 cat
drwxr-xr-x.  3 root  root    18 Dec  9 04:03 .config
-rw-r--r--.  1 root  root    22 Dec  9 04:03 cp-demo-1.txt
-rw-r--r--.  1 root  root    22 Dec  9 04:03 cp-demo-9.txt
drwxr-xr-x.  3 root  root    72 Dec  9 04:03 cp-demo-folder
drwxr-xr-x.  2 root  root    46 Dec  9 04:03 cp-demo-folder-9
-rw-r--r--.  1 root  root    22 Dec  9 04:03 cp-demo.txt
drwx------. 17 root  root  4096 Dec  9 04:03 daygeek
drwxr-xr-x.  4 root  root    39 Dec  9 04:03 .mozilla
-rw-r--r--.  1 root  root    25 Dec  9 04:03 passwd-up.sh
-rw-r--r--.  1 root  root    23 Dec  9 04:03 service-1.sh
-rw-r--r--.  1 root  root    23 Dec  9 04:03 service-2.sh
-rw-r--r--.  1 root  root    23 Dec  9 04:03 service-3.sh
-rw-r--r--.  1 root  root    23 Dec  9 04:03 service-4.sh
-rw-r--r--.  1 root  root    23 Dec  9 04:03 service.sh
drwx------.  8 root  root  4096 Dec  9 04:03 user2
lrwxrwxrwx.  1 root  root    20 Dec  9 04:03 user-add.sh -> /home/u1/user-add.sh

10) How to Backup the File When Using the cp Command if the File is in the Target Directory

The cp command allows you to backup the file if the file is in the target directory using the --backup option.

In this example, we are copying the /home/user1/passwd-up.sh file into the target directory /home/user2/. If the file exist, it will backup the “passwd-up.sh” file in the target directory.

To backup a file, you should give “yes” when you see the message below.

# cp --backup /home/user1/passwd-up.sh /home/user2/
cp: overwrite ‘/home/user2/passwd-up.sh’? y

You can check this using the ls command. This adds the “Tilde (~)” symbol at the end of the old file.

# ls -lh /home/user2/passwd-up.sh*
-rw-r--r--. 1 root root 25 Dec  9 04:06 /home/user2/passwd-up.sh
-rw-r--r--. 1 root root 25 Dec  9 04:01 /home/user2/passwd-up.sh~

11) How to Copy a File With Attributes in Linux Using the cp Command

By default, Linux replaces your permissions such as username, group name, date and time when you copy a file from others, and it doesn’t carry the original file attributes.

This is easily understandable compared to the release of the example above. But if you want to preserve the original attributes of a file, use the -p option with the cp command.

# cp -p /home/user1/cp-demo-folder-9/service-1.sh /home/user2/

You can check this using the ls command. The “service-1.sh” file still retains its original permissions instead of root permissions.

# ls -lh /home/user2/service-1.sh 
-rw-r--r--. 1 user1 user1 23 Dec  9 03:20 /home/user2/service-1.sh

12) How to Avoid Overwriting an Existing File When Copying a File Using the cp Command on Linux

If you don’t want to overwrite an existing file when copying a file in Linux, use the -n option with the cp command.

This will only copy the source file if there is no file with the same name in the target directory. If it is, the command runs but doesn’t make any changes.

# cp -n /home/user1/service-3.sh /home/user2

Use the ls command to check the output. It has no action against this file because I can still see the old time stamp, which is in the example output above.

# ls -lh /home/user2/service-3.sh
-rw-r--r--. 1 root root 23 Dec  9 04:01 /home/user2/service-3.sh

13) How to Ask for Confirmation if the File Exist in the Target Directory

If you would like to ask for confirmation before overwriting a file, use the -i option with the cp command.

# cp -i /home/user1/cp-demo-9.txt /home/user2
cp: overwrite ‘/home/user2/cp-demo-9.txt’? n

Use the ls command to check the output. Yes, it was overwritten after receiving confirmation from you, because the timestamp of the file has now been changed.

# ls -lh /home/user2/cp-demo-9.txt 
-rw-r--r--. 1 root root 22 Dec  9 04:01 /home/user2/cp-demo-9.txt

14) How to Copy a Link File on Linux Using the cp Command

By default, the copy command exclude link files while performing a copy operation, use the -d option with the cp command to copy all types of files.

# cp -d /home/user1/user-add.sh /home/user2

It has been successfully completed, and you can see the soft link file in the below output.

# ls -lh /home/user2/user-add.sh
lrwxrwxrwx. 1 root root 20 Dec  9 04:18 /home/user2/user-add.sh -> /home/u1/user-add.sh

Conclusion:

In this tutorial, we have included 14 examples that are widely used by most of the Linux administrators in their daily operations. If you think there are other options to try, visit the man page of the cp command.

# man cp
or
# cp --help

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...