Being a Linux user, copying files and directories is one of the most common tasks. Show
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:
To demonstrate file copy operation in detail, we will be performing a copy operation between 1) Copying files with cp commandUse 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 nameUse 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 LinuxThe 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 recursivelyTo copy a directory recursively from one location to another, use the 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 recursivelyUse 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 LinuxSometimes you may have to copy files with a specific extension for a certain purpose. If so, you should use the cp command with the 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 LinuxTo copy all the files except the directory from one location to another, use the following cpcommand with cpcommand excludes directories by default, and the 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 LinuxBy default, the cpcommand 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 commandcpcommand allows you to backup the file if the same file name exists in the target directory using the 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 attributesBy 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 # 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 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 commandUse the 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 LinuxBy default, the cp command excludes link files
while performing the copy operation. Use the # 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 What is the Linux command for copying a file or folder?You use the cp command for copying files from one location to another. This command can also copy directories (folders). [file/directory-sources] specifies the sources of the files or directories you want to copy. And the [destination] argument specifies the location you want to copy the file to.
When using the cp command you can copy files?cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name. cp command require at least two filenames in its arguments.
How copy file Linux to cp?The Linux cp command is used for copying files and directories to another location. To copy a file, specify “cp” followed by the name of a file to copy. Then, state the location at which the new file should appear. The new file does not need to have the same name as the one you are copying.
Can cp copy multiple files?CP allows you to copy directories and command files using the command line. With this command, you can transfer multiple files or folders, preserve attribute information and create their backups.
|