How to Copy a Directory in Linux Command Line [Beginner's Tip]
This quick tip for beginners demonstrates how to copy a directory in Linux using the cp command. You'll also learn a few additional tips around copying directory.
β Abhishek Prakash
If you are new to Linux command line, you probably have this question in mind: How do I copy a directory and the files inside it to another directory in Linux command line?
Hereβs how to copy a directory in Linux:
cp -r source_directory destination_directory
You probably already know that you can use cp command to copy files in Linux. Do you know that you can use the same cp command to copy a folder in Linux command line?
Hah! You already tried that and perhaps got this error:
cp: -r not specified; omitting directory 'test_dir'
Let me show you a thing or two about copying directory in Linux.
Copy directory in Linux command line
You can definitely use the same cp command but with the recursive option -r to copy a folder with its content to another folder. The above mentioned error also hints that you missed the -r option.
All you got to do is to use the command in this fashion:
cp -r source_directory destination_directory
And now if you use ls command on the destination directory, it should have the entire source directory inside it.
The -r option allows the recursive option. This means the entire content of the directory including its own subdirectories, everything in the directory will be copied to the destination.
A few things to note about copying directory in Linux
Here are a couple of things to note and tips about copying folders.
The destination directory doesn't exist? It will be created
If the destination directory doesnβt exist (but the path exists), it will be created up to one level (explained in the next section). However, it will now copy the contents of the source directory, not the source directory itself.
For example, if you do this:
cp -r source_directory non_existing_directory
The non_existing_directory will be created with the content of the source_directory but it wonβt have the source_directory inside it. Just the files of source_directory will be copied. It would be like non_existing_directory will be a replica of source_directory.
abhishek@linuxhandbook:~$ ls
test_dir
abhishek@linuxhandbook:~$ cp -r test_dir new_dir
abhishek@linuxhandbook:~$ tree
.
βββ new_dir
β βββ c.xyz
β βββ myzip1.zip
β βββ myzip2.zip
βββ test_dir
βββ c.xyz
βββ myzip1.zip
βββ myzip2.zip
2 directories, 6 files
Can't create nested directory while copying
You cannot use the above command to create nested directory structure.
For example, if you try to use cp -r source_dir dir1/dir2/dir3 but dir2 and dir3 donβt exist, it wonβt create the nested directory structure and the command fails.
Preserve the original file attributes
One last tip to keep things short. If you use the -a option along with the -r option, it will preserve the original file information such as file permissions, file timestamps etc. It will archive the directory to the new location instead of creating it afresh.
Thatβs it
This much information should be enough for you to know how to copy a directory in Linux. If you have questions or suggestions, please feel free to leave a comment.
Creator of Linux Handbook and It's FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.