Linux copy one file to another directory

How can I Copy Files in a Folder into Another Folder on Linux Terminal

When you’re in need of copying the contents of a directory into another, in the case of Linux, there are plenty of tools at your disposal. Which one to use? It depends on the situation and personal preference. In this article, we’ll have a look at ways you can copy file contents from one folder to another in the terminal. It’ll feature some of the popular choices for different situations.

File copying in terminal

Copying the content of one directory into another is a very simple task. However, you have plenty of options of doing it. Linux is blessed with numerous tools to manipulate files and directories through various actions.

All the steps are demonstrated on Ubuntu 18.04.1. At first, let’s create a handful of dummy files to work with.

Now, let’s create the dummy files using touch.

Let’s create another folder within dir_A with a handful of dummy files.

Now, the entire structure of the directory dir_A looks like this.

Cp Command

The cp command is, by far, the most common tool for copying files. It’s a tool that comes by default with any Linux system. Copying files and directories are the sole purpose of cp. It supports a wide range of features.

This is the basic structure of the cp command.

For example, let’s make a copy of the dummy1 file with the name dummy1_copy.

Now, let’s try copying multiple files at once. For such an operation, cp requires the destination be a directory. Here, I’ll be using wildcard expression to grab all the dummy files and put them into the ~/Downloads directory.

How about copying a directory?

Here, we used two different flags. Let’s have a quick breakdown of them.

  • -r: Stands for a recursive copy (including any hidden file(s)). Must-have flag if copying involves an entire directory or more.
  • -v: Stands for verbose. The cp tool will output every single action it’s performing.

Want to follow the symbolic links? Add the “-L” flag.

Let’s say the destination directory already has a file with the same name. If you want to avoid overwriting, use the “-n” flag. It’ll ensure that in case of conflict, cp won’t overwrite the file.

If you’re performing a sensitive copy/paste action, it’s better to have the finest control over what happens, right? In such a case, use the “-i” flag. It stands for an interactive mode where cp will ask for confirmation every time a conflict arises.

Читайте также:  Tiny core linux in usb

If you’re more interested, the man page is always one of the best documentation. Learn more about cp.

mv command

The mv tool is similar to the cp tool. However, instead of copying, mv moves files and directories. Compared to cp, it’s a simpler one.

The command structure of mv is similar to cp.

To move the contents of dir_A to dir_A_copy, run the following command.

Here, the “-v” flag is for verbose mode. If you want to move just the contents of dir_A, use the following one instead. In this case, the target directory must exist beforehand.

The mv tool doesn’t have much to work with. For all the available options, check out the man page.

Rsync Command

This is one of the most popular and powerful tools for file copying, used mostly for backup. It holds the ability to copy files over a remote connection. Unlike cp and mv, it also comes with a ton of advanced file copying options that offer the utmost customization of its behavior.

Rsync is mostly famous for its delta-transfer algorithm that dramatically reduces the amount of data that needs to be exchanged. In the case of remote syncing, this saves up a lot of bandwidth.

Rsync doesn’t generally come pre-installed in any Linux distro. However, because of its popularity, it’s now available on all the major Linux distros. Use Pkgs.org to find out the rsync package for your distro. Assuming that you got rsync installed, let’s get started.

First, the basic command structure of rsync. It’s quite similar to cp.

Before getting started, here are a handful of popular rsync arguments.

  • -v: Verbose mode, outputs whatever action being performed on the console screen.
  • -vv: Verbose mode with more details
  • -r: Recursive mode, must be present if copying directories
  • -z: Compress data
  • -a: Archive data
  • -n: Dry run, should be run in combination with verbose mode. Simply outputs the actions if the command was to run for real
  • –delete: Delete the file(s) and directory(s) on the destination directory if the source directory doesn’t contain them any longer
  • -no-whole-file: Forces rsync to use its delta transmission algorithm, no matter what. Useful in scenarios where you must ensure the least amount of data write.
  • –remove-source-file: Delete source file(s) after copying.

Let’s check them out in action. First, the directory copy. Run the following command.

For further detailed output, let’s use the “-vv” flag.

