How to Copy files and folders in Linux

Being a Linux user, copying files and directories is one of the most common tasks.

Linux users don’t spend a day without using the cp (copy) command according to my personal experience. cp command is used to copy a single file or group of files or directory.

To perform the copy operation, you must have at least read permission in the source file and write permission in the target directory.

In this article, we will explain how to use the cp command.

To perform remote file copy, use the commands such as rsync command or scp command or pscp command.

What is cp command?

‘cp’ command is one of the basic and most widely used Linux commands for copying files and directories from one location to another.

When copying files from source to destination, the source file remains the same and the target file may have the same or different name.

Common syntax for cp command:

cp [Option] [Source] [Destination]

Common options for cp command:

OptionsDescription
-vVerbose mode (Show progress)
-r/RCopy directories recursively
-nDon’t overwrite an existing file
-dCopy a link file
-iPrompt before overwrite
-pPreserve the specified attributes
-bMake a backup of each existing destination file

To demonstrate file copy operation in detail, we will be performing a copy operation between user1 and user2.

1) Copying files with cp command

Use the following command to copy a file from one location to another.

In the following example, we will copy the source file “/home/user1/cp-demo.txt” to the target directory “/home/user2” with the same name:

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

You can check whether the specific file is copied to the target directory using the ls command as shown below:

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

2) Copying files with a different name

Use the following command to copy a file from one place to another with a different name. You need to give a new name to the target file.

In the following example, we will copy a file named “/home/user1/cp-demo.txt” to the target directory with a new name “/home/user2/cp-demo11.txt” :

# 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

3) Copying multiple files in Linux

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

In this example, we will copy the following three files named “cp-demo.txt, cp-demo-1.txt and cp-demo-9.txt” to the target.

No additional option is required to copy multiple files, and the file name(s) must be separated with a space as shown below:

# 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 verify the copied files. You should see them in the target directory as shown below:

# 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 directories recursively

To copy a directory recursively from one location to another, use the -r/R option with the cp command. It copies everything, including all its files and subdirectories.

In the following example, we will copy the ‘/home/user1/cp-demo-folder’ directory to ‘/home/user2/’ and the target directory name will remain the same:

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

Use the ls command to verify the outcome. You should see them in the target directory as shown below:

# 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) Copying multiple directories recursively

Use the following command to copy multiple directories at once. In the following example, we will 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

Output can be verified 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) Copying specific format files in Linux

Sometimes you may have to copy files with a specific extension for a certain purpose. If so, you should use the cp command with the "wildcard (*)" option and the file extension name.

In the following example, we will copy all the files containing the *.sh extension from the source to 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 verified 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) Copying all files at once in Linux

To copy all the files except the directory from one location to another, use the following cp command with wildcard (*) option.

cp command excludes directories by default, and the -r option must be added to copy them.

In this example, we will copy all the files from ‘/home/user1/’ to ‘/home/user2′ :

# 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

Verify the output using the ls command as shown below:

# 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 hidden files (‘.’ dot files) in Linux

By default, the cp command does not copy “dot (.)” or ‘hidden’ files in Linux.

Use the following command to copy all types of files, including link files (soft link or hard link), folders and hidden or dot files from source to destination.

This example is very useful to copy user’s home directory which has several hidden files:

# 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

9) Backup the file with cp command

cp command allows you to backup the file if the same file name exists in the target directory using the --backup option.

In this example, we will copy the /home/user1/passwd-up.sh file to the target directory /home/user2/.

It will backup the “passwd-up.sh” file in the target directory during the copy operation, if you say “yes” when you see the message below:

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

It adds the “Tilde (~)” symbol at the end of the old file, the same can be verified in the following output.

# 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~

10) Copying files with attributes

By default, Linux replaces file attributes such as username, group name, date and time when copying files from a user, and does not carry the original file attributes.

To preserve the original attributes of the files, use the -p option with the cp command.

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

Upon querying we can see that the file “service-1.sh” has retained its original attributes as shown below:

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

11) How to avoid file overwriting with cp command?

When copying a file, use the -n option with the cp command to avoid overwriting an existing file.

It will only copy the source file, if the target directory does not have a file with the same name. If a duplicate file exists in the target, then the command will run, but won’t make any changes:

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

Upon querying using the ls command, it can be seen that no action is taken against the file because it still has the old attributes, which can be seen in the above example (refer section 10):

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

12) Prompting confirmation with cp command

Use the -i option with the cp command to prompt for confirmation before overwriting the file, in case the file exists in the target location.

The following example prompts for confirmation when copying the “cp-demo-9.txt” file because the file already exists in the target directory:

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

13) Copying link files in Linux

By default, the cp command excludes link files while performing the copy operation. Use the -d option with the cp command to copy the link files as shown below:

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

It has been successfully copied, and the soft link file can be found 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 demonstrated 13 methods that are widely used by the Linux administrators in their daily operations. If you would like to explore other options, please visit the man page of the cp command by using the following command:

# man cp
or
# cp --help

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

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