How to Copy Files and Directories in Linux Using the cp Command

Updated on August 16, 2024
How to Copy Files and Directories in Linux Using the cp Command header image

Introduction

The cp command in Linux is used to copy files and directories from one location to another. It's also known as copy cp and used in file management, data backup, directories, and configuration files in a file system.

This article explains how to use the cp command in Linux to copy and manage files.

cp Command Syntax

Below is the basic cp command syntax:

console
$ cp [options] source destination

Within the above command, source is the file or directory to copy, and destination is the target location.

cp Command Options

Option Description
-a Archive mode, copies files recursively and preserves attributes such as symbolic links, file permissions, and timestamps.
-r Recursive, copies directories and the included contents.
-v Verbose mode, displays detailed information about the copying process.
-i Interactive mode, enables confirmation prompts before overwriting files.
-u Copies files only when the source file is newer than the destination file or when the destination file is missing.
-f Forcefully overwrites existing files without a confirmation prompt.
-p Preserve the existing file attributes, such as mode, ownership, and timestamps.

Practical Examples of the cp Command

  1. Copy a file in your working directory to another directory.

    console
    $ cp file1.txt /home/user/backup/
    

    The above command copies file1.txt to the /home/user/backup/ directory.

    Output:

    copy a file command

  2. Copy multiple files to a specific directory.

    console
    $ cp file1.txt file2.txt file3.txt /home/user/backup/
    

    The above command copies file1.txt, file2.txt, and file3.txt to the /home/user/backup/ directory.

    Output:

    copy multiple files command

  3. Recursively copy a directory and its contents.

    console
    $ cp -r /home/user/documents /home/user/backup/
    

    The above command copies the documents directory and all included contents to the backup directory.

    Output:

    copy directory command

  4. Copy a file and output detailed information about the command's processes.

    console
    $ cp -v file1.txt /home/user/backup/
    

    The above command uses the verbose option -v to display detailed information about the file1.txt copying process.

    Output:

    copy file with Display information command

  5. Copy a file and include a confirmation prompt before overwriting the existing file.

    console
    $ cp -i file1.txt file2.txt
    

    The above command outputs a confirmation prompt before overwriting an existing file to the destination directory.

    Output:

    copy with confirmation

  6. Copy a file if the source is different from the destination or does not exist.

    console
    $ cp -u file1.txt /home/user/backup/
    

    The above command copies file1.txt only if it's newer or the destination file does not exist.

    Output:

    copy when new source

  7. Forcefully copy a file.

    console
    $ cp -f file1.txt file2.txt
    

    The above command overwrites the existing file in the destination without any confirmation prompt.

    Output:

    forceful copy

  8. Copy a file and preserve the original file attributes.

    console
    $ cp -p file1.txt file2.txt
    

    The above command copies file1.txt while preserving its mode, ownership, and timestamps.

    Output:

    copy preserve file

  9. Enable archive mode while copying.

    console
    $ cp -a /home/user/new /home/user/backup/
    

    The above command recursively copies the /home/user/new to /home/user/backup/ and preserves the original directory attributes.

    Output:

    copy in archive mode

  10. Copy all files.

    console
    $ cp *.txt /home/user/new
    

    The above command copies all files with a .txt extension to the /home/user/new directory.

    Output:

    copy all files

Advanced Examples and Combined Uses

  1. Interactively copy files and display the command processes.

    console
    $ cp -iv file1.txt /home/user/backup/
    

    The above command combines interactive and verbose modes to enable confirmation prompts before overwriting the destination file and displaying detailed information.

    Output:

    cp -iv command

  2. Copy a file and preserve all existing attributes.

    console
    $ cp -pv file1.txt /home/user/backup/
    

    The above command preserves all file attributes and provides a verbose output about the copy process.

    Output:

    cp -pv command

  3. Copy newer files with a verbose output about the copy process.

    console
    $ cp -uv file1.txt file2.txt /home/user/backup/
    

    The above command copies newer files file1.txt file2.txt to the /home/user/backup directory with a verbose output about the copy command processes.

    Output:

    cp -uv command

  4. Combine and copy files using the find and xargs commands.

    console
    $ find . -name "*.txt" -type f -print | xargs cp -t /home/user/backup/
    

    The above command finds all .txt files in the active working directory and copies the results to the /home/user/backup/ directory.

    Output:

    find and xargs command

  5. Copy files and create a backup of the existing files.

    console
    $ cp --backup file1.txt /home/user/backup/
    

    The above command copies file1.txt to the /home/user/backup/ directory and creates a backup of any existing files.

    Output:

    cp --backup command

  6. Use rsync to enable efficient copying to compare the source and destination files.

    console
    $ rsync -avh /home/user/documents/ /home/user/backup/
    

    The above command uses rsync to compare and copy the source directory /home/user/documents/ files with the destination directory /home/user/backup/. rsync is a powerful tool that works similar to the cp command and enables the copying and verification of files while preserving the source attributes.

    Output:

    rsync command

Conclusion

You have used the cp command in Linux to copy files, and directories while preserving the original file attributes. For more command options, run the man cp command to view the cp manual page.