Copy one folder to another folder linux

How can I copy the contents of a folder to another folder in a different directory using terminal?

I am trying to copy the contents of a folder to another folder in a different directory using terminal. Would somebody be able to provide me an example of the command line syntax required to achieve this?

8 Answers 8

You can copy the content of a folder /source to another existing folder /dest with the command

The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.

The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.

@DylanValade: -a already implies —preserve=all , that is wider than -p = —preserve=mode,ownership,timestamps .

@BennyNeugebauer: scp is used to copy over a network (through ssh ) and only encrypts the communication channel, not the files on the destination filesystem.

«The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.», this is not related to cp, but related to bash. The dot means «this location» and avoids the use of bash globbing, where by default files/directories starting with a . are not expanded.

Can someone explain the need behind use of mypath/. instead of the existing bash wildcard expansion character, mypath/* ?

rsync -a source/ destination 

The advantages of rsync are:

  1. After the initial sync, it will then copy only the files that have changed.
  2. You can use it over a network, convenient for files in $HOME, especially config files.

This will not copy hidden files, since bash expands * only to non-hidden file. The solution by @Joschua is safer.

If it’s a big folder you may wish to use one of these options to view progress while it’s copying askubuntu.com/questions/609303/…

Lets say you have a folder called folder1 in your ~ , inside folder1 is 1 file called file1 and 2 folders called sub1 and sub2 each with other files and folders inside them.

To copy all the contents of ~/folder1 to ~/new_folder1 you would use

new_folder1 would then contain all the files and folders from folder1 .

cp is the command to copy using a terminal, -r makes it recursively (so, current directory + further directories inside current) ~/folder1 is the origin folder, ~/new_folder1 is the destination folder for the files/folders inside the origin.

Thank you Bruno! It helped me to understand the syntax, though I had to change it a bit(removing ~ sign). Maybe because the destination folder was in /opt, which resides in another file system. And thank you Portablejim to remember the hidden file thing!

Читайте также:  Линукс архив tar gz установить

The trailing period is important. Without it, sometimes it may create a new subdirectory ~/new_folder1/folder1 instead of copying the contents over.

@Alex78191 [root@ home]# mkdir food [root@ home]# cd food/ [root@ food]# mkdir .fruit [root@ food]# mkdir veggies [root@ food]# touch veggies/carrots [root@ food]# touch .fruit/apple [root@ food]# ls * carrots [root@ food]#

Simple example.

Copy the directory dir_1 and its contents (files) into directory dir_2:

cp -r ./dir_1 ./dir_2 # or cp -r ./dir_1/ ./dir_2/ # Results in: ./dir_2/dir_1/_files_ 

Copy only the contents (files) of dir_1 into directory dir_2:

cp -r ./dir_1/. ./dir_2 # or cp -r ./dir_1/. ./dir_2/ # Results in: ./dir_2/_files_ 

_files_ is a placeholder for the actual files located in the directory.

Check this http://www.cyberciti.biz/faq/copy-folder-linux-command-line/ for more information on copying folder. Hope this helps.

cp is a Linux command for copying files and directories. The syntax is as follows:

cp source destination cp dir1 dir2 cp -option source destination cp -option1 -option2 source destination 

In this example copy /home/vivek/letters folder and all its files to /usb/backup directory:

cp -avr /home/vivek/letters /usb/backup 

-a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.

-v : Explain what is being done.

-r : Copy directories recursively. Example

Copy a folder called /tmp/conf to /tmp/backup:

This code with Flag «-R» copies perfectly all the contents of «folder1» to existing «folder2»:

Flag «-R» copies symbolic links as well but Flag «-r» skips symbolic links so Flag «-R» is better than Flag «-r».

-R, --dereference-recursive For each directory operand, read and process all files in that directory, recursively, following all symbolic links. 
-r, --recursive For each directory operand, read and process all files in that directory, recursively. Follow symbolic links on the command line, but skip symlinks that are encountered recursively. Note that if no file operand is given, grep searches the working directory. This is the same as the ‘--directories=recurse’ option. 

Источник

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.

Читайте также:  Install python on linux command line

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.

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.

Читайте также:  Run ssh server linux

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.

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.

Источник

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