cp Command in Linux/Unix | Linux Copy File'cp' means copy. 'cp' command is used to copy a file or a directory. To copy a file into the same directory syntax will be, ![]() In above snapshot, we have created a copy of 'docu' and named it as 'newdocu'. If in case, <new file name> (in our case it is 'newdocu') alreade exists, then it will simply over write the earlier file. To copy a file in a different directoryWe have to mention the path of the destination directory. In the snapshot below, earlier there is no 'text' file. After giving the command, 'text' file has been copied to the destination directory that is 'Desktop'. ![]() cp Options
Linux cp -rOption 'r' with the copy command can be used to copy a directory including all its content from a source directory to the destination directory. Syntax: Example: ![]() In the above example, we have copied directory 'library' to the destination directory /home/sssit/Documents. Here, all the contents of 'library' directory including its contents have been copied to destination directory. Linux Copy Multiple Files or DirectoriesMultiple files or directories can be copied to a destination directory at once. In this case, target must be a directory. To copy multiple files you can use wildcards (cp *.extension) having same pattern. Syntax: Example: ![]() In above example, we have copied files (file1, file2, file3) having same extension '.txt' to Documents directory. Linux cp --backupIf the file you want to copy already exists in the destination directory, you can backup your existing file with the use of this command. Syntax: Example: ![]() As you can see above, 'file2.txt' already exists in the destination directory. Hence, we have created a backup of this file and copied it in the same directory (having same name). Now our destination directory that is 'Downloads' has two files with the same name (that is 'file2.txt'). Linux cp -iThe cp '-i' option allows you to confirm once before overwriting your file. Syntax: Example: ![]() As you can see, it is asking for permission to over write the file 'file3.txt' as this file already exists in the destination directory. Now, you can press y to overwrite file and n not to overwrite file. Linux cp -lIf you want to create a hard link of a file instead of copying that file, you can use option 'l'. Note: In creating hard link of the file the inode number of the two files will remain same. While in case of copying, inode number changes. Syntax: Example: ![]() Note here that we have created a hard link of the file 'file1.txt' in 'usr'. Inode number of both the files are same. Linux cp -pThe cp '-p' option is used to preserve the properties and attributes of a file. You can also preserve the selected properties which you want. Syntax: Example: ![]() Now, you can match the two files (original one and the copied one) in the above picture, both have the same properties. Linux cp -u -vThe cp -u -v command is used when you want to make sure that destination file is missing or doesn't exist. Syntax: Example: ![]() In the above picture, you can see that there are two files 'docc' and 'file1.txt'. Now we want to copy these two files in the 'Download' directory. But we don't know that in the 'Downloads' directory file 'file1.txt' already exists. Giving the command 'cp -u -v' will automatically take care of the already existing file (file1.txt) and will not over write it. This command is useful in copying big files.
Next TopicLinux mv command
|