Copy all files to one folder linux

How to move all files including hidden files into parent directory via *

Its must be a popular question but I could not find an answer. How to move all files via * including hidden files as well to parent directory like this:

This will move all files to parent directory like expected but will not move hidden files. How to do that?

this question has a duplicate at SU, with an even more correct answer (not the accepted one though): cp -r /path/to/source/. /destination

9 Answers 9

You can find a comprehensive set of solutions on this in UNIX & Linux’s answer to How do you move all files (including hidden) from one directory to another?. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.

You can use these two commands together:

mv /path/subfolder/* /path/ # your current approach mv /path/subfolder/.* /path/ # this one for hidden files 
mv /path/subfolder/* /path/subfolder/.* /path/ 

(example: echo ab expands to a.b ab )

Note this will show a couple of warnings:

mv: cannot move ‘/path/subfolder/.’ to /path/.’: Device or resource busy mv: cannot remove /path/subfolder/..’: Is a directory 

Just ignore them: this happens because /path/subfolder/* also expands to /path/subfolder/. and /path/subfolder/.. , which are the directory and the parent directory (See What do “.” and “..” mean when in a folder?).

If you want to just copy, you can use a mere:

cp -r /path/subfolder/. /path/ # ^ # note the dot! 

This will copy all files, both normal and hidden ones, since /path/subfolder/. expands to «everything from this directory» (Source: How to copy with cp to include hidden files and hidden directories and their contents?)

The braces are just a short cut for mv /path/subfolder/* /path/subfolder/.* /path/ , not strictly necessary to combine the two commands into one.

I get the following error: mv: overwrite `/path/.’? y mv: cannot move `/path/subfolder/.’ to `/path/.’: Device or resource busy mv: overwrite `/path/..’? y mv: cannot move `/path/subfolder/..’ to `/path/..’: Device or resource busy

@Dejan Just ignore it. . denotes current directory and .. denotes up directory. You must have noticed that all other files are moved.

«Just ignore the warning» may not always be a good idea. Right now I’m having a problem with a script in which I need to stop execution if any step fails — since this solution always causes an error, it kills my script. I need a way to determine if the mv command failed or not.

* works fine as a pattern to match hidden and unhidden files excluding . and .. but always returns itself as well even if there are some matching files. This seems to be a bug and can be worked around by setting nullglob ( shopt -s nullglob ). But if one does that one could set dotglob instead which seems favorable to me. Either should probably only be enabled temporarily.

Читайте также:  Kali linux post install

I think this is the most elegant, as it also does not try to move .. :

mv /source/path/* /destination/path 

this would miss files like ..anything or . anything etc. — stackoverflow.com/a/31438355/2351568 contains the correct regex for this problem. || but anyway using shopt -s dotglob is still the better solution!

@DylanB don’t memorize it. remember that it matches whatever is in the curlybrackets, separated by commas. * would find all files starting with a or b such as «anatomy» and «bulldozer». The second match is just an empty match, equivalent to * , and the first match is equivalent to .[!.] , where the group [!.] means a group NOT starting with a . . This means .* but not ..* .

This will move all files to parent directory like expected but will not move hidden files. How to do that?

You could turn on dotglob :

shopt -s dotglob # This would cause mv below to match hidden files mv /path/subfolder/* /path/ 

In order to turn off dotglob , you’d need to say:

Very helpful. Wanted to find out more but shopt is a builtin so man shopt doesn’t work and help shopt is very brief. But you can do bashman () < man bash | less -p "^ $1 "; >and then bashman shopt to read all about it straightforwardly. (Might have to hit n to jump down to the command if there are lines starting with shopt, as I found.)

By using the find command in conjunction with the mv command, you can prevent the mv command from trying to move directories (e.g. .. and . ) and subdirectories. Here’s one option:

find /path/subfolder -maxdepth 1 -type f -name '*' -exec mv -n <> /path \; 

There are problems with some of the other answers provided. For example, each of the following will try to move subdirectories from the source path:

1) mv /path/subfolder/* /path/ ; mv /path/subfolder/.* /path/ 2) mv /path/subfolder/* /path/ 3) mv /source/path/* /destination/path 

Also, 2) includes the . and .. files and 3) misses files like ..foobar, . barfoo, etc.

You could use, mv /source/path/* /destination/path , which would include the files missed by 3), but it would still try to move subdirectories. Using the find command with the mv command as I describe above eliminates all these problems.

Alternative simpler solution is to use rsync utility:

sudo rsync -vuar --delete-after --dry-run path/subfolder/ path/ 

Note: Above command will show what is going to be changed. To execute the actual changes, remove —dry-run .

Читайте также:  Portwine linux ru port proton linux

The advantage is that the original folder ( subfolder ) would be removed as well as part of the command, and when using mv examples here you still need to clean up your folders, not to mention additional headache to cover hidden and non-hidden files in one single pattern.

In addition rsync provides support of copying/moving files between remotes and it would make sure that files are copied exactly as they originally were ( -a ).

The used -u parameter would skip existing newer files, -r recurse into directories and -v would increase verbosity.

Источник

How do I create a copy of a directory in Unix/Linux? [closed]

I want to recursively create a copy of a directory and all its contents (e.g. files and subdirectories).

3 Answers 3

The option you’re looking for is -R .

cp -R path_to_source path_to_destination/ 
  • If destination doesn’t exist, it will be created.
  • -R means copy directories recursively . You can also use -r since it’s case-insensitive.
  • To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764 ‘s / @Anton Krug ‘s comment):
cp -a path_to_source/. path_to_destination/ 

i wonder why this exact command in dockerfile copies all source directory files into destination, instead of copying just whole directory.

I believe the ‘/’ on the end makes a difference and that might account for your experience. If the source includes the trailing slash it will copy what is in the directory only. If it does not include the trailing slash, it will copy the directory as well and then the contents inside of it. My memory is this behavior varies by command and maybe event by OS a bit. Here’s a reference with more info.

I would say if you don’t want to include the source and you want to make sure everything is copied (symlinks, hidden files) without copying the source parent folder is to use -ra source/. destination. This will make sure the content of the folder is copied, but not the parent folder itself, which is sometimes handy. And the difference is the /.

Note the importance of «Slash dot» on your source in cp -r src/. dest I know it is mentioned but I still seem to miss it every time.

You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.

If the directory you’re copying is called dir1 and you want to copy it to your /home/Pictures folder:

Linux is case-sensitive and also needs the / after each directory to know that it isn’t a file. ~ is a special character in the terminal that automatically evaluates to the current user’s home directory. If you need to know what directory you are in, use the command pwd .

Читайте также:  Apk add alpine linux

When you don’t know how to use a Linux command, there is a manual page that you can refer to by typing:

Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you’ve started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.

There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R , -r , and —recursive are all equivalent, as mentioned in this answer. For portability, i.e. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r . It’s important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).

Источник

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