1

How change this string:

find . -type f -name "*.jpg" | while read i; do convert "$i" -resize 50% "${i%%.jpg*}_tn.jpg"; done

to make tn_FILENAME.jpg files, not FILENAME_tn.jpg

Thank you!

1 Answer 1

2
find . -type f -name "*.jpg" | while read i; do [[ "${i##*/}" =~ ^tn_ ]] || convert "$i" -resize 50% "${i%/*}/tn_${i##*/}"; done

You mean like this?

${i%/*} is the filename stripped of everything following the last dash (so the dir in which the file is located).

/tn_ adds the tn_ prefix to the file, and

${i##*/} strips everything from the file before the last dash (so it's the filename).

Paste these three together and you get your result.

Sign up to request clarification or add additional context in comments.

5 Comments

yes. but i have error "convert.im6: unable to open image `tn_./sets/testl/test-123.jpg.jpg': No such file or directory @ error/blob.c/OpenBlob/2638."
Try this: "${i%/*}/tn_${i##*/}"
No problem, you need me to clarify the solution?
Add grep -v tn_* when you want to run the command again after new files in the folders.
Good point, although that grep matches any path not containing "tn".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.