![]() The cp command also allows you to use pattern matching. When copying multiple files, the destination must be a directory. To copy multiple files and directories at once, specify the names of source files and directories followed with the destination directory as the last argument: cp file.txt dir file1.txt file2.txt dir1 The command above will copy the file to the specified directory as new_file.txt. If you want to copy the file under a different name, you need to specify the desired file name: cp file.txt /backup/new_file.txt When specifying only the directory name as a destination, the copied file will have the same name as the original file. In the following example, we are copying the file file.txt to the /backup directory: cp file.txt /backup When the destination directory is omitted, the file is copied to the current directory. To copy a file to a directory, specify the absolute or the relative path to the directory. When using this option, the command prints what is being done: cp -v file.txt file_backup.txt 'file.txt' -> 'file_backup.txt' Use the -p option to preserve the file mode, ownershipĪnother option that can be useful is -v. cp -i file.txt file_backup.txtīy default, when using the cp command to copy a file, the new file will be owned by the user performing the command. To get a confirmation prompt before overwriting the files, use the -i option. If the destination file exists, it will be overwritten. ![]() ![]() , you would run the following command: cp file.txt file_backup.txt For example, to copy a file named file.txt to a file named file_backup.txt in the current directory The most simple use case is to copy a file in the current working directory. On Linux and Unix operating systems, the cpĬommand is used for copying files and directories. To be able to copy files and directories, you must have at least read permissions on the source file and write permission on the destination directory. It is common practice to use the cp command to copy files and rsync to copy directories. How to Copy Files in Linux using CP Command
0 Comments
Leave a Reply. |