# Copying All Files from One Directory to Another in Linux


As a Linux user, you may often find yourself needing to copy multiple or all files from one directory to another. Whether you are organizing project files or just tidying up your filesystem, this is a common task. Fortunately, it's also very easy to do in Linux.

## Why Copy Files Between Directories

There are a few common scenarios where copying all files from one directory to another comes in handy:

- Consolidating files scattered across various folders into one location for easier access or archiving.
- Backing up important data files by replicating them in a separate directory.
- Moving files associated with an application or project to a dedicated folder for organization.
- Migrating files to a new hard drive or a newly set up system.

Copying all files manually can be time-consuming and tedious. Luckily, Linux offers simple command line methods to copy everything from one folder to another in just a single step.

## The Basics of `cp`

On Linux, the `cp` command is used to copy files and directories. Its basic syntax is:

```plaintext
cp [options] source destination
```

- `source` is the original file or directory.
- `destination` is the copied file or directory.

Some useful `cp` options include:

- `-R` copies directories recursively, meaning it includes all sub-directories and files.
- `-r` same as `-R` above.
- `-v` gives verbose output while copying.

So in its simplest form, this command copies a single file:

```plaintext
cp myfile.txt /home/user/documents
```

This copies a directory recursively to include all contents:

```plaintext
cp -R myfolder /backup/
```

Next, let's see how to copy _everything_ inside a directory.

## Copy All Files in a Directory

### Using Wildcards

An easy way is to use wildcards - special characters that match multiple files or directories.

The asterisk `*` matches everything in the given directory. So this command copies all files and subdirectories from one folder to another:

```plaintext
cp -R /source/* /destination/
```

Let's break this down:

- `/source/` is the source directory containing the files you want to copy.
- `*` wildcard matches ALL contents in source.
- `/destination/` is the target directory to copy the files to.
- `-R` makes sure even the subdirectories themselves get copied.

As you can see, using `*` lets you copy everything inside in one quick step!

### An Alternative: `rsync`

Another option is `rsync`, a fast and flexible command for syncing files. Its syntax is:

```plaintext
rsync [options] source destination
```

To recursively copy all contents from one folder to another, use:

```plaintext
rsync -r /source/ /destination/
```

Here, `-r` syncs the directories recursively.

`rsync` is especially faster for large transfers thanks to its delta-transfer algorithm. It's also resilient to interruptions - you can resume an interrupted sync with the same command.

## Handling Destination Folder Conflicts

When copying entire directories, there can be name conflicts if a folder with the same name already exists in the target destination.

To handle this, use the `-u` (update only) flag. This will _skip_ copying any files/folders that already exist in the destination:

```plaintext
rsync -r -u /source/ /destination/
```

Or with `cp`:

```plaintext
cp -u -R /source/* /destination/
```

Now only the missing files/folders in the destination are added, without overwriting existing ones.

You can also use `-n` for a trial run to preview what would happen, without actually copying anything:

```plaintext
cp -R -n /source/* /destination/
```

> Also read - [**Understanding Linux Bash For Loops**](https://developnsolve.com/understanding-linux-bash-for-loops)

## Final Words

Whether consolidating your personal documents or migrating an application's files, copying entire directories is a snap on Linux.

With just basic wildcard shortcuts or utilities like `rsync`, you can copy all files from one folder to another with a single terminal command. No tedious clicking and dragging is required.

Over time, being able to efficiently manage files across folders makes using Linux a breeze. As you grow more fluent with these core commands, tasks like backups and migrations will become second nature.

