41

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.

1
  • 5
    Just pipe to sort -r before the for, or launder through ls -r. Commented Dec 22, 2011 at 4:59

8 Answers 8

40

In bash or ksh, put the file names in an array, and iterate over that array in reverse order.

files=(/var/logs/foo*.log)
for ((i=${#files[@]}-1; i>=0; i--)); do
  bar "${files[$i]}"
done

The code above also works in zsh if the ksh_arrays option is set (it is in ksh emulation mode). There's a simpler method in zsh, which is to reverse the order of the matches through a glob qualifier:

for f in /var/logs/foo*.log(On); do bar $f; done

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.

set -- /var/logs/foo*.log
i=$#
while [ $i -gt 0 ]; do
  eval "f=\${$i}"
  bar "$f"
  i=$((i-1))
done
3
  • Once you're using positional parameters, there's no need for your variables i and f; just perform a shift, use $1 for your call to bar, and test on [ -z $1 ] in your while. Commented Feb 15, 2018 at 18:22
  • @user1404316 This would be an overly complicated way of iterating over the elements in ascending order. Also, wrong: neither [ -z $1 ] nor [ -z "$1" ] are useful tests (what if the parameter was *, or an empty string?). And in any case they don't help here: the question is how to loop in the opposite order. Commented Feb 15, 2018 at 18:38
  • The question specifically says that it is iterating over file names in /var/log, so it's safe to assume that none of the parameters would be "*" or empty. I do stand corrected, though, on the point of the reverse order. Commented Feb 15, 2018 at 18:42
15

Try this, unless you consider line breaks as "funky characters":

ls /var/logs/foo*.log | tac | while read f; do
    bar "$f"
done
5
  • Is there a method that works with line breaks? (I know it's rare, but at least for the sake of learning I'd like to know if it's possible to write correct code.)
    – user541686
    Commented Dec 22, 2011 at 5:04
  • Creative to use tac to reverse the flow, and if you like to get rid of some unwanted characters like line breaks you can pipe to tr -d '\n'.
    – Johan
    Commented Dec 22, 2011 at 6:07
  • 5
    This breaks if the file names contain newlines, backslashes or unprintable characters. See mywiki.wooledge.org/ParsingLs and How to loop over the lines of a file? (and the linked threads). Commented Dec 22, 2011 at 8:26
  • The most voted answer broke the variable f in my situation. This answer though, acts pretty much as a drop-in replacement for the ordinary for line. Commented Jul 6, 2015 at 23:14
  • That tac command just saved my day, thanks! Commented Oct 24, 2023 at 4:04
5

If anyone is trying to figure out how to reverse iterate over a space-delimited string list, this works:

reverse() {
  tac <(echo "$@" | tr ' ' '\n') | tr '\n' ' '
}

list="a bb ccc"

for i in `reverse $list`; do
  echo "$i"
done
> ccc
> bb 
> a
7
  • 2
    I don't have tac on my system ; I don't know if it's robust but I've been using for x in ${mylist}; do revv="${x} ${revv}"; done
    – arp
    Commented Jan 11, 2018 at 8:31
  • @arp That's sort of shocking as tac is part of GNU coreutils. But your solution is also a good one. Commented Jan 11, 2018 at 21:45
  • @ACK_stoverflow There are systems out there without Linux userland tools.
    – Kusalananda
    Commented Feb 15, 2018 at 18:21
  • @ACK_stoverflow That's what I'm saying, yes.
    – Kusalananda
    Commented Feb 16, 2018 at 6:46
  • Instead of tac, just do tail -r. Commented Feb 20, 2022 at 21:55
3

In your example you're looping over several files, but I found this question because of its more general title which could also cover looping over an array, or reversing based on any number of orders.

Here's how to do that in Zsh:

If you're looping over the elements in an array, use this syntax (source)

for f in ${(Oa)your_array}; do
    ...
done

O reverses of the order specified in the next flag; a is the normal array order.

As @Gilles said, On will reverse order your globbed files, e.g. with my/file/glob/*(On). That's because On is "reverse name order."

Zsh sort flags:

  • a array order
  • L file length
  • l number of links
  • m modification date
  • n name
  • ^o reverse order (o is normal order)
  • O reverse order

For examples, see https://github.com/grml/zsh-lovers/blob/master/zsh-lovers.1.txt and http://reasoniamhere.com/2014/01/11/outrageously-useful-tips-to-master-your-z-shell/

1
find /var/logs/ -name 'foo*.log' -print0 | tail -r | xargs -0 bar

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:

-print0
         This primary always evaluates to true.  It prints the pathname of the current file to standard output, followed by an ASCII NUL character (charac-
         ter code 0).

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.

tail -r

takes the standard input through the pipe and reverses it (note that tail -r prints all of the input to stdout, and not just the last 10 lines, which is the standard default. man tail for more info).

We then pipe that to xargs -0 :

-0      Change xargs to expect NUL (``\0'') characters as separators, instead of spaces and newlines.  This is expected to be used in concert with the
         -print0 function in find(1).

Here, xargs expects to see arguments separated by the NUL character, which you passed from find and reversed with tail.

My caveat: I've read that tail doesn't play well with null-terminated strings. This worked well on Mac OS X, but I can't guarantee that's the case for all *nixes. Tread carefully.

I should also mention that GNU Parallel is often used as an xargs alternative. You may check that out, too.

I may be missing something, so others should chime in.

7
  • 2
    +1 great answer. It seems like Ubuntu doesn't support tail -r though... am I doing something wrong?
    – user541686
    Commented Dec 22, 2011 at 6:02
  • 1
    No, I don't think you are. I don't have my linux machine up but a quick google for 'linux tail man' doesn't show it as an option. sunaku mentioned tac as an alternative, so I would try that, instead
    – tcdyl
    Commented Dec 22, 2011 at 6:17
  • I've also edited the answer to include another alternative
    – tcdyl
    Commented Dec 22, 2011 at 7:01
  • whoops I forgot to +1 when I said +1 :( Done! Sorry about that haha :)
    – user541686
    Commented Dec 22, 2011 at 7:22
  • 4
    tail -r is specific to OSX, and reverses newline-delimited input, not null-delimited input. Your second solution doesn't work at all (you're piping input to ls, which doesn't care); there is no easy fix that would make it work reliably. Commented Dec 22, 2011 at 8:31
0

Mac OSX does not support the tac command. The solution by @tcdyl works when you are calling a single command in a for loop. For all other cases, the following is the simplest way to get around it.

This approach does not support having newlines in your filenames. The underlying reason is, that tail -r sorts its input as delimited by newlines.

for i in `ls -1 [filename pattern] | tail -r`; do [commands here]; done

However, there is a way to get around the newline limitation. If you know your filenames do not contain a certain character (say, '='), then you can use tr to replace all newlines to become this character, and then do the sorting. The result would look as follows:

for i in `find [directory] -name '[filename]' -print0 | tr '\n' '=' | tr '\0' '\n'
| tail -r | tr '\n' '\0' | tr '=' '\n' | xargs -0`; do [commands]; done

Note: depending on your version of tr, it might not support '\0' as a character. This can usually be worked around by changing the locale to C (but I don't remember how exactly, since after fixing it once it now works on my computer). If you get an error message, and you cannot find the workaround, then please post it as a comment, so I can help you troubleshoot it.

-1

an easy way is using ls -ras mentioned by @David Schwartz

for f in $(ls -r /var/logs/foo*.log); do
    bar "$f"
done
-4

Try this:

for f in /var/logs/foo*.log; do
bar "$f"
done

I think it is the most simple way.

1
  • 3
    The question asked for “for loop in reverse order”.
    – manatwork
    Commented Oct 9, 2013 at 7:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .