The cp command in Linux is used to copy files and directories from one location to another. It is a fundamental command-line utility that allows users to duplicate files or move them to different directories while preserving the original files.
Basic Syntax
The basic syntax of the cp command is:
cp [OPTION]... SOURCE DESTINATION
SOURCE: The file or directory you want to copy.DESTINATION: The location where you want to copy the source file or directory.
Common Use Cases
-
Copying a File:
To copy a file from one location to another:cp file1.txt file2.txtThis command creates a copy of
file1.txtnamedfile2.txtin the current directory. -
Copying a File to a Different Directory:
To copy a file into a specific directory:cp file1.txt /path/to/directory/ -
Copying a Directory:
To copy an entire directory and its contents, you need to use the-r(recursive) option:cp -r dir1/ dir2/This command copies
dir1and all its contents intodir2.
Important Options
-i: Interactive mode; prompts before overwriting files.-u: Copies only when the source file is newer than the destination file or when the destination file does not exist.-v: Verbose mode; shows the progress of the copy operation.-a: Archive mode; preserves file attributes and copies directories recursively.
Summary
The cp command is a versatile tool for file management in Linux, allowing users to easily duplicate files and directories while providing various options to customize its behavior.
If you have any more questions or need further clarification, feel free to ask!
