![]() The main difference is that when copying directories, you need to use the -R option. The options used when copying files can also be used when copying directories. To copy only the files and subdirectories but not the target directory, use the -T option: If the destination directory already exists, the source directory itself and its content are copied to the destination directory. The command above will create the destination directory and recursively copy all files and subdirectories from the source to the destination directory. In the following example, we are copying the directory Pictures to Pictures_backup: cp -R Pictures Pictures_backup To copy a directory, including all its files and subdirectories, use the -R or -r option. png files from the current directory to the /backup directory, you would use: cp *.png /backup Copying Directories with cp Command # 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. There are several commands for copying files in Linux, with cp and rsync being the most widely used tools. rsync -v dir_1/Test.Copying files and directories is one of the most common tasks you’ll perform when working on the command line. If we want to copy the file to dir_2, here’s what the rsync command to do the same would look like. RELATED: How to Use rsync to Backup Your Data on LinuxĬonsidering that we have two directories dir_1, dir_2, and a file Test.txt in dir_1. However, it also allows copying files and directories on the same PC. The rsync command stands for “Remote Sync” and is primarily used to transfer files and directories between computers on the same network. RELATED: 37 Important Linux Commands You Should Know Using the Rsync Command The output would look like this: 'Test_Example.txt' -> 'dir_1/Test_Example.txt To display the output of the copy operation, you’d use the -v option. If you’re unsure if the file or directory was copied to the destination location, you can also use the option -v to print the names of the files or directories that were copied (like a computer program output).įor example, let’s assume that there’s a file Test_Example that needs to be copied to dir_1.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |