Linux Tip: How to Copy Directories and Subdirectories in Linux

Linux Tip: How to Copy Directories and Subdirectories in Linux
Photo by Elena Mozhvilo / Unsplash

Copying files and directories in Linux is a common task for beginners, but understanding how to do it properly can save you a lot of time and prevent errors. In this guide, we’ll explain how to copy an entire directory, including its subdirectories and files, from one location to another. We’ll break down each part of the command and give you some tips along the way.

Imagine you have two directories:

  • /home/user/project-alpha: This is the source directory where all your project files are stored.
  • /home/user/project-beta: This is the destination directory where you want to copy all the files from project-alpha.

Let’s see how you can copy the contents of one directory to another!

The Basic Copy Command: cp

Linux provides the cp command for copying files and directories. The command is simple but has many options that are very useful when copying large directories with subfolders. For our example, let’s assume you want to copy everything from project-alpha to project-beta.

Step-by-Step Guide:

  1. Using the -r Option for Recursive CopyingWhen you copy a directory, you want to make sure that all files and subdirectories inside it are copied. The -r (or --recursive) option ensures this by recursively copying all the contents.The basic structure of the command is:
cp -r /home/user/project-alpha /home/user/project-beta

In this command:

  • cp is the copy command.
  • -r tells Linux to copy the entire directory, including subdirectories.
  • /home/user/project-alpha is the source directory (the directory you are copying from).
  • /home/user/project-beta is the destination directory (the directory you are copying to).

After running this command, project-beta will contain a copy of everything from project-alpha.

  1. Copying Contents Without Creating a New Subdirectory

Sometimes, you want to copy just the contents of the source directory into the destination, not the directory itself. If you don’t want to end up with /home/user/project-beta/project-alpha/, but instead have the contents of project-alpha copied directly into project-beta, you can use this variation:

cp -r /home/user/project-alpha/* /home/user/project-beta/

This command copies everything inside the project-alpha directory directly into project-beta, without creating an extra nested directory.

Tips for Beginners:

  • Check for Spaces in Directory Names: If your directory names contain spaces (e.g., /home/user/my project), you’ll need to enclose the paths in quotation marks:
cp -r "/home/user/my project/" "/home/user/backup project/"
  • Double-Check Destination: Always double-check your destination path before running the command to avoid overwriting important data by mistake.
  • Add Verbosity with -v: If you want to see what files are being copied, add the -v (verbose) option to your command:
cp -rv /home/user/project-alpha /home/user/project-beta

This will display each file and directory as it’s copied, which can be helpful for large or complex operations.

Finally

Copying directories and their contents in Linux is an essential skill for beginners. Using the cp command with the -r option allows you to safely and efficiently copy files and subdirectories. With this knowledge, you can back up projects, move files between directories, and organize your Linux system with ease!

Hope it helps.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe