4 ms·
Why do that when a loop is clearer, safer, and shorter? find images -name '*.jpg' | while read jpg; do convert -geometry 200x "$jpg" "$(echo "$jpg" | s
by BrandonM 14y ago
Why do that when a loop is clearer, safer, and shorter?
find images -name '*.jpg' | while read jpg; do
convert -geometry 200x "$jpg" "$(echo "$jpg" | sed 's/.jpg$/_thumb.jpg/')";
done
This version works even when there are spaces in a filename, whereas yours will break.
- _ZeD_ 14y agofalse $ ls -Ql totale 4 -rw-r--r-- 1 zed users 33 set 6 07:30 " spaces " $ $ find -name '*spaces*' | while read text; do cat "$text"; done cat: ./ spaces: File o directory non esistente $ $ find -name '*spaces*' -print0 | xargs -0 cat while read is broken with spaces $
- mkl 14y agoOnly for file names starting or ending with spaces, as far as I can tell, which are pretty unusual.
- ralph 14y agoIFS needs some care and attention and read should have -r. $ ls -Q " spaces " $ ls | while IFS="\n" read -r f; do ls "$f"; done spaces $ For lots of grim detail see David A. Wheeler's http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html http://www.dwheeler.com/essays/fixing-unix-linux-filenames.h...
- BrandonM 14y agoThat is a good point, but it is a much more degenerate case than the more common internal space.
- shorter 14y agoan alternative approach echo images/*.jpg|sed 's/.*/convert -geometry 200x \"&\" \"&_thumb.jpg\"/;s/\.jpg_thumb/_thumb/' > script; sh -v script 2>log you could also use -x instead of -v reasoning: globbing is as fast as find and is faster than find _if_ you do the same glob twice (b/c it's cached); i'm assuming the images dir is not full of subfolders otherwise i would would do echo images/*/* if the dir structure gets any deeper than 2 levels, i'd use find while loops typed from the command line are a p.i.t.a. if you don't bg them initially and want to bg them later; you need to put them inside brackets or into a function using a script and a log, if something goes wrong while it's running, you can restart after looking at the log; you can't do that with a loop
- mkl 14y agoEven shorter, avoiding the echo sed business: find images -name '*.jpg' | while read jpg; do convert -geometry 200x "$jpg" "${jpg%%.jpg}_thumb.jpg"; done
- super_mario 14y agoWhy use sed here? find images -name "*.jpg" | while read -r jpg; do convert -geometry 200x "$jpg" "${jpg%.jpg}_thumb.jpg" done This correctly handles spaces in file names and uses built in shell string replacement.
- frankc 14y agoYou can parallelize this operation with xargs simply by adding a -P. You could add a & to your convert here but that would run all the jobs at the same time. xargs allows you to only run n at a time. That would be a lot harder to replicate in bash.