How To Copy A Directory In Linux | Warp
Introducing Warp Factories: open, flexible infrastructure for building your own cloud software factory. Learn More
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>
Copying directories 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:
- -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>.