Read in 4 minutes

last updated 

How to Copy Files and Directories in Linux

Copying files and directories is one of the most common tasks you’ll perform when working on the command line. In Linux, there are several commands for copying files with cp and rsync being the most popular.

It is a common practice to use the cp command when copying files and rsync command when copying directories.

Before you Begin

To be able to copy files and directories you must have at least read permissions on the source file and write permission on the destination directory.

Copying Files with cp Command

The cp command is a command-line utility for copying files and directories on Linux systems from one location to another on the same machine.

The most simple use case is to copy a file in the current working directory. For example, to copy a file named file.txt in the current directory as file_backup.txt run the following command:

cp file.txt file_backup.txt

If the destination file exists it will be overwritten. To prompt for confirmation use the -i option.

cp -i file.txt file_backup.txt

When using the cp command to copy a file, the new file will be owned by the user performing the command. Use the -p option to preserve the file mode, ownership and timestamps:

cp -p file.txt file_backup.txt

Another option that can be useful is -v. When using this option the command will print what is being done:

cp -v file.txt file_backup.txt
'file.txt' -> 'file_backup.txt'

Copy a file to another directory

To copy a file to another directory you need to specify the absolute or the relative path to the destination directory. In the following example we are copying the file file.txt to the /backup directory:

cp file.txt /backup

When specifying only the directory name as destination the copied file will have the same name as the original file.

If you want to copy the file under a different name you need to specify the desired file name:

cp file.txt /backup/new_file.txt

The command above will copy the file to the specified directory as new_file.txt.

Copy multiple files

It is also possible to copy multiple files at once by specifying the names of files followed by the destination directory:

cp file.txt file1.txt file2.txt directory

When copying multiple files the destination must be a directory.

The cp command also allows you to use pattern matching. For example, to copy all png files from the current directory to the /backup directory you would use:

cp *.png /backup

Copying Directories with cp Command

To copy a directory, including all its files and subdirectories, use the -R option. In the following example we are copying the directory ~/Pictures to ~/Pictures_backup:

cp -R Pictures Pictures_backup

The command above will create a destination directory and recursively copy all files and subdirectories from the source to the destination directory.

The options used when copying files can be also used when copying directory. The main difference is that when copying directories you need to use the -R switch.

Copying Files and Directories with rsync Command

Rsync is a fast and versatile command line utility that synchronizes files and directories between two locations. It can be used to copy files to local and remote locations.

Rsync provides a number of options that control every aspect of its behavior. The most widely used options is -a which will copy directories recursively, transfer special and block devices, preserve symbolic links, modification times, group, ownership, and permissions.

To copy a single file from one to another location you should would the following command:

rsync -a file.txt file_backup.txt

If the destination file exists rsync will overwrite it.

You can use the same command like the one above to copy a directory with rsync:

rsync -a /var/www/public_html/ /var/www/public_html_backup/

Rsync threats the source directories that end with a trailing slash / differently. If you add a trailing slash on the source directory it will copy only the contents of the directory to the destination directory. When the trailing slash is omitted rsync will copy the source directory inside the destination directory.

To learn more about rsync check the following guides:


In this guide, we have shown you how to copy files and directories in Linux and Unix-based systems using the cp and rsync utilities.

If you have any question, please leave a comment below.