Extract all image names with sub folder names into CSV file using shell script
Just because we can, here's a way that uses sed
to reverse the order of the fields:
find -name "*.jpg" | sed -rn 's|^.||; s|[^/]*.jpg||; :a h; s|.*/(.*)|\1|p; x; s|(.*)/.*|\1| ; ta' | tr '\n' ',' | sed 's/,,/\n/g ; s/,$/\n/; s/^,//'
Yeah I know O_O
But it works even if the directory structure is not consistent
Here it is more readably with comments:
find -name "*.jpg" | sed -rn '{ #get the files and pipe the output to sed
s|^.|| #remove the leading .
s|[^/]*.jpg|| #and the basename, since each image is in a directory of the same name
:a h #create a label a for this branch and put the lines into the hold space in their current state
s|.*/(.*)|\1|p #print only the last field
x #switch the hold space and pattern space
s|(.*)/.*|\1| #exclude the last field from the new pattern space, which won't do anything if there is only one field on each line
ta #if the last s command did anything, then start again from the label (:a) (thus recursively going through the fields and printing them out on separate lines in reverse order)
}' | tr '\n' ',' | sed '{ # finally turn the newlines into commas, then clean up the mess
s/,,/\n/g ; s/,$/\n/; s/^,//
}'