Linux copy file to another folder

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.

Читайте также:  Linux motion stop motion

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.

Читайте также:  Госуслуги плагин firefox linux

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

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.

Источник

The Linux cp Command – How to Copy Files in Linux

Dillion Megida

Dillion Megida

The Linux cp Command – How to Copy Files in Linux

There are a couple different ways to copy and paste content when you’re working on your computer.

If you spend more time in the user interface of your device, you’ll probably use your mouse to do this. You can copy files by right-clicking on the file and selecting «Copy», then going to a different directory and selecting «Paste».

For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the cp command.

In this article, I’ll explain what the cp command is and show you how to copy and paste files and directories in Linux using the terminal.

What is the cp command?

You use the cp command for copying files from one location to another. This command can also copy directories (folders).

The syntax of this command is:

cp [. file/directory-sources] [destination] 

[file/directory-sources] specifies the sources of the files or directories you want to copy. And the [destination] argument specifies the location you want to copy the file to.

To understand the rest of this article, I will use this folder structure example. Let’s say a directory called DirectoryA has two directories in it: DirectoryA_1 and DirectoryA_2. These subdirectories have many files and sub directories in them.

I’ll also assume you’re currently in the DirectoryA location in the terminal, so if you aren’t, make sure you are:

How to copy files with the cp command

If you want to copy a file, say README.txt from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:

cp ./DirectoryA_1/README.txt ./DirectoryA_2 # ./DirectoryA_1/README.txt is the source file # ./DirectoryA_2 is the destination 

If you want to copy more than a file from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:

cp ./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2 

As you can see, you will put all the source files first, and the last argument will be the destination.

Читайте также:  Linux mint hdmi телевизор

How to copy directories with the cp command

By default, the cp command works with files. So if you attempt to copy a directory like this:

cp ./DirectoryA_1/Folder/ ./DirectoryA_2 

You will get an error stating:

./DirectoryA_1/Folder/ is a directory

To copy directories, you have to pass the -r flag. This flag informs the cp command to recursively copy a directory and its contents (which could be files or other sub directories). So for the previous command, you can add the flag before the directory sources like this:

cp -r ./DirectoryA_1/Folder/ ./DirectoryA_2 

This command will recursively copy the Folder directory in ./DirectoryA_1/ as well as all files and directories in the Folder directory.

How to copy files that match a glob pattern

A glob pattern is similar to Regex, which allows you to match multiple files with names that match a specific pattern. Learn more about the difference here: Regex vs Glob patterns.

For example, if you want to copy all files in DirectoryA_1 with the .txt extension, you can execute this command:

cp ./DirectoryA_1/*.txt ./DirectoryA_2 

./DirectoryA_1/*.txt matches files with the .txt extension in their names, and the cp command can copy all those files to the destination.

You can check out the glob documentation to learn more about globbing patterns and characters you can use.

Now you know how to copy files (and directories) right from the command line. Thanks for reading!

Dillion Megida

Dillion Megida

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

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