If you are a system administrator, copying files and directories is one of the most common tasks in your day-to-day work. The cp command is a basic Linux command used for copying files and directories from one location to another. By default, the cp command is available in almost all Unix- and Linux-like operating systems.

In this tutorial, we will show you how to use the cp command to copy files and directories in Linux.

The cp Command Options

The basic syntax of the cp command is shown below:

cp [OPTION] [SOURCE] [DESTINATION]

A brief explanation of each available option is shown below:

  • -a : Copy files and directories recursively and preserve links.
  • -i : You will be prompted before overwriting any existing file on the destination.
  • -r : Copy directory recursively.
  • -f : Remove an existing destination file if it can not be opened.
  • -u : Copy only when the SOURCE file is newer.

Copy a File to Directory

If you want to copy a single file named /etc/crontab to the /mnt directory, run the following command:

cp /etc/crontab /mnt

You can now verify it with the following command:

ls -l /mnt

You should see your file in the following output:

-rw-r--r-- 1 root root 1042 Jun 21 11:32 crontab

Copy Multiple Files to Directory

If you want to copy multiple files named /etc/hosts and /etc/hostname at the same time to the /mnt directory, run the following command:

cp /etc/hosts /etc/hostname /mnt

If you want to display verbose output during the copying process, run the following command:

cp -v /etc/hosts /etc/hostname /mnt

You should see the following output:

'/etc/hosts' -> '/mnt/hosts'
'/etc/hostname' -> '/mnt/hostname'

Copy a File Interactively

If you want to copy a file interactively, use the option -i with cp command. This option will prompt you before copying the file to the destination directory if the same file already exists.

cp -i /etc/crontab /mnt

You should see the following output:

cp: overwrite '/mnt/crontab'? yes

If you don’t want to overwrite an existing file, you can use the option -n.

cp -vn /etc/crontab /mnt

This will not prompt for the overwrite and also will not overwrite the existing file.

Copy a Directory Recursively

You can use option -r with cp command to copy files and directory recursively.

cp -r /etc/apache2 /mnt

You can also use -a option to archive the files and directory during the copy.

cp -ar /etc/apache2 /mnt

Preserve mode, ownership, and timestamps

If you want to preserve mode, ownership, and timestamps after copying files and directories, you can use -p option:

cp -arp /etc/apache2 /opt

Backup a Destination File

By default, the cp command will overwrite the file on the destination directory if the same file exists. In this case, you can use –backup option to back up any existing destination file during the copying process.

cp --backup=simple -v /etc/hosts /mnt

You should see the following output:

'/etc/hosts' -> '/mnt/hosts' (backup: '/mnt/hosts~')

Copy Multiple Files from One Directory to Another

You can also copy multiple files from one directory to another using the *. For example, if you want to copy all PHP files from the /var/www/wordpress directory to the /mnt directory, you can copy them with the following command:

cp -r /var/www/wordpress/*.php /mnt/

This command will find all files with .php extension in the /var/www/wordpress/ directory and copy them into the /mnt directory.

Conclusion

In this guide, you learned how to use the cp command to copy files and directories. Get started with the cp command today on a Linux VPS from Atlantic.Net!