460

I would like to know how could I move all files from a folder to another folder with a command line.

Let's say I'm in my Downloads folder and there are a 100 files that I would like to move to my Videos folder, without having to write all the files name.

3
  • You're asking about moving files but people showing you how to move not only files but foldersas well. Is that OK?
    – Askar
    Feb 26, 2015 at 1:35
  • 2
    @Hontvári Levente gave an answer a year ago that seems to be by far the best, clean, simple, and it works. So how did it get only 3 votes (as compared to 262 for the answer currently at the top)? Jan 19, 2019 at 1:26
  • 1
    Simple: mv * ~/dir
    – Null_Space
    Jul 8, 2021 at 20:42

12 Answers 12

573

Open a terminal and execute this command:

mv  -v ~/Downloads/* ~/Videos/

It will move all the files and folders from Downloads folder to Videos folder.


To move all files, but not folders:

If you are interested in moving all files (but not folders) from Downloads folder to Videos folder, use this command

find ~/Downloads/ -type f -print0 | xargs -0 mv -t ~/Videos

To move only files from the Download folders, but not from sub-folders:

If you want to move all files from the Downloads folder, but not any files within folders in the Download folder, use this command:

find ~/Downloads/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ~/Videos

here, -maxdepth option specifies how deep find should try, 1 means, only the directory specified in the find command. You can try using 2, 3 also to test.

See the Ubuntu find manpage for a detailed explanation

13
  • 4
    you can use mv alone to move an entire directory to another folder: mv folder ~/Documents Aug 9, 2014 at 22:42
  • 48
    FYI I believe your first command ("mv -v ~/Downloads/* ~/Videos/") won't move dot files. Dec 23, 2014 at 6:11
  • 4
    Nb. your -print0 | xargs -0 mv -t ~/Videos can be more efficiently done with -exec mv -t ~/Videos \{\} \+ :-) Jun 9, 2015 at 13:46
  • 10
    mv -v ~/Downloads/* ~/Videos/ does not work for hidden files Mar 18, 2018 at 15:20
  • 4
    for hidden files trick folder/.* (note the dot) works - it says some busy errors, but the move still happens => source: askubuntu.com/a/399632
    – jave.web
    Aug 17, 2019 at 21:41
50
mv ~/Downloads/* ~/Videos

It will move all the files including subfolders in the directory you want to mv. If you want to cp (copy) or rm (remove) you will need the -r (recursive) option to include subfolders.

6
  • 2
    My mv command doesn't have a -R option (Ubuntu 14.04). Dec 23, 2014 at 6:10
  • 1
    @MarkDoliner, yes, mv doesn't need recursive option to include subfolders. One can use it also for renaming. Jan 25, 2015 at 23:26
  • I didn't need * . Same thing happened without using the star.
    – MycrofD
    Mar 22, 2017 at 13:28
  • The ~/ in the prefix of the folder names doesn't always work (doesn't work on bash and git atleast) Jun 6, 2017 at 23:00
  • This solution will give an error if the source directory is empty. Jul 26, 2018 at 13:04
44

For the simple case:

mv ~/Downloads/* ~/Videos

If you want to move dot (hidden) files too, then set the dotglob shell option.

shopt -s dotglob
mv ~/Downloads/* ~/Videos

This leaves the shell option set.

For one time dotglob use, run the commands in a subshell:

(shopt -s dotglob; mv ~/Downloads/* ~/Videos)
5
  • A note for myself: The last option (shopt -s dotglob; mv ~/Downloads/* ~/Videos) only moves (cuts) the contents (including the hidden files). In this case, both the origin and destination folders must exist already. At the end, the origin directory becomes empty.
    – Pathros
    Apr 26, 2019 at 3:25
  • 3
    This should be the accepted answer
    – megar
    May 10, 2019 at 9:26
  • 1
    It works only under certain conditions (namely, if length of files is less than getconf ARG_MAX bytes)
    – DimanNe
    Jul 3, 2022 at 16:10
  • 2
    Yes. On Ubuntu 20.04 getconf ARG_MAX is 2MiB. Therefore this method works if there are less than about 250,000 files in the directory. Aug 10, 2022 at 9:38
  • ~/Downloads/* No such file or directory
    – AlxVallejo
    Mar 13, 2023 at 20:02
22

It's possible by using rsync, for example:

rsync -vau --remove-source-files src/ dst/

where:

-v, --verbose: Increase verbosity.

-a, --archive: Archive mode; equals -rlptgoD (no -H, -A, -X).

-u, --update: Skip files that are newer on the receiver.

--remove-source-files This tells rsync to remove from the sending side the files (meaning non-directories) that are a part of the transfer and have been successfully duplicated on the receiving side.

If you've root privileges, prefix with sudo to override potential permission issues.

2
  • 4
    WARNING! the --delete-after option as noted doesn't work the way you might expect. It doesn't delete the source files after successful copy... IT DELETES ALL THE REMAINING/OTHER FILES IN THE DESTINATION. (as @kenorb noted... but I didn't read carefully enough! DOH)
    – Jay Marm
    Aug 10, 2018 at 4:06
  • 3
    This warning can be ignored / should be removed as the author corrected his answer.
    – pLumo
    Feb 25, 2021 at 6:53
10

To move a directory with or without content to its new name just like how you would use the mv command to rename a file:

mv -T dir1 dir2

where:

  • -T treats the destination as a normal file
  • dir1 is the original name of the directory
  • dir2 is the new name of the directory

NB: dir2 doesn't have to exist.

I hope this saves someone a lot of time, as a noob, before this, I would create a directory with the new name and then move the contents of the directory to the directory created earlier.

Use for subdirectories

This command is useful when many files have been saved in a subfolder of the target directory i.e. Downloads/mp4. In this example, running mv -T Downloads/mp4 Videos will result in mp4 subfolder being removed and all files contained inside are moved to Videos folder.

7
  • 1
    +1 because tested working in Xubuntu 14.04. I have added an example in this answer to show that this command will cause the dir1 to be removed. Aside from this precaution, this answer has noted something good for daily use.
    – user37165
    Dec 9, 2015 at 8:45
  • 1
    "To move a directory with or without content to its new name" How can you move a directory...to a name? That makes no sense. "-T treats the destination as a normal file" How can a destination be a file? Do you mean directory? Apr 29, 2017 at 23:55
  • Its like renaming a file to its new name like you would do with something like mv fileA fileB but the -T flag in this case treats the destination/directory as a file and renames it. Apr 30, 2017 at 22:10
  • Worked for me :) Mar 12, 2019 at 20:14
  • 1
    This should be the accepted answer. Mar 15, 2020 at 8:44
6
mv source_path/* destination_path/

here you have to put forward slash and * after source path so that it will take files inside source_path instead of the complete source directory.

Example: mv /home/username/test/* /home/username/test2/

The above command moves all files (unless they are hidden) in the source directory to the destination directory.

2
  • It works only under certain conditions (namely, if length of files is less than getconf ARG_MAX bytes)
    – DimanNe
    Jul 3, 2022 at 16:10
  • It was not so complicated. Thx. The best answer with no doubt
    – Luis Gar
    Mar 8, 2023 at 9:55
5

Use

mv -v ~/rootfolder/branch/* ~/rootfolder

I hope this helps. Because I had the same pain and wasted a lot of time fixing my mistake.

2
  1. Go to the command line and get into the directory you want to move it to with cd folderNamehere
  2. Type pwd. This will print the directory you want to move it too.
  3. Then change to the directory where all of the files are with cd folderNamehere
  4. Now to move all the files type mv *.* typeAnswerFromStep2here

That will move all files from that directory to the other.

1
  • 3
    This will not match files without any extension. for example if a folder has the files: foo.txt, bar. and bar both bar. and bar will not be moved. Using * instead of *.* takes care of that. But in both cases, hidden files like: .foobar will not be moved.
    – Dan
    Sep 12, 2013 at 14:20
2

try it

find ~/Desktop -type f -iname  "*.mp4" -exec mv {} ~/Videos \;

-type with the argument -type you can specify type file.on this statement that is the mean file.if using of -d that means directory.

-iname: the most common and obvious method to look for a file is using its -name argument.if you are not sure about its case-sensitivity you can use of -iname argument

mv {} and finally to specify target directory and then moving the files on there using mv {} argument

1

This command should do it:

sudo find ~/Downloads -mindepth 1 -prune -exec mv '{}' ~/Videos \;

It moves all visible and hidden files, and doesn't throw unnecessary errors, not even if the source directory is empty.

1

you can copy all contents using rsync and remove all files using --remove-source-files remove copied directories using find -type d | rm :

rsync -auv  --remove-source-files ./src ./dst \
&& find ./dst -type d | sed -n '1d;p' | xargs rm -rf -

The command sed -n '1d;p' excludes first directory path from the directories listed, since the first directory in the list is directory dst itself!

OR you can run all in a single command (Thanks to good muslim @muru):

find  ./dst -mindepth 1 -type d -delete
1
  • 1
    You don't need all that. -mindepth 1 with find will exclude dst from the matched list. find itself has the -delete action for deleting.
    – muru
    Jan 6, 2021 at 7:44
0

Just wanted to add one more answer. If you are already in the folder you want to move files out of, you can do this:

mv * ~/Videos
4
  • This answer is a duplicate of this existing answer: askubuntu.com/a/387727
    – karel
    Feb 29, 2020 at 4:03
  • 1
    @karel This answer is intended to cover the case "If you are already in the folder you want to move files out of" and the command it shows is accordingly different from the command in that answer. Whether or not one considers it useful to have an answer covering this case, this answer is not a duplicate of that one. Feb 29, 2020 at 14:26
  • Thanks for the backup @Eliah Kagan
    – John Solly
    Mar 1, 2020 at 5:30
  • It works only under certain conditions (namely, if length of files is less than getconf ARG_MAX bytes)
    – DimanNe
    Jul 3, 2022 at 16:10

You must log in to answer this question.

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