Copying files and directories on Linux

The basics of copying files and whole directories on Linux.

linux copy files command

Copying files on a Linux OS can be done with cp this is obviously short for “copy”.

The syntax for cp is the source first and destination second

[source] [destination]


Simple file copy

Copying just one file into a directory:

cp image.jpg /data/photos/

Copying two or more files into a directory:

cp image.jpg another.jpg /data/photos/

Keep permissions

Use -p to keep file permissions and timestamps on the copied files

cp -p image.jpg /data/photos/


Copy a directory

With cp the flag -r means recursive, to copy a whole directory into another:

cp -r /data/photos /backups/photos

This will copy the contents of the /data/photos folder into /backups/photos

Don’t overwrite

To not overwrite the file if it exists use -n which is no clobber:

cp -r -n /data/photos /backups/photos

Get task output

Getting a visual output from the cp task is done with the -v flag

cp -v image.jpg /data/photos/

Will output

'image.jpg' -> '/data/photos/image.jpg'