How To Copy A Directory In Linux

Philip Wilkinson
Philip WilkinsonSoftware Engineer, Amazon
Published: July 26, 2024

Copying directories in Linux is a fundamental task for software engineers and system administrators. Whether you need to back up data, duplicate projects, or migrate files, understanding how to copy directories is essential. In this guide, we'll explore the cp command and its various options for copying directories.

The short answer

To copy a directory and all its contents, including its subdirectories, you can use the cp command with the -r flag (short for --recursive) as follows:

Bash
$ cp -r <source_directory> <destination_directory>

Where:

  • source\_directory is the path to the directory you want to copy.
  • destination\_directory is the path where you want to copy the directory to.

For example, to copy a directory called client\_data to a folder called old\_client\_data you would use the following command:

Bash
$ cp -r /client_data /old_client_data

To verify that the files and directories have been copied into the destination directory, you can use the ls command to list the contents of the old\_client\_data folder:

Bash
$ ls old_client_data

Easily retrieve this command using Warp’s AI Command Suggestions

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering copy directory into the AI Command Suggestions will prompt a cp command that can then be quickly inserted into your shell by doing CMD+ENTER.

Copying an entire directory vs. copying its contents

When using the cp command, if the destination directory already exists, you can then choose whether to copy just the contents of the source directory or the entire directory by adding or removing a trailing slash character (/) to its path:

Bash
$ cp -r <source_directory>/ <destination_directory>

For example, if the old\_client\_data directory already exists and you only want to copy the contents of the current\_client\_data directory to it, you could use the following command:

Bash
$ cp -r current_client_data/ old_client_data

You can also extend this by using Wildcards to selectively copy certain files or subdirectories from the current\_client\_data folder based on patterns.

Including hidden files

To copy all files including hidden files (i.e. files starting with a dot .), you can append the following expression /. to the end of the source directory's path as follows:

Bash
$ cp -r <source_directory>/. <destination_directory>

Preventing the overwriting of files

By default, the cp command will automatically overwrite existing files with the same name in the destination directory. To prevent this, you can either use the -i flag or the -n flag.

The interactive flag

To be prompted when an override is about to occur, you can use the cp command with the -i flag (short for --interactive) as follows:

Bash
$ cp -r -i <source_directory> <destination_directory>

For each file that could be overwritten, you will be prompted in the command line with something similar to:

Bash
cp: overwrite 'file'?

If you wish to overwrite the file, you can type y and press ENTER or type n followed by ENTER to skip it.

The no-clobber flag

To prevent overwrites without being prompted, you can use the cp command with the -n flag (short for --no-clobber) as follows:

Bash
$ cp -r -n <source_directory> <destination_directory>

This means that if a file with the same name already exists in the destination\_directory, it will not be copied from the source\_directory.

Copying multiple directories

The cp command can also be used to copy multiple directories at once, by specifying them before the destination folder:

Bash
$ cp -r <source_folder_1> … <source_folder_n> <destination_folder>

For example, the following command will copy the client\1, client\2, and client\_3 directories into the client\_resources directory:

Bash
$ cp -r client_1/ client_2/ client_3/ client_resources

Copying files with force

In cases where the destination cannot be opened, you can use the -f flag (short for --force) to remove destination files or directories and try again:

Bash
$ cp -r -f <source_directory> <destination_directory>

Preserving file attributes

To preserve file attributes when copying directories, such as symbolic links, file permissions, and ownership, you can use the -a flag (short for --archive) as follows:

Bash
$ cp -r -a <source_directory> <destination_directory>

On the other hand, if you want a more fine-grained preservation of file attributes, you can either use the -p flag to preserve mode, ownership, and timestamps:

Bash
$ cp -r -p <source_directory> <destination_directory>

Or you can use the --preserve flag followed by a list of attributes, such as mode, ownership, timestamps, content, links, extended attributes or all of them together:

Bash
$ cp -r --preserve=<attribute_list> <source_directory> <destination_directory>

For example, to preserve all attributes, you can use the following command:

Bash
$ cp -r --preserve=all <source_directory> <destination_directory>

And to preserve hard links and ownership, you can use the following command:

Bash
$ cp -r --preserve=links,ownership <source_directory> <destination_directory>

When copying directories using symbolic links, you have a few options of flags to use as to how they are treated.

To preserve their nature as symbolic links you can use:

  • -P or --no-dereference which tells the cp command not to dereference (follow) symbolic links. This means that the symbolic links themselves will be copied as links rather than copying files or the directories they point to.
  • -d which is the same as --no-dereference --preserve=links which prevents cp from following the symbolic links, instead copying them as symbolic links themselves.

To remove their nature as symbolic links you can use:

  • -L or --dereference which will always follow the symbolic links in the <source\_directory> and copy the original file to the <destination\_directory>.
Written by
Philip Wilkinson
Philip WilkinsonSoftware Engineer, Amazon
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.