Practical Ways to Copy Files and Directories with cp in Linux

Published:

The cp command is the standard way to copy files and directories in Linux. Depending on what you need, it can place a file into another folder, duplicate a file under a new name, copy several files at once, or handle existing files more carefully with interactive or non-overwriting options.

Copy a file into a directory

Basic form:

cp sourcefile diectory

The following example copies sfile.dat into the desdir directory:

alair@e64 MINGW64 ~/Documents/example
$ cp sfile.dat desdir/
alair@e64 MINGW64 ~/Documents/example
$ ls desdir/
sfile.dat

After the command runs, sfile.dat appears inside desdir.

Copy a file and give the copy a new name

Basic form:

cp sourcefile sourcefile_copy

Here, sfile.dat is copied as sfile_copy.dat:

alair@e64 MINGW64 ~/Documents/example
$ cp sfile.dat sfile_copy.dat

alair@e64 MINGW64 ~/Documents/example
$ ls
desdir/  sfile.dat  sfile_copy.dat

alair@e64 MINGW64 ~/Documents/example
$ cat sfile.dat
eof

alair@e64 MINGW64 ~/Documents/example
$ cat sfile_copy.dat
eof

Using cat shows that sfile.dat and sfile_copy.dat contain the same content.

Copy multiple files into one directory

You can pass more than one source file before the destination directory. In the example below, sfile.dat and sfile2.dat are copied into desdir:

alair@e64 MINGW64 ~/Documents/example
$ cp sfile.dat sfile2.dat desdir/

alair@e64 MINGW64 ~/Documents/example
$ ls desdir/
sfile.dat  sfile2.dat

This is useful when several files need to be collected into a single target location in one command.

Avoid overwriting files with the same name

By default, if a file with the same name already exists in the destination, cp will overwrite it.

If you want to prevent that, use the -n option. It tells cp not to overwrite existing files.

In the following example, two files are copied to desdir, but only the file that does not already exist is actually copied:

alair@e64 MINGW64 ~/Documents/example
$ ls
desdir/  sfile.dat  sfile1.dat  sfile2.dat  sfile_copy.dat

alair@e64 MINGW64 ~/Documents/example
$ ls desdir/
sfile.dat  sfile2.dat

alair@e64 MINGW64 ~/Documents/example
$ cp -n -v sfile.dat sfile1.dat desdir/
'sfile1.dat' -> 'desdir/sfile1.dat'

Because sfile.dat was already present in desdir, it was skipped. The -v option makes the command print what was copied, so only sfile1.dat appears in the output.

Use interactive mode when copying

If you want confirmation before overwriting files, use the -i option. In interactive mode, cp asks for confirmation for each file that would replace an existing one.

alair@e64 MINGW64 ~/Documents/example
$ cp -i *.dat desdir/
cp: overwrite 'desdir/sfile.dat'? y
cp: overwrite 'desdir/sfile1.dat'? y
cp: overwrite 'desdir/sfile2.dat'? y

This is a safer option when working with many files and you want manual control over each overwrite.

Copy an entire directory

To copy a directory, use the recursive option -r.

The example below copies desdir/ into lbc/:

alair@e64 MINGW64 ~/Documents/example
$ cp -r desdir/ lbc/

alair@e64 MINGW64 ~/Documents/example
$ ls lbc/
desdir/

After the command finishes, desdir appears as a subdirectory inside lbc.