The Linux ‘cp’ Command: Copy files and Directories

'cp' Command

This tutorial will teach you to use the Linux cp command – the command that copies files from one directory to another.

Linux is the preferred platform for a lot of developers. If you’re new to Linux it won’t be long before you need to start copying files from one place to another – read on for an explanation of the Linux cp command and some tips and tricks on how to use it.

The cp Command

The cp command has a simple purpose – to copy files and folders from one place to another.

It can do this across any file system connected to the Linux system and will preserve the original file while giving you options to rename and overwrite the copied files.

The syntax for the cp command according to its manual page is:

cp [OPTIONS] SOURCE DESTINATION

cp Command Options

When entering your copy command, you will be able to specify what you want to be done with files that already exist, and whether to include directory contents in the copy. Here are some commonly used options:

-b Backup (make a backup of each destination file)

-f Force (if an existing destination file cannot be opened, remove it and try again)

-i Interactive (prompt before overwrite)

-n No-clobber (do not overwrite an existing file)

-p Preserve (preserve mode, ownership, timestamps)

-r Recursive (copy directories and their contents)

-u Update (copy only when the SOURCE file is newer than the destination file or when the destination file is missing)

-v Verbose (explain what is being done)

Command Examples for Copying Files and Directories Using cp

Copying a Single File

Simply enter the cp command followed by the path of the source and destination files, separated by a single space:

cp /path/to/source/file.txt /path/to/destination/file.txt

Copying a Directory and its Contents

Enter the cp command followed by the -r (recursive) option, and the path of the source and destination directories, separated by a single space. The entire source directory will be copied to the destination directory, with its original name:

cp -r /path/to/source_directory /path/to/destination_directory

Copying Multiple Files or Directories

To copy multiple files to the destination directory, just list them all separated by a space – the last path given will be treated as the destination:

cp source_file_1.txt source_file_2.txt path/to/destination_directory

You can also do the same when copying multiple directories if you pass the -r (Recursive) option:

cp -r source_directory_1/ source_directory_2/ /path/to/destination_directory

To copy all of the files and directories in the source directory to another directory you can use a wildcard (*):

cp -r /path/to/source/* /path/to/destination

Copying files With a Certain Filename or Extension

You can add a filename mask to copy files with a matching name or extension using a wildcard (*). Here’s how to copy every JPG image from the source directory to the destination directory:

cp /path/to/source/*.jpg path/to/source/*.JPG /path/to/destination_directory

Copying a symlink File

Symlink files are files that do not contain data and are just a reference to another file on the disk. They are excluded by default when using the cp command. To include them use the -d option:

cp –d source_file_1.txt /path/to/destination_directory

Copying Only Files Less Than 7 Days Old

You can combine the cp command with other commands for more control over what you’re copying. This example will copy only files less than 7 days old, by filtering them using the find command, and then executing the cp command:

find /path/to/files* -mtime -7 -exec cp {} /mnt/nas/ \;

Viewing the Results of Your Copy

You can view the contents of the destination directory using the ls command:

ls -hl /path/to/destination_directory

Conclusion

Copying files is a necessary day-to-day task and the syntax will quickly become second nature to you. For other common Linux command line tasks, check out our other articles.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *