I have 100 files in a single folder including a folder called "target." I want to migrate all the files in this folder into the folder target (except for the target folder itself).
Is there an efficient terminal command to do this?
If there are only the files (and the directory target) in your directory, simply use mv
, rather than find
:
mv * target
It will complain that 'target' can't be moved on itself, but the files will all be in target afterwards.
target/
to make sure that I didn't accidentally rename all files to 'target'.
Use this
$ mv target ..
$ mv * ../target
$ mv ../target .
This works as well:
find . -maxdepth 1 ! -name target ! -name . -exec mv "{}" target \;
One of the key advantages of find
over relying on bash completion is you get the hidden files at the same time. mv * ...
won't do that.
You could perhaps go with something like:
mv -t target * .[^.]*
Or as James points out you could trim down the find command to only look at files... And as LoremIpsum pointed out if there are billions of them, using xargs
would be slightly more efficient:
find . -maxdepth 1 -type f -print0 | xargs -0 mv -t target
find . -maxdepth 1 -type f -exec ...
to select by type rather than excluding particular names.
Mar 29, 2012 at 4:02
The approach of moving the destination directory to a previous one then moving everything to it and finally moving it back to current directory if fine. The only problem is that it does not work for a git project directory where we need to keep the history.
So for a git directory just move like this: git mv * target -k
The -k option is used to skip move/rename errors