Development

Copying files and directories on Linux

The basics of copying files and whole directories on Linux.

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'

 

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago