In Linux, Copy files and directories is one of the most common day-to-day tasks of any Linux users.

cp is a command-line tool in Linux operating systems used to copy files and directories from one place to another.

There are several ways to copy files on Linux operating systems. Each method has its own benefit, depending on what you need to get done.

In this tutorial, we will show you how to use cp command to copy files and directories with hands-on examples.

cp command help page

Basic Syntax

Basic syntax of the cp command is shown below:

cp [OPTIONS] SOURCE-FILE DESTINATION-FILE

This command copies first file to the second one.

cp [OPTIONS] SOURCE-FILE DESTINATION-DIRECTORY

This command copies the source file to the destination directory.

cp [OPTIONS] SOURCE-FILE1 SOURCE-FILE2 SOURCE-FILE3 DESTINATION-DIRECTORY

Copy All Files within Directory to Another Directory

This command copies all Files from a Source directory to the destination directory.

cp [OPTIONS] SOURCE-DIRECTORY DESTINATION-DIRECTORY

This command copies the source directory with all files and sub-directories to the destination directory.

A brief explanation of each option is shown below:

  • -a : Used to archive files.
  • -f : Force copy by removing the destination file.
  • -i : Prompt before overwrite the destination file.
  • -l : Hard link files instead of copying.
  • -L : Always follow symbolic links in SOURCE.
  • -R : Used to copy directories recursively.
  • -u : Copy only when the source file is newer than the destination file.
  • -v : Display what is being done.
  • -p : Used to preserve the file attributes like mode, ownership and timestamps.
  • -s : Symbolic link files instead of copying.
  • -n : Do not overwrite the existing file while copying.

Copy a Single and Multiple Files

To copy a single file use the following command:

cp file.txt file-backup.txt

Or

cp file.txt /opt/file-backup.txt

To copy multiple files at once into the directory /mnt use the following command:

cp file1.txt file2.txt file3.txt /mnt/

To copy multiple files from different location to /mnt directory use the following command:

cp /etc/passwd /etc/apt/sources.list /mnt/

Example:

You can use option -i with cp command to get a confirmation prompt if the destination file exists.

cp -i file1.txt file2.txt file3.txt /mnt/

You will be asked to confirm before copying the files:

cp: overwrite ‘/mnt/file1.txt’? y
cp: overwrite ‘/mnt/file2.txt’? y
cp: overwrite ‘/mnt/file3.txt’? y

Example:

You can use option-v with cp command to display verbose output:

cp -v file1.txt file2.txt file3.txt /mnt/

You should see the following output:

‘file1.txt’ -> ‘/mnt/file1.txt’
‘file2.txt’ -> ‘/mnt/file2.txt’
‘file3.txt’ -> ‘/mnt/file3.txt’

Example:

If you don’t want to overwrite the existing destination files while copying. You can use -n option with cp command to achieve this as shown below:

cp -n file1.txt /mnt/

Copy a Single and Multiple Directory

By default, cp command does not copy directory. You will need to specify -R option with cp command to copy a directory.

Run the following command to copy a single directory:

cp -R dir1 dir1-bak

If the destination directory exists, you can copy only the files and sub-directories but not the target directory, use the * option:

cp -R dir1/* dir1-bak

To copy multiple directories /etc/apt and /etc/network into the directory /mnt run the following command:

cp -R /etc/apt /etc/network /mnt/

To copy multiple directories from the same location into the directory /mnt run the following command:

cp -vR dir1 dir2 dir3 /mnt/

Example:

You can also copy the multiple files and directories at once using the following command:

cp -R file1.txt file2.txt /etc/apt file3.txt /etc/network /mnt/

Note: When copying multiple files and directories, the destination must be a directory.

Take a backup when copying a file

If a copy operation will overwrite a destination file, you can use -S option to create a back up of the file.

For example, copy file1.txt , file2.txt , file3.txt and create a backup copy with .bak extention run the following command:

cp -S .bak file1.txt file2.txt file3.txt /mnt/

You can now list all files with their backup copy using the following command:

ls /mnt/

You should see the following utput:

file1.txt file1.txt.bak file2.txt file2.txt.bak file3.txt file3.txt.bak passwd sources.list usb

Example:

Create a Hardlink and Symbolic Link

Most people already know what both a hardlink and symlink are, but for those who don’t know what they are, here’s their definitions:

  1. A Hard link is a directory entry that associates a name with a file on a file system.1
  2. A symbolic link (also symlink or soft link) is the word that defines any file that references to another file or directory in the form of an absolute or relative path and that affects path-name resolution. 2

You can create a hardlink instead of copying using the -l option with cp command:

cp -l file1.txt file1-hard.txt

To create a symlink instead of copying using the -s option with cp command:

cp -s file2.txt file2-sym.txt

Example:

Preserve file attributes while copying

If you want to preserve the file attributes like mode, ownership and timestamps while copying then use -p option with cp command as shown below:

cp -p file1.txt /tmp/test1/

Now, match the file attributes on both source and destination files:

ls -l file1.txt
-rw-r--r-- 1 root root 0 Dec 29 11:52 file1.txt
ls -l /tmp/test1/file1.txt
-rw-r--r-- 1 root root 0 Dec 29 11:52 file1.txt

Example:

Do not follow Symlinks

By default, cp command follow symlinks in the source while copying. You do not follow symbolic links using the option -P with cp command:

First, check the symlink with the following command:

ls -l file1-sym.txt

You should see the following output:

lrwxrwxrwx 1 root root 9 Dec 29 12:06 file1-sym.txt -> file1.txt

Next, copy file1.txt with -P option as shown below:

cp -P file1.txt /mnt/file1.txt

Next, check the file attributes with the following command:

ls -l /mnt/file1.txt

You should see the following output:

-rw-r--r-- 2 root root 0 Dec 29 12:15 /mnt/file1.txt

Example:

Copy only Updated Files (Incremental Copy)

In some cases, you want to copy only those files that are newer than the destination files. You can achieve this using -u option with cp command:

To understand it better, let’s create a two blank files file1.txt and file2.txt:

touch file1.txt file2.txt

Next, copy both file to the /mnt/ directory as shown below:

cp -v file1.txt file2.txt /mnt/

You should see the following output:

‘file1.txt’ -> ‘/mnt/file1.txt’
‘file2.txt’ -> ‘/mnt/file2.txt’

Next, make some changes in the source file1.txt file with the following command:

echo "Hi How Are You?" >> file1.txt

Next, copy only updated file into the /mnt directory using the following command:

cp -v -u file* /mnt/

You should see that only file1.txt will be copied to the /mnt directory:

‘file1.txt’ -> ‘/mnt/file1.txt’

Example:

Conclusion

In the above article, we’ve learned how to copy files and directories with cp command using several different methods. I hope you have now enough knowledge on how to use the cp command to perform day-to-day tasks including copy multiple files, directories and even backing up your files (Full & Incremental).

Feel free to ask any below in the comments if you have any!

 

Sources:

1. Hardlink, Wikipedia.org, https://en.wikipedia.org/wiki/Hard_link

2. Symlink, Wikipedia.org, https://en.wikipedia.org/wiki/Symbolic_link