5

I'm trying to find files containing *.nef and move them to a subdirectory name NEF of the folder in which the file is found.

I've started testing with the following command, but this always copies to my current directory, which is my home folder.

find testfolder/*.nef -exec mv NEF \;

In the man find section i've read about using -execdir, but using this instead of -exec still has the same result.

So the question is: how can i dynamically assume the currently found directory and mv found files to a sub-directory folder named NEF? (which does not yet exist)

Thanks in advance!

Sudheer
  • 5,213
fairlynuts
  • 63
  • 1
  • 5

2 Answers2

6

You were right in considering -execdir. Something simple like the below should work

find testfolder/ -name '*.nef' -execdir mkdir -p NEF \; -execdir mv {} NEF/ \; 
2

Try:

find  testfolder/ -iname "*.nef" -exec bash -c 'mkdir $(dirname "{}")/NEF ; mv "{}" $(dirname "{}")/NEF/' \;

dirname used to extract path from result then use it to make new subdirectory before moving the file.

user.dz
  • 49,176