176

How can I move all files and folders from one directory to another via mv command?

0

7 Answers 7

204

Try with this:

mv /path/sourcefolder/* /path/destinationfolder/
9
  • 49
    This wouldn't include any "hidden" files (eg. .htaccess) Oct 5, 2012 at 12:53
  • 27
    Good point. If you are using bash, then you can run shopt -s dotglob and then "*" will match hidden files, too.
    – chutz
    Oct 5, 2012 at 14:52
  • 2
    What happens if there are folders and files with the same name in the destination folder? Are they overwritten? Jun 14, 2016 at 1:08
  • 3
    ... it seems folders with the same name are not overwritten. mv: cannot move '/a/js' to '/b/js': Directory not empty Jun 14, 2016 at 1:29
  • 2
    You just pass -f to it to overwrite
    – Luka
    Jul 20, 2018 at 11:22
39

zsh:

mv /src/*(D) /dst/

(D) to include dot-files.

3
  • 1
    Syntax error on (D) Why??
    – Pathros
    Jun 23, 2018 at 17:12
  • 4
    @Pathros, probably because you'd not doing doing it in zsh. Jun 23, 2018 at 19:13
  • 1
    I know I accepted the first answer many years ago, and it's stupid for me now to change it. But actually, I've been always using this method.
    – Luka
    Jul 20, 2018 at 11:23
21

This works for me in Bash (I think this depends on your shell quite a bit...)

$  mv source/{,.}* /destination/folder/here
4
  • 4
    Actually, it shouldn't since in Bash source/{,.}* matches dir-entries named ./ and ../
    – poige
    Oct 10, 2012 at 21:48
  • 1
    When I try I get mv: overwrite 'destination/.'? mv: overwrite 'destination/..'?, but adding -n to mv stops it from trying to overwrite
    – Putnik
    Dec 5, 2012 at 20:05
  • 2
    @Putnik - that's a good gotcha! what os/distro ? ( I was working on OSX when I was messing around with this...) Dec 7, 2012 at 3:38
  • 1
    @Niall Byrne - I see the same prompt as Putnik. Using Ubuntu 14.04. Dec 23, 2014 at 6:16
10

This works for me in Bash 4.2.46, it moves all files and folders including hidden files and folders to another directory

mv /sourcedir/{,.[^.]}* /destdir/

Notice that .[^.]* means all hidden files except . and ..

5
  • 2
    .[^.]* (or its POSIX equivalent .[!.]*) also excludes ..anything files. Oct 26, 2016 at 12:30
  • It works in tcsh too.
    – Jaime M.
    Feb 12, 2018 at 14:45
  • unix.stackexchange.com/a/402856/93768 contains the full correct expression. Sep 6, 2018 at 18:50
  • It says "directory not empty" in for my .git folder Apr 26, 2019 at 10:09
  • using an example with the same folder name to avoid contents copying over into a parent directory mv /mnt/data/home/USERX/folderY/{,.[^.]}* /home/USERX/folderY
    – jxramos
    Oct 10, 2023 at 18:37
3

I'd say it's a bit boring, but really bullet-proof (GNU) way is:

cd /SourceDir && find ./ -maxdepth 1 -mindepth 1 -exec mv -t /Target/Dir {} +

P. S. Now you can possibly see why lots of people do prefer Midnight Commander, though.

-1

If you only want to do a cut and paste-like action there is a simple way that worked for me:

$mv /media/dir_source $HOME/Documents/ 

It will move the folder named dir_source located in /media to the directory $HOME/Documents/

-2

yet another way just for the heck of it (because I love convoluted ways to do things I guess)

cd /source
for f in $(\ls -QA); do eval mv $f /destination/$f; done

the -Q and the -A are not POSIX, however the -A is fairly prevalent, and to not use the -Q you need to change the IFS (which then means you don't need the eval but need to quote the variable)

IFS="
" && for f in $(ls -A); do mv "$f" /destination/"$f"; done
2
  • ls -Q doesn't output in a format that is suitable to use with eval or even $(...). Try after having run touch '$(reboot)' for instance (or touch '$(uname)' for a milder version). Oct 8, 2015 at 14:56
  • -A is now POSIX (since POSIX.1-2008). Oct 8, 2015 at 14:58

Not the answer you're looking for? Browse other questions tagged .