Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

I have a directory with the following structure:

file_1
file_2
dir_1
dir_2
# etc.
new_subdir

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?

like image 763
GSto Avatar asked Mar 23 '12 16:03

GSto


People also ask

How do I copy a directory to a subfolder in Linux?

In order to copy a directory on Linux, you have to execute the “cp” command with the “-R” option for recursive and specify the source and destination directories to be copied.

How copy all files in directory Linux?

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.

How do I copy multiple files from one directory to another in Linux?

To copy multiple files you can use wildcards (cp *. extension) having same pattern. Syntax: cp *.

How do I copy an entire directory to a destination folder?

In Windows 10 and earlier versions, right-click the folder and select Copy, or click Edit and then Copy. Navigate to the location where you want to place the folder and all its contents. icon on the menu bar. Alternatively, right-click the folder, select Show more options and then Paste.


2 Answers

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

like image 98
Eihiko Avatar answered Sep 21 '22 12:09

Eihiko


You mean like

cp -R * new_subdir

?

cp take -R as argument which means recursive (so, copy also directories), * means all files (and directories).

Although * includes new_subdir itself, but cp detects this case and ignores new_subdir (so it doesn't copy it into itself!)

like image 36
Shahbaz Avatar answered Sep 20 '22 12:09

Shahbaz