cp Command in Linux/Unix | Linux Copy File'cp' means copy. 'cp' command is used to copy a file or a directory. Introduction to cp CommandThe cp command is used to copy directories and files. The command contains three primary operation modes, represented by the argument types shown to the program to copy a file to other files, multiple files to any directory, or to copy the whole directories to other directories. Further, the utility accepts several command-line option flags for detailing the operations implemented. The two primary specifications are GNU cp and POSIX cp. GNU cp contains various extra options on the POSIX version. Also, the command is available within the EFI shell. Operating Modes of cp commandThe cp command contains three primary operating modes. These modes are completed from the count and type of arguments shown to the program under invocation.
Options of cp Command
The sparse SOURCE files are found by a cheap heuristic, and the related DESR file is created sparse by default. It is the behavior chosen by --sparse=auto. We can describe --sparse=always to make a sparse DEST file if the SOURCE file includes a long enough order of zero bytes. We can also apply --sparse=never to constrain the establishment of sparse files. If --reflink[=always] is mentioned, implement a lightweight copy, in which the data blocks have been copied only if changed. If it's not possible or --reflink=auto is mentioned, the copy fails and falls back to the standard copy. We can also apply --reflink=never to guarantee a standard copy is implemented. 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'. 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
|