Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move (and overwrite) all files from one directory to another?

Tags:

linux

unix

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?

like image 742
Chris Avatar asked Aug 25 '11 13:08

Chris


People also ask

How do you move all files from a directory to another?

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.

Does move overwrite?

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.

Does mv overwrite a folder?

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.

How do I override a directory?

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.


2 Answers

mv -f source target 

From the man page:

-f, --force           do not prompt before overwriting 
like image 131
David Parks Avatar answered Sep 22 '22 02:09

David Parks


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.

like image 33
musiKk Avatar answered Sep 25 '22 02:09

musiKk