- How to move all files and folders via mv command [duplicate]
- How to Move Files and Directories in Linux (mv Command)
- How to Use the mv Command #
- Moving Multiple Files and Directories #
- mv Command Options #
- Prompt before overwriting #
- Force overwriting #
- Do not overwrite existing files #
- Backing up files #
- Verbose output #
- Conclusion #
- Moving folder and subfolder to another path
- 6 Answers 6
- Edit
- You must log in to answer this question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- How do I move all files from one folder to another using the command line?
- 12 Answers 12
How to move all files and folders via mv command [duplicate]
Good point. If you are using bash, then you can run shopt -s dotglob and then «*» will match hidden files, too.
What happens if there are folders and files with the same name in the destination folder? Are they overwritten?
. it seems folders with the same name are not overwritten. mv: cannot move ‘/a/js’ to ‘/b/js’: Directory not empty
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.
This works for me in Bash (I think this depends on your shell quite a bit. )
$ mv source/* /destination/folder/here
When I try I get mv: overwrite ‘destination/.’? mv: overwrite ‘destination/..’? , but adding -n to mv stops it from trying to overwrite
@Putnik — that’s a good gotcha! what os/distro ? ( I was working on OSX when I was messing around with this. )
This works for me in Bash 4.2.46, it moves all files and folders including hidden files and folders to another directory
Notice that .[^.]* means all hidden files except . and ..
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.
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/
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
How to Move Files and Directories in Linux (mv Command)
Moving files and directories is one of the most basic tasks you often need to perform on a Linux system.
In this tutorial, we will explain how to use the mv command to move files and directories.
How to Use the mv Command #
The mv command (short from move) is used to rename and move and files and directories from one location to another. The syntax for the mv command is as follows:
mv [OPTIONS] SOURCE DESTINATION
The SOURCE can be one, or more files or directories, and DESTINATION can be a single file or directory.
- When multiple files or directories are given as a SOURCE , the DESTINATION must be a directory. In this case, the SOURCE files are moved to the target directory.
- If you specify a single file as SOURCE , and the DESTINATION target is an existing directory, then the file is moved to the specified directory.
- If you specify a single file as SOURCE , and a single file as DESTINATION target then you’re renaming the file .
- When the SOURCE is a directory and DESTINATION doesn’t exist, SOURCE will be renamed to DESTINATION . Otherwise if DESTINATION exist, it be moved inside the DESTINATION directory.
To move a file or directory, you need to have write permissions on both SOURCE and DESTINATION . Otherwise, you will receive a permission denied error.
For example, to move the file file1 from the current working directory to the /tmp directory you would run:
To rename a file you need to specify the destination file name:
The syntax for moving directories is the same as when moving files. In the following example, if the dir2 directory exists, the command will move dir1 inside dir2 . If dir2 doesn’t exist, dir1 will be renamed to dir2 :
Moving Multiple Files and Directories #
To move multiple files and directories, specify the files you want to move as the source. For example, to move the files file1 and file2 to the dir1 directory you would type:
The mv command also allows you to use pattern matching. For example, to move all pdf files from the current directory to the ~/Documents directory, you would use:
mv Command Options #
The mv command accepts several options that affect default command behavior.
In some Linux distributions, mv may be an alias to the mv command with a custom set of options. For example, in CentOS mv is an alias to mv -i . You can find whether mv is an alias using the type command:
If mv is alias the output will look something like this:
If conflicting options are given, the last one takes precedence.
Prompt before overwriting #
By default, if the destination file exists, it will be overwritten. To prompt for confirmation, use the -i option:
To overwrite the file type y or Y .
Force overwriting #
If you try to overwrite a read-only file, the mv command will prompt you whether you want to overwrite the file:
mv: replace '/tmp/file1', overriding mode 0400 (r--------)?
To avoid being prompted use the -f options:
This option is especially useful when you need to overwrite multiple read-only files.
Do not overwrite existing files #
The -n option tells mv never to overwrite any existing file:
If a file1 exists the command above will do nothing. Otherwise it will move the file to the /tmp directory.
Backing up files #
If the destination file exists you can create a backup of it using the -b option:
The backup file will have the same name as the original file with a tilde ( ~ ) appended to it.
Use the ls command to verify that the backup was created:
Verbose output #
Another option that can be useful is -v . When this option is used, the command prints the name of each moved file:
Conclusion #
The mv command is used to move and rename files and directories.
For more information about the mv command, check the man page or type man mv in your terminal.
New Linux users who are intimidated by the command line can use the GUI file manager to move their files.
If you have any questions or feedback, feel free to leave a comment.
Moving folder and subfolder to another path
I need to move my folder with many subfolders to another path. I’m using putty and this is what I tried: MV -r fromflderpath tofolderpath What am I doing wrong?
6 Answers 6
The mv command doesn’t have an -R flag, it moves folders recursively:
Edit
If you want a file not to be replaced, use the -i for being prompted in case a file with the same name exists.
:I’ve a file,i’ve to move this to another directory but in this directory,i’ve a file with same name.what is the command to move it ,if i move,will i get replaced.
am I missing something here? mv moves a folder and all its contents without any flags. no cp -r;rm -r needed
For those trying to move folder, on Ubuntu using Putty, just use the following command:
sudo mv /root/folder1 /home/folder2/
«/» in the end means you are going to move folder1 inside folder2
If you don’t, you will get «no such file or directory»
I tested on Ubuntu 20.04, and found that we don’t need the / at the end. with or without / , the results are the same: move folder1 to the inside of folder2. But anyways, it’s good practice to put the trailing / at the end, just in case of some mistakes.(check this out).
mv src_folder target_folder/src_folder
i.e. not mv src_folder target_folder/
If it’s hard to use pure shell commands, then you can install Midnight Commander, console application that makes it easier:
In Midnight Commander to move folder or file from one panel to another is F6 , copy F5 .
If you need root access
If you want to have a mouse support
If you want to empty fromflderpath without renaming it, stand in the folder and use
sudo mv source_folder/* target_folder/
source_folder/file.ext source_folder/bin/image.jpg source_folder/etra/info.text
all of them will be moved under target_folder like:
target_folder/file.ext target_folder/bin/image.jpg target_folder/etra/info.text
You must log in to answer this question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How do I move all files from one folder to another using the command line?
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.
You’re asking about moving files but people showing you how to move not only files but folders as well. Is that OK?
@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)?
12 Answers 12
Open a terminal and execute this command:
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
Nb. your -print0 | xargs -0 mv -t ~/Videos can be more efficiently done with -exec mv -t ~/Videos \ \+ 🙂\>
for hidden files trick folder/.* (note the dot) works — it says some busy errors, but the move still happens => source: askubuntu.com/a/399632
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.
@MarkDoliner, yes, mv doesn’t need recursive option to include subfolders. One can use it also for renaming.
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)
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.
It works only under certain conditions (namely, if length of files is less than getconf ARG_MAX bytes)
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.
It’s possible by using rsync , for example:
rsync -vau --remove-source-files src/ dst/
-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.
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)
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:
- -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.