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.
8 Answers 8
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
-
Once you're using positional parameters, there's no need for your variables
i
andf
; just perform ashift
, use1ドル
for your call tobar
, and test on[ -z 1ドル ]
in yourwhile
.user1404316– user14043162018年02月15日 18:22:33 +00:00Commented 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.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2018年02月15日 18:38:01 +00:00Commented 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.user1404316– user14043162018年02月15日 18:42:16 +00:00Commented Feb 15, 2018 at 18:42
Try this, unless you consider line breaks as "funky characters":
ls /var/logs/foo*.log | tac | while read f; do
bar "$f"
done
-
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– user5416862011年12月22日 05:04:53 +00:00Commented 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– Johan2011年12月22日 06:07:18 +00:00Commented Dec 22, 2011 at 6:07
-
6This 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).Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2011年12月22日 08:26:33 +00:00Commented 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 ordinaryfor
line.Serge Stroobandt– Serge Stroobandt2015年07月06日 23:14:26 +00:00Commented Jul 6, 2015 at 23:14 -
That tac command just saved my day, thanks!Budianto IP– Budianto IP2023年10月24日 04:04:48 +00:00Commented Oct 24, 2023 at 4:04
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
-
2I 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}"; donearp– arp2018年01月11日 08:31:51 +00:00Commented 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.ACK_stoverflow– ACK_stoverflow2018年01月11日 21:45:42 +00:00Commented Jan 11, 2018 at 21:45 -
@ACK_stoverflow There are systems out there without Linux userland tools.2018年02月15日 18:21:01 +00:00Commented Feb 15, 2018 at 18:21
-
@ACK_stoverflow That's what I'm saying, yes.2018年02月16日 06:46:54 +00:00Commented Feb 16, 2018 at 6:46
-
Instead of
tac
, just dotail -r
.Taher Ghaleb– Taher Ghaleb2022年02月20日 21:55:07 +00:00Commented Feb 20, 2022 at 21:55
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 orderL
file lengthl
number of linksm
modification daten
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/
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.
-
2+1 great answer. It seems like Ubuntu doesn't support
tail -r
though... am I doing something wrong?user541686– user5416862011年12月22日 06:02:29 +00:00Commented Dec 22, 2011 at 6:02 -
1No, 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, insteadtcdyl– tcdyl2011年12月22日 06:17:58 +00:00Commented Dec 22, 2011 at 6:17 -
I've also edited the answer to include another alternativetcdyl– tcdyl2011年12月22日 07:01:15 +00:00Commented Dec 22, 2011 at 7:01
-
whoops I forgot to +1 when I said +1 :( Done! Sorry about that haha :)user541686– user5416862011年12月22日 07:22:59 +00:00Commented 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 tols
, which doesn't care); there is no easy fix that would make it work reliably.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2011年12月22日 08:31:21 +00:00Commented Dec 22, 2011 at 8:31
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.
an easy way is using ls -r
as mentioned by @David Schwartz
for f in $(ls -r /var/logs/foo*.log); do
bar "$f"
done
-
This is the most simple wayAnton Samokat– Anton Samokat2025年01月11日 21:04:09 +00:00Commented Jan 11 at 21:04
Try this:
for f in /var/logs/foo*.log; do
bar "$f"
done
I think it is the most simple way.
-
4The question asked for "
for
loop in reverse order".manatwork– manatwork2013年10月09日 07:52:26 +00:00Commented Oct 9, 2013 at 7:52
You must log in to answer this question.
Explore related questions
See similar questions with these tags.
sort -r
before thefor
, or launder throughls -r
.