Terminus
How To Copy A Directory In Linux

How To Copy A Directory In Linux

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 [.inline-code]cp[.inline-code] 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 [.inline-code]cp[.inline-code] command with the [.inline-code]-r[.inline-code] flag (short for [.inline-code]--recursive[.inline-code]) as follows:

$ cp -r <source_directory> <destination_directory>

Where:

  • [.inline-code]source_directory[.inline-code] is the path to the directory you want to copy.
  • [.inline-code]destination_directory[.inline-code] is the path where you want to copy the directory to.

For example, to copy a directory called [.inline-code]client_data[.inline-code] to a folder called [.inline-code]old_client_data[.inline-code] you would use the following command:

$ 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 [.inline-code]ls[.inline-code] command to list the contents of the [.inline-code]old_client_data[.inline-code] folder:

$ ls old_client_data

[#easily-recall-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-with-ai]

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

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

[#copy-the-contents-only] Copying an entire directory vs. copying its contents [#copy-the-contents-only]

When using the [.inline-code]cp[.inline-code] 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 ([.inline-code]/[.inline-code]) to its path:

$ cp -r <source_directory>/ <destination_directory>

For example, if the [.inline-code]old_client_data[.inline-code] directory already exists and you only want to copy the contents of the [.inline-code]current_client_data[.inline-code] directory to it, you could use the following command:

$ 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 [.inline-code]current_client_data[.inline-code] folder based on patterns.

Including hidden files

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

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

[#prevent-overwriting-files] Preventing the overwriting of files [#prevent-overwriting-files]

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

[#copy-in-interactive-mode] The interactive flag [#copy-in-interactive-mode]

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

$ 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:

cp: overwrite 'file'? 

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

[#skip-overlapping-files] The no-clobber flag [#skip-overlapping-files]

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

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

This means that if a file with the same name already exists in the [.inline-code]destination_directory[.inline-code], it will not be copied from the [.inline-code]source_directory[.inline-code].

[#copy-multiple-directories] Copying multiple directories [#copy-multiple-directories]

The [.inline-code]cp[.inline-code] command can also be used to copy multiple directories at once, by specifying them before the destination folder:

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

For example, the following command will copy the [.inline-code]client_1[.inline-code], [.inline-code]client_2[.inline-code], and [.inline-code]client_3[.inline-code] directories into the [.inline-code]client_resources[.inline-code] directory:

$ cp -r client_1/ client_2/ client_3/ client_resources

[#copy-with-force] Copying files with force [#copy-with-force]

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

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

[#preserve-file-attributes] Preserving file attributes [#preserve-file-attributes]

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

$ 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 [.inline-code]-p[.inline-code] flag to preserve mode, ownership, and timestamps:

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

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

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

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

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

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

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

[#copy-with-symbolic-links] Copying directories with symbolic links [#copy-with-symbolic-links]

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:

  • [.inline-code]-P[.inline-code] or [.inline-code]--no-dereference[.inline-code] which tells the [.inline-code]cp[.inline-code] 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.
  • [.inline-code]-d[.inline-code] which is the same as [.inline-code]--no-dereference --preserve=links[.inline-code] which prevents [.inline-code]cp[.inline-code] from following the symbolic links, instead copying them as symbolic links themselves.

To remove their nature as symbolic links you can use:

  • [.inline-code]-L[.inline-code] or [.inline-code]--dereference[.inline-code] which will always follow the symbolic links in the [.inline-code]<source_directory>[.inline-code] and copy the original file to the [.inline-code]<destination_directory>[.inline-code].