Linux cp Command
The Linux cp (short for "copy file") command is primarily used to copy files or directories.
Syntax
cp [options] source dest
or
cp [options] source... directory
Parameter Description:
-a: This option is commonly used when copying directories. It preserves links, file attributes, and copies all contents under the directory. Its effect is equivalent to the combination of dpR parameters.
-d: Preserve links while copying. The links mentioned here are similar to shortcuts in the Windows system.
-f: Overwrite existing destination files without prompting.
-i: Opposite to the -f option, it prompts before overwriting destination files, requiring user confirmation. The destination file will be overwritten if the user responds with
y
.-p: In addition to copying the file content, it also copies the modification time and access permissions to the new file.
-r: If the source file provided is a directory, it will copy all subdirectories and files within it.
-l: Does not copy the file but instead creates a link to it.
Example
Use the cp
command to copy all files in the current directory test/ to the new directory newtest/. Enter the following command:
$ cp -r test/ newtest
Note: When using this command to copy directories, the parameter -r or -R must be used.