How to Copy Directory in Linux / Unix

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.

If you go to the cp man page then you 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:
copy directory with -r option

  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 my 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.

Previous How To Protect Contents and Images On A Site From Being Copied or duplicated

About author

siba 698 posts

A B.Tech degree holder with more than 5 years of experience on Embedded Systems design domain. He has worked on Aerospace domain, NAND storage, Server storage and Wireless networking.

View all posts by this author →

You might also like

Linux Guides 0 Comments

How to Create a USB Bootable Ubuntu Linux Live Disk

Looking to make a Live Ubuntu Linux USB disk? then you have arrived at the right place. Here I will provide you direct download link to the Ubuntu Linux ISO

Linux Guides 0 Comments

How to Learn Linux with an Online Linux Terminal

Are you looking for some online linux terminals to work with?? Yes, there are some Linux terminals available online which will be helpful for you to learn Linux on the

Linux Guides 0 Comments

How to Learn Linux Without Having a Linux Machine?

Wondering how to learn Linux without having a Linux PC with you?? You might be thinking, is it really possible or you’re just kidding me?? Trust me it’s all possible

0 Comments

No Comments Yet!

You can be first to comment this post!