1

I have many files in sub-directories that I would like to have all moved to the parent directory.

Ex:

20150530XXXX/1.jpg
20150530XXXX/2.jpg
20150530XXXX/3.jpg
20150530XXXX/4.jpg
20150531XXXX/1.jpg
20150531XXXX/2.jpg
20150531XXXX/3.jpg
20150531XXXX/4.jpg

I found in another question on stackexchange the following commmand:

find ./ -mindepth 2 -type f -exec mv -t A {} +

however, that seems to overwrite the image files because each have the same name (1-4.jpg). Is there a way I can concatenate the sub-directory's name with the file name, for instance move 20150531XXXX/4.jpg to ./20150531XXXX_4.jpg?

5

If you have the perl version of rename (named prename on some distributions):

find . -type f -mindepth 2 -name '*.jpg' -exec rename -n 's!/([^/]+)$!_$1!' {} +

This will only show you what the command will do. If you're happy with that, drop the -n option to actually rename the files (the actual rename doesn't print anything).

  • @brett If lcd047 has solved your question, don't forget to mark lcd047's answer as accepted, by clicking the grey check-mark next to it. Thanks :) – Sergiy Kolodyazhnyy Jun 28 '15 at 21:37

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.