How To Copy An Entire Directory In Linux – Entire Directory Recursive Backup

If you need to know how to copy an entire directory in linux, you have come to the right place. This task is common for system administrators, developers, and anyone managing files on a Linux server. The process is straightforward once you understand the right command and its options.

In Linux, copying a directory is not the same as copying a single file. You need to use the cp command with specific flags. The most important flag is -r or -R, which stands for recursive. This tells the system to copy the directory and everything inside it, including subdirectories and files.

Let us walk through the exact steps. You will learn the basic command, advanced options, and common mistakes to avoid.

How To Copy An Entire Directory In Linux

The core command for copying a directory is cp -r source_directory destination_directory. The -r flag is essential. Without it, cp will refuse to copy a directory and show an error.

Here is a simple example. Suppose you have a directory named project and you want to copy it to a location called /backup. You would run:

cp -r project /backup

This command creates a copy of the project directory inside /backup. The new directory will be named /backup/project. If you want to copy the contents of project directly into /backup without creating a subdirectory, you can add a trailing slash to the source:

cp -r project/ /backup

But be careful: if /backup already exists, the behavior can vary. It is safer to specify the exact destination path.

Understanding The Recursive Flag

The -r flag is short for recursive. It tells cp to traverse the directory tree and copy every file and subdirectory. Without it, cp treats directories as special files and will not copy them.

There is also a -R flag, which is similar but preserves more details like device files and FIFO pipes. For most everyday use, -r works fine. If you are copying special files, use -R.

Preserving File Attributes With The -P Flag

When you copy a directory, you might want to keep the original file permissions, timestamps, and ownership. Use the -p flag to preserve these attributes. Combine it with -r:

cp -rp project /backup

This command copies the directory and retains the original modification times, access times, and file permissions. It is very useful for backups or when moving data between systems.

Verbose Output With The -V Flag

If you want to see what files are being copied, add the -v flag. This stands for verbose. It prints each file name as it is copied. This is helpful for large directories to monitor progress:

cp -rv project /backup

You will see output like:

'project' -> '/backup/project'
'project/file1.txt' -> '/backup/project/file1.txt'
'project/subdir' -> '/backup/project/subdir'
...

Copying With Interactive Mode -I Flag

Sometimes you may accidentally overwrite existing files. The -i flag makes cp ask for confirmation before overwriting. This is a safety net:

cp -ri project /backup

When a file with the same name exists in the destination, you will be prompted to type y or n.

Using The -U Flag For Updates

The -u flag copies only when the source file is newer than the destination file or when the destination file is missing. This is great for incremental backups:

cp -ru project /backup

It saves time by skipping files that have not changed.

Common Mistakes And How To Avoid Them

Many beginners forget the recursive flag. If you run cp project /backup without -r, you will get an error like cp: omitting directory 'project'. Always include -r or -R.

Another mistake is copying a directory into itself. For example, if you are inside project and try to copy it to a subdirectory, you can create an infinite loop. Linux usually prevents this, but it is good to be aware.

Also, be careful with trailing slashes. A trailing slash on the source means “copy the contents of the directory,” not the directory itself. This can lead to unexpected results if the destination already has files.

Copying Over SSH To Remote Servers

Sometimes you need to copy a directory to another machine. Use the scp command for secure copy over SSH. The syntax is similar:

scp -r project user@remote_host:/path/to/destination

This copies the entire directory recursively over an encrypted connection. You can also use rsync for more advanced features like resuming interrupted transfers.

Using Rsync For Efficient Directory Copying

Rsync is a powerful tool that copies only the differences between source and destination. It is ideal for large directories or frequent backups. The basic command:

rsync -av project/ /backup/

The -a flag stands for archive, which preserves permissions, timestamps, and copies recursively. The -v flag gives verbose output. Note the trailing slashes: they ensure the contents are copied into the destination directory.

To copy over a network:

rsync -av project/ user@remote_host:/backup/

Rsync is efficient because it checks file sizes and timestamps before copying. It only transfers changed parts.

Advanced Options For Specific Needs

Sometimes you need to copy only certain file types or exclude some files. Use the --include and --exclude patterns with cp or rsync.

For cp, you can use shell globbing. For example, to copy only .txt files from a directory:

cp -r project/*.txt /backup

But this does not copy subdirectories. A better approach is to use find with cp or rsync.

Excluding Files With Rsync

Rsync has a built-in exclude option. To copy a directory but skip all .log files:

rsync -av --exclude='*.log' project/ /backup/

You can also exclude multiple patterns by repeating the flag:

rsync -av --exclude='*.log' --exclude='temp/' project/ /backup/

Copying With Hard Links

If you want to save disk space, you can create a copy that uses hard links instead of duplicating data. The cp -l flag creates hard links to the original files. This is not a true copy because changes to one affect the other, but it can be useful for snapshots:

cp -rl project /backup

Use with caution.

Copying With Symbolic Links

If your directory contains symbolic links, you might want to copy them as links or follow them and copy the target files. By default, cp -r copies the links themselves. To follow links and copy the actual files, use -L:

cp -rL project /backup

This dereferences symbolic links.

Practical Examples For Daily Use

Let us look at some real-world scenarios.

Backing Up A Website Directory

Suppose you have a website in /var/www/html. To back it up to /backup/website with permissions preserved:

sudo cp -rp /var/www/html /backup/website

Use sudo if you need root access.

Copying A Home Directory To An External Drive

To copy your home folder to an external drive mounted at /mnt/backup:

cp -rv /home/username /mnt/backup

This will create /mnt/backup/username.

Copying A Project To A Shared Server

Using scp:

scp -r ~/project user@server:/shared/projects/

Enter your password when prompted.

FAQ: How To Copy An Entire Directory In Linux

What Is The Command To Copy A Folder In Linux?

The command is cp -r source destination. Replace source with the folder you want to copy and destination with the target location.

How Do I Copy A Directory With All Subdirectories?

Use the recursive flag -r or -R. For example: cp -r myfolder /backup copies the entire folder tree.

How Can I Copy A Directory And Keep Permissions?

Add the -p flag: cp -rp myfolder /backup. This preserves file permissions, ownership, and timestamps.

What Is The Difference Between Cp -R And Cp -R?

Both copy directories recursively. -R also preserves device files and FIFO pipes. For normal files and directories, they are identical.

How Do I Copy A Directory To A Remote Server?

Use scp -r or rsync -av. For example: scp -r myfolder user@server:/path.

Summary Of Key Points

To copy an entire directory in Linux, always use the cp command with the -r flag. Remember to preserve attributes with -p if needed. For remote copies, use scp or rsync. Avoid common mistakes like forgetting the recursive flag or misusing trailing slashes.

Practice these commands in a test directory first. Once you are comfortable, you can handle any directory copy task efficiently. The ability to copy directories is a fundamental skill for Linux users, and mastering it will save you time and prevent data loss.

Now you know how to copy an entire directory in linux. Use the right flags, check your paths, and you will never lose data again.