Linux cp command

cp is used copy files and directories from one location to another.

Syntax:

cp [options] source destination

Options:

Option Description
-R Copies files/directories recursively
-f Copies forcibly
-v Provides verbal output
-i prompt before overwrite
-l link files instead of copying
-Z set security context of copy to CONTEXT

Note:If the destination file newfile already exists, it will overwrites the existing file. To avoid this use -i option with cp command.

Examples:

Copy the contents of file1 to file2.

cp file1 file2

The above command replaces the contents of file2 with file1.

Copy multiple files into single directory

cp file1 file2 file3 Javafiles

This copies whatever(number of) the files/directories specified between cp and Javafiles.

Want to copy same type of files to the Directory Javafiles

cp *.txt Javafiles

Linux cp command

Same way we can copy any number of JPG, JPEG, PDF, TAR, etc files from one location to another.

Copy from one directory to another.

cp Javatest JavaFiles

Copy other files from known location to the current location.

cp /opt/sap/test/ .

Powered by k2schools
Skip to toolbar