How to Copy a Directory in Linux – Beginner’s Tutorial


I know how to copy files in Linux by using cp command, but on same when I tried to copy the Documents directory, and I got the following error “cp: -r not specified; omitting directory ‘Documents'”

Are you facing the same problem?

Are you a new user of Linux and don’t know how to copy a directory in Linux? Don’t worry anymore. In this article, I cover how to copy the directory in Linux with all possibilities.

So you will not face any problem in future to copy directory in Linux

Article Content

Pre-requisites to Copy a Directory in Linux

Before you start copy a directory in Linux you must know two thing

  • You have read permission of source directory
  • And You have write permission of destination directory

If you don’t have write permission on the destination you will get “Permission denied” error. As following.

vijay@Ubuntu-19:~$cp -r Documents /var/backups/
cp: cannot create directory '/var/backups/Documents': Permission denied

Copy Directory from source to destination

cp command is used to copy files from one location to another. The basic syntax of the command is.

cp [Option] [Source] [Destination]

You have used already cp command to copy, rename files.

You can use cp command followed by -r option, source, and destination. You can use the complete path or relative path. As see example follow. Full path /home/vijay/backups and a relative path is backups only.

vijay@Ubuntu-19:~$cp -r Documents /home/vijay/backups
vijay@Ubuntu-19:~$cp -r Documents backups

Copy and Rename Directory in Linux

Basically, copy means creating a new directory with consist of old directory. but sometimes I need to copy a directory and rename it. Read How to rename a file in Linux

Simple way copy directory and rename it. but another copy directory with a new name. You can give destination location with the new directory name.

$cp -r /source_directory_path /Destination path/New_name_for_directory

vijay@Ubuntu-19:~$cp -r Documents/ Documents_backup

Copy Directory with Subdirectories and Files

-r option is enough to copy a directory with its sub-directories and files. It’s default mode, So you don’t need to change anything.

vijay@Ubuntu-19:~$cp -r Python-3.7.3 Python-3.7.3_back

Copy Directory with Verbose Option

if you want to copy a directory verbosely then use cp command followed by -v option. In this case, don’t forget to use -r option. The complete command will be as shown following.

vijay@Ubuntu-19:~$cp -rv Python-3.7.3 Python-3.7.3-verbose
'Python-3.7.3' -> 'Python-3.7.3-verbose'
'Python-3.7.3/config.status' -> 'Python-3.7.3-verbose/config.status'
'Python-3.7.3/config.log' -> 'Python-3.7.3-verbose/config.log'
'Python-3.7.3/Doc' -> 'Python-3.7.3-verbose/Doc'
'Python-3.7.3/Doc/c-api' -> 'Python-3.7.3-verbose/Doc/c-api'

Copy Directory and Preserve content Attributes.

When you copy the directory in Linux using -r option. Attributes of the directory’s content will be changed. Attributes are followings:

  • Modification time/date
  • Access time
  • File flags
  • File mode
  • User ID (UID)
  • Group ID (GID)
  • Access Control Lists (ACLs)
  • Extended Attributes (EAs)

I have copied directory Python-3.7.3 to Python-3.7.3_back And I saw file and folder attribution of modifying date has been changed.

You can see in the following examples:

vijay@Ubuntu-19:~$ls -l Python-3.7.3
-rw-r--r--  1 root  root      3105 Jun 19 10:59 python-config
-rw-r--r--  1 root  root      2042 Jun 19 10:59
-rw-r--r--  1 root  root     65363 Jun 19 10:59
-rw-r--r--  1 vijay vijay    10113 Mar 26 01:51 README.rst
-rw-r--r--  1 vijay vijay   102108 Mar 26 01:51
vijay@Ubuntu-19:~$ls -l Python-3.7.3_back
-rw-r--r--  1 vijay vijay     3105 Jun 20 11:26 python-config
-rw-r--r--  1 vijay vijay     2042 Jun 20 11:26
-rw-r--r--  1 vijay vijay    65363 Jun 20 11:26
-rw-r--r--  1 vijay vijay    10113 Jun 20 11:26 README.rst
-rw-r--r--  1 vijay vijay   102108 Jun 20 11:26

Now, I want to copy again with preservation of contents attribution. I studied the manual page and help option.

I found -p option is used for preserving attribution at the time of copy directory. I tested it I got success.

You can use cp command followed by -rp command to preserve attribution of contents at the time of taking backup or copying directory.

See the example below:

vijay@Ubuntu-19:~$cp -rp Python-3.7.3 Python-3.7.3-new

Verification of cp -rp command:

vijay@Ubuntu-19:~$ls -l Python-3.7.3-new
-rw-r--r--  1 root  root      3105 Jun 19 10:59 python-config
-rw-r--r--  1 root  root      2042 Jun 19 10:59
-rw-r--r--  1 root  root     65363 Jun 19 10:59
-rw-r--r--  1 vijay vijay    10113 Mar 26 01:51 README.rst
-rw-r--r--  1 vijay vijay   102108 Mar 26 01:51

Copy directory in Linux using rsync command

rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details.

rsync is a file transfer program capable of an efficient remote update via a fast differencing algorithm.

vijay@Ubuntu-19:~$rsync -a /var/www/public_html/ /var/www/public_html_backup/

Warning: the source directory must end with trailing slash /. If you don’t do same, rsync will copy the source directory inside the destination directory. If you add a trailing slash end of the source directory it will copy only the contents of source directory to the destination directory.


You have learned lots of option to copy a directory in Linux by using the command line. You can use a graphic interface but I didn’t describe. If you want to read more about cp command read the official documentation here

If you have any question related to this article write in the comment box.


If you like our content, please consider buying us a coffee.
Thank you for your support!

Leave a Reply

Your email address will not be published. Required fields are marked *