0

I have a huge directory that is made up of a lot of sub-directories. How do I flatten the images in such a way that they appear on top of the other, and rename them to the subdirectory they were in?

For example, how to rename dir1/dir2/dir3/dir4/XYZ.jpg to dir1__dir2__dir4.jpg using $bash?

Thanks in advance

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 3
    We encourage questioners to show what they have tried so far to solve the problem themselves. – Cyrus May 21 '21 at 19:57

1 Answers1

1
find . -name '*.jpg' -print0 | while read -d '' file
do
  target="${file#./}" # remove './' at the beginning
  target="${target%\/*}" # remove '/*' at the end
  target="${target//\//_}.jpg" # replace '/' by '_' and add '.jpg'
  mv "$file" "$target" # do the moving
done

This is basically a combination of two things: How to loop through file names returned by find? and parameter expansion.

j1-lee
  • 13,764
  • 3
  • 14
  • 26