How to Copy Directory in Linux / Unix


If you are looking to copy a directory in Linux using a command line, then here I have explained what command to use to copy a folder in Linux or Unix based OS.
To be able to copy a directory you need to use the cp command in Linux.

To know more about the cp command you can visit the cp (1) man page or execute the following command in a Linux or unix machine to get the manual page for cp command:

man 1 cp

 

What mistakes people do in Linux Copy Directory Command:

Typically people use the following cp command without any option to copy Linux directory which does not work:

cp <source Linux directory> <destination Linux directory>

Well, to copy a directory needs a recursive option on cp command.

Sometimes people also execute in the following order:

cp -r <destination Linux directory> <source Linux directory>

Now, this command copies the destination directory into the source directory.

You can visit the cp man page to see that cp command uses the -R or the -r or the –recursive option to copy directory recursively.
 

So, What is the correct Linux Copy Directory Command:

To copy a directory (with all the files in it) to another directory in Linux you can use the following command:

cp -r <Which Linux directory to copy> <Where you want to copy>

If both the directories are in the current working directory then the Linux Copy Directory command will look like this:

cp -r src_dir/ dest_dir/

The above command will copy the src_dir to a dest_dir in current working directory.

If you want to copy the directory to a directory which is not in working directory then you need to add the complete path in the destination directory:

cp -r src_dir/ /usr/src/

This command will copy the src_dir (which must be in the current working directory) to /usr/src directory.

See the below image to get an idea how the above Linux Copy Directory command works:
cp -r directory

  1. ls -la lists the dest_dir/ content before copying anything.
  2. Then copy the src_dir/ to dest_dir/ using -r option
  3. List the content in dest_dir/ after copying
  4. List the current directory data. You can see the src_dir/ is still present here, because we have copied the directory and not moved.

All the following commands to copy a folder in Linux does the same thing as above:

cp -r src_dir dest_dir
cp -r src_dir dest_dir/
cp -r src_dir/ dest_dir

Just NOTE the forward slash in each command.
All of the above commands do the same thing of copying a directory to another directory in Linux or Unix based operating system.

Well, the forward slash holds a meaning when you delete a directory recursively.
I will cover that while explaining the rm command in Linux.
 

How to copy all Files in a directory into another Directory:

If you want to copy all the files in a directory (without any directory inside) and also not the directory itself to be copied then you need to execute the following command:

cp <Which Linux directory to copy>/* <Where you want to copy>

For example:

cp /home/shibaa987/Downloads/* /tmp/Downloads/

I have the following contents in /home/shibaa987/Downloads/ directory:
Two files named: file1.txt and test.c
and a folder named: test
files in directory

The above command will copy ONLY the files and leave any directory present in the /home/shibaa987/Downloads/ while copying to /tmp/Downloads/ directory.

See the below image that lists out what all content have been copied to /tmp/Downloads/ directory:
cp command without -r option


If you want to copy all the files and directories in a directory to copied and not the directory itself to be copied then you need to execute the following command:

cp -r <Which Linux directory to copy>/* <Where you want to copy>

For example:

cp -r /home/shibaa987/Downloads/* /tmp/Downloads/

The above command will copy all the files and directories present in the /home/shibaa987/Downloads/ to /root/Downloads directory.

See the below image that shows the content after copying with -r option in cp command:
cp command with -r option

I think this should do what you are looking for.
If still face some problem either comment here or create a forum thread in Linux commands Forum.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.