If you want to use the copy directory Linux command, what you’re really doing is manipulating a file. Linux has an interesting design: it’s a file-based system. While this may not be intuitive, directories themselves are files—they’re simply files that contain a list of other files. When performing directory management, this becomes important.

Person working on laptop

How to copy a Linux directory using the copy directory Linux command

To copy a Linux directory, you will use the “cp” command. You use it as follows:

$ cp -R <original_folder> <new_folder>
Linux Administration: The Complete Linux Bootcamp for 2022

Last Updated September 2022

  • 213 lectures
  • All Levels
4.7 (1,408)

Linux Administration (Ubuntu and CentOS) for Beginners. Get the Linux skills to boost your career and get ahead. | By Andrei Dumitrescu, Crystal Mind Academy

Explore Course

Let’s say that you had a folder called “Pictures,” and you wanted to back it up. You might use the following command:

$ cp -R Pictures Pictures_Backup

Now, you have two directories with the same content: Pictures and Pictures_Backup.

The -R flag denotes that the Linux command is being run recursively. If you don’t use the R flag when copying files and directories, any subdirectories will not be copied over to the target directory. 

This goes back to how Linux treats directories and files essentially the same; you need to have an extra -R flag, or you’re just copying the contents of the directory itself, not any other directories it points to.

Likewise, if you were trying to copy the files themselves, you could use the same command:

$ cp file.txt backup.txt

This will create an extra copy of the file file.txt as backup.txt, and it’s essentially the same operation as creating a new directory.

If you didn’t want to copy the files from the source directory and you wanted to copy only the title, you would just use mkdir:

$ mkdir new_directory

This would create a new directory that’s now empty.

Copy multiple directories with the copy directory Linux command

Let’s say you wanted to copy the content from multiple directories or you wanted to copy multiple files at the same time. It’s still the same command.

For instance, you could do something like this:

$ cp -R old_directory_1 old_directory_2 new_directory

Multiple directories will be copied to the same location. You could perform the same command with multiple files if you desired. 

Copying multiple directories with the rsync command

The rsync command is used if you want to copy a directory to a remote location. In other words, you would be uploading or transmitting the file. You would use the rsync command as follows:

$ rsync -R <old_folder> <user>@<host>:<new_folder>

That being said, rsync isn’t always installed on a device. You may need to install rsync first:

$ sudo apt-get install rsync
$ sudo yum install rsync

Note that, to copy the directory to a remote source file, you will have to have valid user/host information to feed into the command line. 

Copying all files when you copy a directory in Linux

The cp command by itself usually copies nearly all your files. But you might have noticed that there was an original file not copied. This is because Linux copies regular files, but it might not copy all files (such as hidden or configuration files or subdirectories) by default.

If you want to make sure the directory, including all its files, is copied, use the cp command with both the -R and -a flags. This will copy even non-standard files or directories. But you may want to look directly into how to copy a file in Linux if you’re copying files rather than directories.

Moving a directory in Linux

Moving, renaming, and deleting files in Linux are all very similar to copying files. That’s because from a computer’s perspective, moving a file is a lot like copying the file to a new address then deleting the old address.

But to move a directory in Linux would be a different command:

$ mv old_directory new_directory_location

As an example, let’s say you were backing up your Pictures directory:

$mv Pictures /archive/

Now you’ve moved the entire directory to /archive/.

The permissions you need to copy directories

Linux is a permissions-based system. There are read, write, and execute permissions under Linux. If you don’t have read permissions on a directory, you can’t copy it. If you don’t have write permissions on a directory, you can’t copy a directory to it.

If you’re getting an error, you may need to use the “sudo” command, like so:

$sudo cp <old_directory> <new_directory>

The sudo command ensures that you’re using administrative permissions. If you aren’t logged in as a root user, you will need to do so. You may also need to use the chmod function to change your permissions.

Other than this, consider that the permissions may change when you copy a file. If you’re copying a file to another network drive, for instance, you will find that it will become open to that drive; it will adopt permissions from its directory structure.

To preserve everything about the directories and files when you’re copying them, use the -a flag. Again, the -a flag will copy over everything, including things like non-standard directories and files and permission settings.

Renaming a directory in Linux

What if you don’t need a copy of a directory? What if you just need it renamed? Actually, this is also the mv command. You would do this:

$mv Pictures Pictures2

Essentially, you’ve “moved” the directory to a different name. But in practice, you have renamed the directory successfully. There are many ways to rename a file in Linux, as well, depending on your end goal.

Deleting a directory in Linux

You can delete files in Linux using the rm command, as well as directories. However, you should be very aware that the rm command can be dangerous. Type something wrong, and, well… boom, deleted. If you’re on a network drive, it could be deleted forever.

Use the rm command as follows:

rm -d <directory_name>

You can also specifically use the rmdir (“remove directory”) command:

rmdir <directory_name>

That is the same as using the rm command with the -d flag, but it’s a little more direct and concise.

Copying directories and files with the Linux cp command

When it comes to Unix vs. Linux, both Linux and Unix copy files very similarly, and it shouldn’t be difficult for you to copy directories or files with the cp command. In fact, it’s one of the most basic functions. After a while, you’ll realize that it’s even more natural to type cp into the commandline to copy a file in Linux than to press CTRL+V — that’s why Linux is used for so many situations in which efficiency is critical.

If you want to know more about Linux files and what you can do with Linux, you can look up some Linux commands or go through a Linux course.

Page Last Updated: March 2022

Top courses in Linux

Linux students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo