The cp command in Linux is used to copy files and directories. It allows users to create copies of files, duplicate directories recursively, and manage permissions during copying. The basic syntax involves specifying the source and destination, with various options to enhance functionality.
Examples of Using the cp Command:
cp file.txt newfile.txt
This command creates a copy of file.txt named newfile.txt in the same directory.
cp /source/*.txt /destination备份/
Recursively copies all contents from /source/ to /destination/.
cp -r documents backup/
Copies the documents directory and its contents into backup/.
cp -f file.txt /target/file.txt
Forces overwriting without prompting.
cp -p important_file backup/
Copies important_file while preserving its original permissions.
cp -b file.txt /target/file.txt
Renames the existing file with a ~ extension before overwriting.
The cp command is an essential tool in Linux for efficiently managing files and directories. Its versatility allows users to copy individual files, handle multiple files with wildcards, preserve permissions, and even work with directories recursively. Understanding the various options enhances productivity, making cp a cornerstone of file management in Linux systems.