How To Copy A Directory To Another Directory In Linux – Copying Directories With Subfolders

Outlook distribution lists can be duplicated by saving the list as a contact group and then importing it into a new folder. But if you are working in Linux, you need a different approach to copy directories. This guide covers everything about how to copy a directory to another directory in linux, from basic commands to advanced options.

Copying directories is a common task for Linux users. Whether you are backing up files, moving projects, or setting up servers, knowing the right command saves time. The main tool for this is the cp command, but there are other methods too. Let us start with the basics and work up to more complex scenarios.

Understanding The Linux Copy Command

The cp command is short for “copy.” It is built into every Linux distribution. To copy a directory, you must use the -r or -R flag, which stands for “recursive.” This tells the system to copy the directory and everything inside it, including subdirectories and files.

Without the recursive flag, cp will give you an error. It only works on single files by default. So remember: for directories, always include -r.

Basic Syntax For Copying A Directory

The general syntax is:

cp -r source_directory destination_directory

Here, source_directory is the folder you want to copy, and destination_directory is where you want it to go. If the destination does not exist, Linux creates it. If it exists, the source directory is placed inside it.

For example, to copy a folder named “projects” into a folder named “backups,” you would run:

cp -r projects backups

This creates a new folder called “projects” inside “backups.” The original “projects” folder stays unchanged.

How To Copy A Directory To Another Directory In Linux

Now we get to the exact keyword: How To Copy A Directory To Another Directory In Linux. This section explains the process step by step. You will learn the most common method and some variations.

Step 1: Open The Terminal

First, open your terminal emulator. You can find it in your applications menu or use a shortcut like Ctrl+Alt+T. The terminal is where you type all commands.

Step 2: Navigate To The Source Directory’s Parent

Use the cd command to move to the folder that contains the directory you want to copy. For instance, if your source directory is in /home/user/docs, type:

cd /home/user/docs

But you do not have to be in the same folder. You can use full paths instead.

Step 3: Run The Copy Command

Type the cp -r command followed by the source and destination paths. For example:

cp -r my_folder /home/user/backups

This copies “my_folder” into the “backups” directory. If “backups” does not exist, it will be created.

Step 4: Verify The Copy

After the command runs, check that the copy succeeded. Use the ls command to list the contents of the destination:

ls /home/user/backups

You should see the copied folder listed. You can also use tree if installed to see the full structure.

Common Options For The Cp Command

The cp command has many options to control how copying works. Here are the most useful ones for directories:

  • -r or -R: Recursive copy. Required for directories.
  • -i: Interactive mode. Asks before overwriting files.
  • -u: Update mode. Copies only when the source is newer or the destination is missing.
  • -v: Verbose mode. Shows each file as it is copied.
  • -p: Preserves file attributes like timestamps and permissions.
  • -a: Archive mode. Combines -r, -p, and other options to preserve everything.

Using The Interactive Option

If you want to avoid accidental overwrites, use the -i flag:

cp -ri source destination

This will prompt you before overwriting any existing files. Type “y” to confirm or “n” to skip.

Using The Verbose Option

To see what is happening, add -v:

cp -rv source destination

This prints each file name as it is copied. It is helpful for large directories to monitor progress.

Using The Archive Option

The -a flag is the most comprehensive. It copies recursively and preserves permissions, timestamps, symbolic links, and other attributes:

cp -a source destination

This is ideal for backups because it keeps everything intact.

Copying A Directory With Rsync

Another powerful tool is rsync. It is often used for syncing files, but it can also copy directories. Rsync is more efficient for large or repeated copies because it only transfers differences.

Basic Rsync Syntax

The syntax is similar to cp:

rsync -a source destination

The -a flag stands for archive mode, which preserves attributes. To copy a directory, you usually include a trailing slash on the source to control behavior.

Example With Rsync

To copy the folder “data” into “backups,” run:

rsync -a data backups

This creates “backups/data.” If you add a trailing slash to the source like data/, it copies the contents of “data” into “backups” without creating a subfolder.

Advantages Of Rsync

  • Faster for large directories after the first copy
  • Can copy over SSH to remote servers
  • Shows progress with --progress
  • Handles interruptions gracefully

Copying A Directory With Tar And Pipe

For advanced users, you can use tar combined with a pipe to copy directories. This method preserves permissions and can be faster in some cases.

How It Works

You create a tar archive of the source and extract it at the destination in one command:

tar cf - source | tar xf - -C destination

Here, tar cf - source creates an archive and sends it to stdout. The pipe sends it to the second tar, which extracts it in the destination folder.

Example

To copy “myfolder” into “/tmp/backup,” run:

tar cf - myfolder | tar xf - -C /tmp/backup

This is useful when you need to copy across file systems or preserve special files.

Copying A Directory With Wildcards

