cp command in Linux/Unix

cp is a Linux shell command to copy files and directories.

cp command syntax

Copy from source to dest

$ cp [options] source dest

cp command options

cp command main options:

option description
cp -a archive files
cp -f force copy by removing the destination file if needed
cp -i interactive - ask before overwrite
cp -l link files instead of copy
cp -L follow symbolic links
cp -n no file overwrite
cp -R recursive copy (including hidden files)
cp -u update - copy when source is newer than dest
cp -v verbose - print informative messages

cp command examples

Copy single file main.c to destination directory bak:

$ cp main.c bak

 

Copy 2 files main.c and def.h to destination absolute path directory /home/usr/rapid/ :

$ cp main.c def.h /home/usr/rapid/

 

Copy all C files in current directory to subdirectory bak :

$ cp *.c bak

 

Copy directory src to absolute path directory /home/usr/rapid/ :

$ cp src /home/usr/rapid/

 

Copy all files and directories in dev recursively to subdirectory bak:

$ cp -R dev bak

 

Force file copy:

$ cp -f test.c bak

 

Interactive prompt before file overwrite:

$ cp -i test.c bak
cp: overwrite 'bak/test.c'? y

 

Update all files in current directory - copy only newer files to destination directory bak:

$ cp -u * bak

cp code generator

Select cp options and press the Generate Code button:

Options
Force copy (-f)
Interactive - ask before overwrite (-i)
Link files (-l)
Follow symbolic links (-L)
No overwrite (-n)
Recursive directory tree copy (-R)
Update newer files (-u)
Verbose messages (-v)
 
Files / folders
Source files / folders:
Destination folder / file:
 
Output redirection
 
 

Click on textbox to select code, then copy and paste it into terminal:

 


See also

Write how to improve this page

LINUX
RAPID TABLES