0

i'm new in Linux, and i've been trying to run a script which processes all the files in a folder using ImageMagick's convert (I rather do this task in Shell than using mogrify, because as far as I know it doesn't save different files). The files have to be processed in 'last modified' order, so I used this code:

for file in `ls -1tr {*.jpg,*.png}`; do
    # imagemagick processes with the filename...
done

This code breaks for files with spaces, and according to this answer using ls is wrong for these purposes.

I also tried this solution from this response, but apparently I got it totally wrong (It raised an 'ambiguous redirect' error) and I decided I needed help.

while read LINE; do
    ...
done `(ls -1tr {*.png,*.jpg}`

So how do I get an ordered list of filenames for a loop? (It doesn't necessarily have to be a FOR...IN loop, it can be a WHILE, or anything.)

Community
  • 1
  • 1
Enriq
  • 13
  • 3
  • If you want to use `mogrify` without overwriting the input file, you can add `-path outputFolder` into your `mogrify` command and it will write the results in a subdirectory called `outputFolder`, e.g. `mogrify -resize 10% -output results *.jpg` – Mark Setchell Nov 04 '15 at 10:05

1 Answers1

0

try this :

for file in `ls -1tr {*.jpg,*.png} | awk '{print $9}'`; do
    # imagemagick processes with the filename...
done

ls -lrth give's 9 coulmns in output, out of which you need only 9th column(file names), you can get that using awk

If you have space seperated filenames, modify awk print to print all data after 9th column

Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48
  • I tried this but the `-1tr` flag (for sorting according to the date) returns only the name, then I modified to `awk {print $1}` and it returned only the first name of a space-separated filename as I was afraid. – Enriq Nov 04 '15 at 04:41
  • If you have space seperated file name you have to modify awk to capture all data after 9th column – Akhil Thayyil Nov 04 '15 at 04:43
  • I got the solution by using `{print $0}` for capturing all the arguments in line and then setting the IFS variable as [this post](http://stackoverflow.com/a/2860103/3517253) says, thanks for the help! – Enriq Nov 04 '15 at 05:54