How to move all files and folders via mv command

713,104

Solution 1

Try with this:

mv /path/sourcefolder/* /path/destinationfolder/

Solution 2

zsh:

mv /src/*(D) /dst/

(D) to include dot-files.

Solution 3

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

$  mv source/{,.}* /destination/folder/here

Solution 4

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

Solution 5

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.

Share:
713,104

Related videos on Youtube

Luka
Author by

Luka

My name is Luka, I am student of Business economics at John Naisbitt University. And I am Linux System Administrator, PHP, jQuery (JavaScript) developer. Love technology and new things, always learning. Currently running a web hosting company in Serbia Leo Host.

Updated on September 18, 2022

Comments

  • Luka
    Luka over 1 year

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

  • Ben Lessani
    Ben Lessani over 11 years
    This wouldn't include any "hidden" files (eg. .htaccess)
  • chutz
    chutz over 11 years
    Good point. If you are using bash, then you can run shopt -s dotglob and then "*" will match hidden files, too.
  • poige
    poige over 11 years
    Actually, it shouldn't since in Bash source/{,.}* matches dir-entries named ./ and ../
  • Silvio Silva
    Silvio Silva over 11 years
    When I try I get mv: overwrite 'destination/.'? mv: overwrite 'destination/..'?, but adding -n to mv stops it from trying to overwrite
  • Jensen010
    Jensen010 over 11 years
    @Putnik - that's a good gotcha! what os/distro ? ( I was working on OSX when I was messing around with this...)
  • Stéphane Chazelas
    Stéphane Chazelas over 8 years
    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).
  • Stéphane Chazelas
    Stéphane Chazelas over 8 years
    -A is now POSIX (since POSIX.1-2008).
  • user1063287
    user1063287 almost 8 years
    What happens if there are folders and files with the same name in the destination folder? Are they overwritten?
  • user1063287
    user1063287 almost 8 years
    ... it seems folders with the same name are not overwritten. mv: cannot move '/a/js' to '/b/js': Directory not empty
  • Stéphane Chazelas
    Stéphane Chazelas over 7 years
    .[^.]* (or its POSIX equivalent .[!.]*) also excludes ..anything files.
  • Jaime M.
    Jaime M. about 6 years
    It works in tcsh too.
  • Pathros
    Pathros almost 6 years
    Syntax error on (D) Why??
  • Stéphane Chazelas
    Stéphane Chazelas almost 6 years
    @Pathros, probably because you'd not doing doing it in zsh.
  • Luka
    Luka almost 6 years
    You just pass -f to it to overwrite
  • Luka
    Luka almost 6 years
    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.
  • DJCrashdummy
    DJCrashdummy over 5 years
    unix.stackexchange.com/a/402856/93768 contains the full correct expression.
  • trainoasis
    trainoasis over 5 years
    @Luka actually -f doesn't help with overwriting folders. Says the same thing. You gotta use rsync or make sure folders are empty
  • Luka
    Luka over 5 years
    It does help if you use /bin/mv -f instead of mv -f. Because mv is by default aliased to -i on some distributions. And that's why it appears it doesn't work. Add this alias cp='cp' to ~/.bash_aliases to fix it.
  • Luka
    Luka over 5 years
    @trainoasis so sorry, not -f, you should use -r. But the same applies to alias if it's aliased it won't work properly.
  • Thomas Weller
    Thomas Weller about 5 years
    It says "directory not empty" in for my .git folder
  • Gal Bracha
    Gal Bracha over 4 years
    To move the current dir content - mv ./* ../another_folder