I know of the mv
command to move a file from one place to another, but how do I move all files from one directory into another (that has a bunch of other files), overwriting if the file already exists?
Using mv Command The mv command is used to move files and directories from one place to another. We can also use it to rename files and directories. This will move all the files from /path/subfolder to /path/ except for hidden files and directories.
Attention: The mv (move) command can overwrite many existing files unless you specify the -i flag. The -i flag prompts you to confirm before it overwrites a file. If both the -f and -i flags are specified in combination, the last flag specified takes precedence.
The mv command moves files and directories from one directory to another or renames a file or directory. If you move a file or directory to a new directory, it retains the base file name.
Usually, when you run a cp command, it overwrites the destination file(s) or directory as shown. To run cp in interactive mode so that it prompts you before overwriting an existing file or directory, use the -i flag as shown.
mv -f source target
From the man page:
-f, --force do not prompt before overwriting
It's just mv srcdir/* targetdir/
.
If there are too many files in srcdir
you might want to try something like the following approach:
cd srcdir find -exec mv {} targetdir/ +
In contrast to \;
the final +
collects arguments in an xargs
like manner instead of executing mv
once for every file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With