How do I properly do a for loop in reverse order?
for f in /var/logs/foo*.log; do
bar "$f"
done
I need a solution that doesn't break for funky characters in the file names.
|
How do I properly do a
I need a solution that doesn't break for funky characters in the file names. |
||||
|
In bash or ksh, put the file names in an array, and iterate over that array in reverse order.
The code above also works in zsh if the
POSIX doesn't include arrays, so if you want to be portable, your only option to directly store an array of strings is the positional parameters.
|
|||
|
|
|
Try this, unless you consider line breaks as "funky characters":
|
|||||||||
|
Should operate the way you want (this was tested on Mac OS X and I have a caveat below...). From the man page for find:
Basically, you're finding the files that match your string + glob and terminating each with a NUL character. If your filenames contain newlines or other strange characters, find should handle this well.
takes the standard input through the pipe and reverses it (note that We then pipe that to
Here, xargs expects to see arguments separated by the NUL character, which you passed from My caveat: I've read that I should also mention that GNU Parallel is often used as an I may be missing something, so others should chime in. |
|||||||||||||||||
|
sort -rbefore thefor, or launder throughls -r. – David Schwartz Dec 22 '11 at 4:59