YouTip LogoYouTip

Linux Comm Cp

# Linux cp Command [![Image 3: Linux Command Manual](#) Linux Command Manual](#) The Linux cp (English full name: copy file) command is mainly used to copy files or directories. Through the cp command, users can copy files or directories from one location to another, and can choose to preserve the attributes of the original file (such as permissions, timestamps, etc.). ### Syntax cp source dest or cp source_file destination_file Where source (source file) represents the path of the file or directory to be copied, and dest (destination file) represents the path of the copied file or directory. **Option Description**: * `-r` or `-R`: Recursively copy directories and their contents (used for copying directories). * `-i`: Interactive mode, prompts the user for confirmation before overwriting. * `-f`: Force copy, overwrites the destination file without prompting. * `-v`: Displays detailed copy process (verbose). * `-p`: Preserves the original attributes of the file (such as permissions, timestamps, etc.). * `-a`: Archive mode, equivalent to `-dpR`, preserves all file attributes and recursively copies directories. * `-u`: Copies only when the source file is newer than the destination file (update mode). * `-l`: Creates a hard link instead of copying the file. * `-s`: Creates a symbolic link (soft link) instead of copying the file. ### Examples **1. Copy a file to a target directory** cp file.txt /path/to/destination/ Copies file.txt to the /path/to/destination/ directory. **2. Copy a file and rename it** cp file.txt /path/to/destination/newfile.txt Copies file.txt to the /path/to/destination/ directory and renames it to newfile.txt. **3. Recursively copy a directory** cp -r /path/to/source_dir /path/to/destination/ Recursively copies the source_dir directory and its contents to the destination directory. **4. Interactive mode copy** cp -i file.txt /path/to/destination/ If a file with the same name already exists in the target location, it will prompt the user for confirmation to overwrite. **5. Preserve file attributes** cp -p file.txt /path/to/destination/ Copies the file and preserves its original attributes (such as permissions, timestamps, etc.). **6. Copy only updated files** cp -u file.txt /path/to/destination/ Copies only if file.txt is newer than the destination file. **7. Display copy process** cp -v file.txt /path/to/destination/ Displays detailed information about the copy. **8. Create hard link or symbolic link** cp -l file.txt /path/to/destination/ # Create hard link cp -s file.txt /path/to/destination/ # Create symbolic link **9. Copy multiple files to a directory** cp file1.txt file2.txt /path/to/destination/ Copies multiple files to the target directory. **10. Copy using wildcards** cp *.txt /path/to/destination/ Copies all .txt files to the target directory. **11. Copy specific files using the find command** find /path/to/source -name "*.log" -exec cp {} /path/to/destination/ ; Finds and copies all .log files to the target directory. The above are just some common uses of the cp command. You can view more options and usage by running the man cp command. ### Notes 1. If the target path is a directory, `cp` will copy the source file or directory into that directory. 2. If the target path is a filename, `cp` will copy the source file and rename it to the target filename. 3. When copying a directory, you must use the `-r` or `-R` option, otherwise an error will occur. 4. If the destination file already exists, `cp` will overwrite it by default (unless the `-i` option is used). [![Image 4: Linux Command Manual](#) Linux Command Manual](#)
← Linux Comm McopyLinux Comm Which β†’