5

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?

9

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.

  • 2
    +1. However, I would use target/ to make sure that I didn't accidentally rename all files to 'target'. – zpletan Mar 29 '12 at 3:25
  • 1
    As long as you've got more than one file to move, that shouldn't be a problem. It doesn't hurt though. – James Henstridge Mar 29 '12 at 4:00
  • 1
    This does not handle hidden files. – Oli Mar 29 '12 at 12:13
2

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
  • You could probably simplify this to find . -maxdepth 1 -type f -exec ... to select by type rather than excluding particular names. – James Henstridge Mar 29 '12 at 4:02
  • Maybe xargs could be used, too ? – LoremIpsum Mar 29 '12 at 8:14
1

Use this

$ mv target ..
$ mv * ../target
$ mv ../target .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.