Sometimes you want to copy only specific files or folders. Wildcards help you select items by pattern.

Using Asterisk

To copy all directories starting with “proj,” use:

cp -r proj* /destination

This copies every folder whose name begins with “proj.”

Using Question Mark

The question mark matches a single character. For example, cp -r file? /dest copies “file1,” “file2,” etc., but not “file10.”

Using Brackets

Brackets match a set of characters. cp -r [abc]* /dest copies folders starting with a, b, or c.

Common Mistakes And How To Avoid Them

Copying directories can go wrong. Here are frequent errors and fixes:

  • Forgetting the -r flag: You get an error like “cp: omitting directory.” Always add -r.
  • Overwriting files accidentally: Use -i to prompt before overwriting.
  • Copying into itself: If you try to copy a directory into itself, you get an infinite loop. Linux prevents this, but it can be confusing.
  • Permissions issues: Use sudo if you need to copy system directories. For example: sudo cp -r /etc /backup.
  • Trailing slashes: A trailing slash on the source changes behavior. cp -r source/ dest copies the contents of source into dest, not the folder itself.

Copying Directories Across Different File Systems

When copying between file systems, like from ext4 to NTFS, you might lose attributes. Use cp -a or rsync -a to preserve as much as possible. However, some attributes like permissions may not transfer to non-Linux file systems.

Example With External Drive

To copy to a mounted USB drive:

cp -r /home/user/docs /mnt/usb

Make sure the drive is mounted first. Use mount or lsblk to check.

Copying Directories Over SSH

For remote servers, you can use scp or rsync over SSH. This is essential for managing cloud or remote machines.

Using Scp

The scp command copies files over SSH. For directories, use -r:

scp -r /local/folder user@remote:/remote/path

This copies the folder to the remote server. You will need the remote user’s password or SSH key.

Using Rsync Over SSH

Rsync works over SSH by default if you specify the remote path:

rsync -a /local/folder user@remote:/remote/path

This is faster for large transfers because it only sends changes.

Copying Directories With Progress Indicators

For large copies, you want to see progress. Both cp and rsync have options.

With Cp

Use cp -rv to see each file. But this can be noisy. A better option is to use pv (pipe viewer) with tar:

tar cf - source | pv | tar xf - -C destination

This shows a progress bar and speed.

With Rsync

Add --progress to see per-file progress:

rsync -a --progress source destination

This shows each file’s transfer rate and percentage.

Automating Directory Copies With Scripts

If you copy directories often, write a shell script. This saves time and reduces errors.

Simple Backup Script

Create a file called backup.sh with:

#!/bin/bash
cp -r /home/user/documents /home/user/backup/$(date +%Y%m%d)

This copies the documents folder into a dated backup folder. Make it executable with chmod +x backup.sh and run it with ./backup.sh.

Using Cron For Scheduled Copies

To run the script daily, add it to cron. Edit your crontab with crontab -e and add:

0 2 * * * /path/to/backup.sh

This runs the script at 2 AM every day.

Troubleshooting Common Issues

Here are solutions to problems you might face:

  • Permission denied: Use sudo or change permissions with chmod.
  • Disk full: Check space with df -h. Free up space or use a different destination.
  • Symbolic links broken: Use cp -a to preserve links. Otherwise, they may point to wrong paths.
  • Copying hidden files: Hidden files (starting with dot) are included by default with cp -r. No special flag needed.
  • Large directories taking too long: Use rsync with compression: rsync -az source destination.

Frequently Asked Questions

What Is The Command To Copy A Directory In Linux?

The command is cp -r source destination. The -r flag makes it recursive, so it copies the directory and all its contents.

How Do I Copy A Directory And Its Contents To Another Location?

Use cp -r /path/to/source /path/to/destination. This copies the entire directory tree. Add -v to see the files being copied.

Can I Copy A Directory Without Using The -R Flag?

No, the -r flag is required for directories. Without it, cp will refuse to copy and show an error message.

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

cp -r copies recursively but may not preserve all attributes. cp -a (archive mode) preserves permissions, timestamps, symbolic links, and other metadata. Use -a for backups.

How Do I Copy A Directory Over SSH?

Use scp -r /local/folder user@remote:/remote/path or rsync -a /local/folder user@remote:/remote/path. Both work over SSH and are secure.

Final Tips For Copying Directories In Linux

Practice these commands in a test environment first. Create dummy directories with mkdir and files with touch to experiment. This builds confidence.

Always double-check your paths. A small typo can copy files to the wrong place. Use tab completion to avoid mistakes.

For critical data, make multiple copies. Use different methods or destinations to ensure redundancy. Remember that cp does not delete the source, so it is safe for duplication.

Now you know how to copy a directory to another directory in linux using various tools. Whether you prefer cp, rsync, or tar, you have the skills to handle any copy task. Start using these commands today to manage your files efficiently.