How To Copy A Directory In Linux
Philip Wilkinson
Software Engineer, Amazon
Published: 7/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:
$ cp -r <source_directory> <destination_directory>
Run in Warp
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:
$ cp -r /client_data /old_client_data
Run in Warp
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:
$ ls old_client_data
Run in Warp
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:
$ cp -r <source_directory>/ <destination_directory>
Run in Warp
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:
$ cp -r current_client_data/ old_client_data
Run in Warp
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:
$ cp -r <source_directory>/. <destination_directory>
Run in Warp
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:
$ cp -r -i <source_directory> <destination_directory>
Run in Warp
For each file that could be overwritten, you will be prompted in the command line with something similar to:
cp: overwrite 'file'?
Run in Warp
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:
$ cp -r -n <source_directory> <destination_directory>
Run in Warp
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:
$ cp -r <source_folder_1> … <source_folder_n> <destination_folder>
Run in Warp
For example, the following command will copy the client_1, client_2, and client_3 directories into the client_resources directory:
$ cp -r client_1/ client_2/ client_3/ client_resources
Run in Warp
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:
$ cp -r -f <source_directory> <destination_directory>
Run in Warp
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:
$ cp -r -a <source_directory> <destination_directory>
Run in Warp
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:
$ cp -r -p <source_directory> <destination_directory>
Run in Warp
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:
$ cp -r --preserve=<attribute_list> <source_directory> <destination_directory>
Run in Warp
For example, to preserve all attributes, you can use the following command:
$ cp -r --preserve=all <source_directory> <destination_directory>
Run in Warp
And to preserve hard links and ownership, you can use the following command:
$ cp -r --preserve=links,ownership <source_directory> <destination_directory>
Run in Warp
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>.
Written by
Philip Wilkinson
Software Engineer, Amazon
Filed Under
Related Articles
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.
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.
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.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
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 Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
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.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.