135

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:

mv /path/subfolder/* /path/

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

1
  • 1
    this question has a duplicate at SU, with an even more correct answer (not the accepted one though): cp -r /path/to/source/. /destination
    – Florian
    Oct 15, 2015 at 12:53

9 Answers 9

234

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

Or all together (thanks pfnuesel):

mv /path/subfolder/{.,}* /path/

Which expands to:

mv /path/subfolder/* /path/subfolder/.* /path/

(example: echo a{.,}b 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?)

10
  • 2
    The braces are just a short cut for mv /path/subfolder/* /path/subfolder/.* /path/, not strictly necessary to combine the two commands into one.
    – chepner
    Nov 25, 2013 at 13:48
  • 7
    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
    – Ura
    Mar 28, 2014 at 18:25
  • @Dejan Just ignore it. . denotes current directory and .. denotes up directory. You must have noticed that all other files are moved.
    – Debiprasad
    May 16, 2016 at 7:59
  • Worth mentioning that the braces shortcut won't work for all shells, but @chepner command I think it will Jun 8, 2016 at 9:17
  • 8
    "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...
    – MarioVilas
    May 8, 2017 at 9:55
31

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

mv /source/path/{.[!.],}* /destination/path
4
  • 1
    yeah I noticed it giving those error messages also, nice find. Apr 13, 2015 at 7:05
  • I think this is a very good solution but it's kinda hard to remember that pattern
    – Dylan B
    Jan 17, 2018 at 5:02
  • 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! Sep 6, 2018 at 18:03
  • @DylanB don't memorize it. remember that it matches whatever is in the curlybrackets, separated by commas. {a,b}* 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 ..*.
    – mazunki
    Apr 22, 2020 at 13:52
29

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:

shopt -u dotglob
2
  • 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.)
    – Nick Rice
    Nov 20, 2014 at 16:22
  • 2
    this will also affect all other commands like ls.. thus not really what you'd want, probably
    – phil294
    Mar 14, 2016 at 21:12
6

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.

0
6

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.

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.

4
  • Best solution ever! In my case I just removed the -u parameter, because I wouldn't like to update the root folder. Thanks Jul 13, 2015 at 17:10
  • This is one of those commands where if you're not careful, you can really mess things up for yourself in linux, permanently. Please update your answer to include sudo before the rsync command, otherwise, users can easily get Permission Denied for mkdir, but the file deletions will nevertheless move ahead, thus, the rsync command will delete your entire directory you want moved, and do nothing else. You will lose your entire directory. Once you add sudo to your answer, this should probably be the accepted answer. So the command should be: sudo rsync -vuar --delete-after path/subfolder/ path/
    – user2607743
    Feb 18, 2020 at 19:09
  • @JohnnyB Sorry about the inconvenience. I've added sudo and --dry-run, so people can test the changes before running the actual command in order to avoid any potential mess.
    – kenorb
    Feb 19, 2020 at 10:07
  • Oh, I didn't realize there was a --dry-run option. Good addition. No need for apology. This is excellent.
    – user2607743
    Feb 19, 2020 at 16:05
3

Let me introduce you to my friend "dotglob". It turns on and off whether or not "*" includes hidden files.

$ mkdir test
$ cd test
$ touch a b c .hidden .hi .den
$ ls -a
. ..  .den  .hi .hidden a b c

$ shopt -u dotglob
$ ls *
a b c
$ for i in * ; do echo I found: $i ; done
I found: a
I found: b
I found: c

$ shopt -s dotglob
$ ls *
.den  .hi .hidden a b c
$ for i in * ; do echo I found: $i ; done
I found: .den
I found: .hi
I found: .hidden
I found: a
I found: b
I found: c

It defaults to "off".

$ shopt dotglob
dotglob         off

It is best to turn it back on when you are done otherwise you will confuse things that assume it will be off.

2

My solution for this problem when I have to copy all the files (including . files) to a target directory retaining the permissions is: (overwrite if already exists)

yes | cp -rvp /source/directory /destination/directory/

yes is for automatically overwriting destination files, r recursive, v verbose, p retain permissions.

Notice that the source path is not ending with a / (so all the files/directory and . files are copied)

Destination directory ends with / as we are placing contents of the source folder to destination as a whole.

1
  • While yes is an awesome command to use if you want to override destination files, that is not always desired. Only use it if you are sure you want to do this. Also, is there an difference between yes | cp ... and cp -f ... ?
    – mazunki
    Apr 22, 2020 at 13:58
0

Just do

for I in $(ls -A dir)
do
mv dir/$I newDir
done
0

Assuming you are in the subfolder run find . -maxdepth 1 -exec mv {} .. \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.