Home Learn Linux Copying all files and folders to another directory in Linux

Copying all files and folders to another directory in Linux

We'll walk you through the step-by-step process of copying all files from one directory to another using the cp command. We'll also provide examples to help you understand how to use the command in different scenarios.

by Divya Kiran Kumar
copying files and folders in linux

Copying files from one directory to another is a common task in Linux. Whether you’re backing up important data, moving files to a new location, or simply making a copy for safekeeping, it’s important to know how to copy files in Linux. Fortunately, the process is relatively straightforward, and you can use a few different methods.

In this guide, we’ll focus on copying all files from one directory to another using the cp command. The cp command is a simple yet powerful tool that allows you to copy files and directories in Linux. It’s an essential command that every Linux user should know how to use.

Why copy files from the terminal instead of GUI copy and paste

While copying and pasting files using the graphical user interface (GUI) in Linux is possible, there are several reasons why you might want to use the terminal instead. Here are a few advantages:

  • Speed: Copying files using the command line is often faster than using the GUI. The terminal is optimized for typing commands quickly and efficiently, and you can copy and paste multiple files at once using the cp command.
  • Efficiency: The terminal allows you to automate repetitive tasks using scripts, saving you much time and effort in the long run. For example, if you need to copy files from one directory to another regularly, you can create a script that does it automatically.
  • Flexibility: The terminal gives you more control over the copying process than the GUI. You can specify precisely which files you want to copy, and you can overwrite or skip files already in the destination directory.
  • No GUI required: Not all Linux systems have a GUI, so knowing how to copy files from the terminal is essential for using Linux in these environments.

So, while the GUI may be more user-friendly for beginners, using the terminal can be a more efficient and flexible option for copying files in Linux.

Copying all files and folders to another directory in Linux

Here are the steps to follow:

Step 1: Open the Terminal

Open the Terminal application on your Linux system. You can do this by pressing Ctrl + Alt + T or by searching for Terminal in the Applications menu.

Step 2: Navigate to the directory containing files

Use the cd command to navigate to the directory containing the files you want to copy. For example, if the files are located in the Downloads folder, you can type:

cd ~/Downloads

In Linux and Unix-like operating systems, the tilde character (~) is a shorthand notation representing the current user’s home directory. For example, if the username is “john”, the tilde character (~) would represent the path /home/john/.

Step 3: Create a destination directory

Create a new directory where you want to copy the files. You can do this using the mkdir command. For example, if you want to create a new directory called “Backups” in the current user’s home directory, you can type:

mkdir ~/Backups
creating a directory

Creating a directory

Step 4: Copy all files to the destination directory

Now that you’re in the directory containing the files you want to copy and created the destination directory, you can use the cp command to copy all the files to the destination directory. Here’s the basic syntax:

cp * /path/to/destination/directory

In this command, the * wildcard symbol tells the cp command to copy all files in the current directory. Replace /path/to/destination/directory with the actual path to the destination directory you created in Step 3.

If you’re trying to copy a directory using the cp command and you receive an error message that says “-r not specified; omitting directory”, it means that you need to use the -r (or –recursive) option to copy the entire directory and its contents.

Here’s an example of how to use the cp command with the -r option to copy a directory:

cp -r /path/to/source/directory /path/to/destination/directory

Replace “/path/to/source/directory” with the actual path to the source directory you want to copy, and “/path/to/destination/directory” with the actual path to the destination directory where you want to copy the files.

The -r option tells the cp command to copy directories recursively, which means it will copy all files and subdirectories within the specified directory.

Once you’ve added the -r option to your cp command, try rerunning the command, and it should copy the directory and its contents successfully.

For example, if you want to copy all files and folders in the Downloads folder to the backup directory you created in your home directory, you can type:

cp -r ~/Downloads ~/Backups
copying files and folders using cp command

Copying Files and Folders using the CP command

Step 5: Verify the files have been copied

Use the ls command to list the contents of the destination directory and verify that all the files have been copied correctly. For example, you can type:

ls ~/Backups
listing files using ls command

Listing files using the ls command

This should list all the files copied from the Documents folder to the backup directory.

Helpful tips and tricks

Here are some tips and tricks that can help you copy files more efficiently using the terminal:

  • Use wildcards: The cp command supports wildcards, allowing you to copy multiple files simultaneously. For example, you can use the * wildcard to copy all files in a directory or use *.txt to copy all files with a .txt extension.
  • Use the -r option for directories: If you’re copying a directory, you need to use the -r option to copy all files and subdirectories within it. For example, cp -r /path/to/source/directory /path/to/destination/directory.
  • Use the -v option for verbose output: The -v option tells the cp command to display verbose output, which can help track the progress of a copy operation. For example, cp -v /path/to/source/directory/* /path/to/destination/directory.
  • Use the rsync command for remote copies: If you need to copy files between two remote systems, the rsync command is better than the cp. It’s designed for efficient remote file transfers and can resume interrupted transfers.
  • Double-check before overwriting: When copying files; it’s essential not to overwrite any critical data. Always double-check the destination directory before copying files, and use the -i option to prompt before overwriting any existing files. For example, cp -i /path/to/source/directory/* /path/to/destination/directory.

By following these tips and tricks, you can copy files more efficiently and avoid potential mistakes.

Conclusion

Copying files from one directory to another is a common task in Linux, and the cp command is an essential tool for doing so. Following the steps outlined in this guide, you can copy all files from one directory to another using the cp command in the terminal.

While the graphical user interface (GUI) may be more user-friendly for beginners, the terminal can be a more efficient and flexible option for copying files in Linux. The terminal allows you to automate repetitive tasks using scripts and gives you more control over the copying process.

In addition, we discussed the advantages of using the cp command over the GUI for copying files, including speed, efficiency, and flexibility. We also provided some helpful tips and tricks, such as using wildcards, the -r option for directories, the -v option for verbose output, and the rsync command for remote copies.

Knowing how to copy files from the terminal is an essential skill for anyone using Linux. With the knowledge and skills gained from this guide, you’ll be able to copy files more efficiently and effectively and take full advantage of the power and flexibility of the Linux terminal.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.