Now, let’s check out the cool part of rsync. We discussed how rsync intelligently copies only the files that are modified, saving bandwidth and disk write. It’s time to check it out in action. In this example, the action will be performed within the dir_A directory.

Despite delta-transmission enabled, rsync may not apply it when you’re performing local data transfer.

Читайте также:  Linux восстановление файловой системы на флешке

Now, let’s check out the dry run feature. Before running any rsync command for real, it’s always important that you test it out beforehand. This is where the dry run comes in. Rsync will output what will happen if the command was to actually run.

Rsync can also work with remote directories, no problem. All it needs is a proper SSH connection with SSH keys configured.

Want to perform a move operation? To put that in rsync’s perspective, rsync will copy the contents of the source directory and then, delete the source content.

Check out the result of the operation.

Final thoughts

The cp and rsync are the most common tools you can use for copying files in a folder on the Linux terminal. Both of them are also really good for scripting. Just make sure to test the functions out before implementation.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

Источник

How to Copy All Files from a Directory to another Directory in Linux

Copying the file or folder means creating a new file having duplicate content as in the existing file.

Sometimes, we need to copy the files or folders rather than having a backup program. The files can be copied with the same name, or you can change the name as well.

Copying a file, folder, or directory is a simple and basic task in the Linux operating system. Rename, delete or copy commands are used as daily purpose operations while working with the command-line interface.

Although there are multiple commands to copying the files, the “cp” and “rsync” command are widely used simplest approaches.

How to Copy Files with “cp” Command in Linux:

The “cp” command is one of the commonly used commands to perform the copy operation. You can copy files or folders from source to destination, i-e, one directory through this command.

The syntax of the “cp” command is:

Let’s take a look at an example to understand the “cp” command tool better.

In the home directory, create a “temp” folder with the text file named “text_file1.txt” and add random content to it.

Copy a file with the same name:

To copy a “text_file1.txt” file directory with the same name, open the terminal and type the mentioned “cp” command with the right path.

Get the folder’s path by right-clicking on the file and navigate to the “Properties” option (it is the easy way to get the path link).

A dialogue box will open with the complete path of a text file:

Use this path with the “cp” command to copy file:

This command will copy the “text_file1.txt” file to the “temp2” folder.

To verify it, type the “ls” command in the terminal:

Читайте также:  Графическое окружение linux lxde

Copy a file with a different name:

To copy the file in the current working directory with the different name, type the following “cp” command with file location:

Verify it using the “ls” command”:

Copy Multiple Files with “cp” Command:

To copy multiple files with the “cp” command, navigate the terminal to the directory where files are saved and then run the “cp” command with the file names you want to copy and the destination path.

$ cp text_file1.txt text_file2.txt text_file3.txt / home / wardah / temp2

Run the mentioned command to verify if files are copied successfully:

Above mentioned scenarios are how to copy a single or selected file in a directory. Now, use the wildcard character (*) to copy present files of one directory to any other specific directory.

Run the “ls” command to check how many files exist in the temp directory:

Instead of mentioning all file names in the terminal, use the wildcard (*) with the directory path to copy all the files into destination:

Now, run the “ls” command again to check if all files are copied in the “temp2” directory:

How to Copy files with the “rsync” command in Linux:

The “rsync” command is another versatile Linux tool to synchronize and copy files and directories locally as well as remotely.

The syntax of the “rsync” command is to copy files is:

It is a pre-built tool in many Linux distribution. However, if you don’t get it on your system, install it by executing the following command:

To copy a file from one place to another, run the following command:

To copy all the directory files to another location, the command would be:

(The “-a” with the “rsync” command is used to copy directories recursively)

Here are the two concepts:

If you add a trailing slash (/) with the path, it will copy the content of the source directory to the destination directory, just like shown in the image:

But, if you don’t add it, it will copy the source directory inside the destination directory, like:

The above command will copy a “dir1” directory to the “dir2” directory.

Conclusion:

Copying a file or directory is the basic command one can operate. One can use it multiple times while using Linux operating system.

This guide has seen the two simplest approaches, the “cp” command and the “rsync” command. Using these commands, we have learned how to copy a single file, multiple files, and even copy one directory to another.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Источник

Оцените статью
Adblock
detector