Skip to main content

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

Warp Terminal

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.

Abhishek Prakash