---
title: How to Copy Files and Directories in Linux Using the cp Command
url: https://docs.vultr.com/how-to-copy-files-and-directories-in-linux-using-the-cp-command
description: Learn how to efficiently copy files and directories in Linux with the cp command. Discover essential options, syntax, and practical examples for everyday use.
publish_date: 2024-08-16T17:50:31.061193Z
last_updated: 2024-09-25T21:37:34.451454Z
author: Vishwas Agrawal
header_image: https://docs.vultr.com/public/doc-assets/1909/0568bd39-dee5-4743-a2dd-0b6dfed396b8.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## 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](https://docs.vultr.com/public/doc-assets/1909/07c658ea-02b0-4c25-bd73-bc35016a84fa.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/bf756e14-d5f7-4b65-a866-e5767d9aefbb.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/5c35f044-a81b-4138-836a-db59c76e659a.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/f0ca3f06-935b-4f1f-b90a-77f9c75b40d4.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/4a565f36-91c2-457c-a29f-ffc714433107.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/28bc189d-692a-4dd7-bc21-fdeffdd37b78.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/f80d7a7a-dd53-43bf-bbe7-a3b0f995e589.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/96b8f80f-482b-4021-9e42-f773c045fd6b.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/65f88809-5c97-4444-9613-b8457d290114.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/e2492210-d812-44f2-9870-1591c6e89c67.gif)

## 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](https://docs.vultr.com/public/doc-assets/1909/d8fdbab6-d365-43d0-80ba-6e49a175f010.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/2d2b526d-7616-42cf-adbc-253aef52dd73.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/186bb2c9-4ca0-451e-a325-13ed3b1989a7.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/136a192f-194f-4723-b207-ef4d8d50dc2e.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/2cb9277d-e7ce-4faa-968c-c80c9506168d.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1909/4fa61125-8ef1-4702-8554-69345755d102.gif)

## 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.
