Copy all files in one directory to another 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.

Читайте также:  Linux run command as system user

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!

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 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.

Читайте также:  Linux горячая клавиша вставить

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:

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:

Читайте также:  Linux file names with spaces

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.

Источник

Copy all files in a directory to a local subdirectory in linux

I’d like to make a copy of all the existing files and directories located in this directory in new_subdir . How can I accomplish this via the linux terminal?

3 Answers 3

This is an old question, but none of the answers seem to work (they cause the destination folder to be copied recursively into itself), so I figured I’d offer up some working examples:

Copy via find -exec:

find . ! -regex ‘.*/new_subdir’ ! -regex ‘.’ -exec cp -r ‘<>‘ new_subdir \;

This code uses regex to find all files and directories (in the current directory) which are not new_subdir and copies them into new_subdir. The ! -regex ‘.’ bit is in there to keep the current directory itself from being included. Using find is the most powerful technique I know, but it’s long-winded and a bit confusing at times.

Copy with extglob:

cp -r !(new_subdir) new_subdir

If you have extglob enabled for your bash terminal (which is probably the case), then you can use ! to copy all things in the current directory which are not new_subdir into new_subdir.

Copy without extglob:

mv * new_subdir ; cp -r new_subdir/* .

If you don’t have extglob and find doesn’t appeal to you and you really want to do something hacky, you can move all of the files into the subdirectory, then recursively copy them back to the original directory. Unlike cp which copies the destination folder into itself, mv just throws an error when it tries to move the destination folder inside of itself. (But it successfully moves every other file and folder.)

Источник

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