Say that in a given directory I got
tzury@x200:~/Desktop/sandbox$ ls -l
total 20
drwxr-xr-x 2 tzury tzury 4096 2011-03-09 10:19 N00.P000
drwxr-xr-x 2 tzury tzury 4096 2011-03-09 10:19 N00.P001
drwxr-xr-x 2 tzury tzury 4096 2011-03-09 10:19 N00.P002
drwxr-xr-x 2 tzury tzury 4096 2011-03-09 10:19 N00.P003
drwxr-xr-x 2 tzury tzury 4096 2011-03-09 10:19 N00.P004
drwxr-xr-x 2 tzury tzury 4096 2011-03-09 10:19 N01.P000
drwxr-xr-x 2 tzury tzury 4096 2011-03-09 10:19 N01.P001
drwxr-xr-x 2 tzury tzury 4096 2011-03-09 10:19 N01.P002
I seek for a bash way to grab the list of files which their name is either grater or smaller than a given parameter, for instance:
$ my_finder lt N00.P003
shall return N00.P000
, N00.P001
and N00.P002
$ my_finder gt N00.P003
shall return N00.P004
, N01.P000
, N01.P001
and N01.P002
I was thinking of iterating over for name in $(ls)
and while $name != $2
but believe there are more elegant way of doing so
Never ever iterate over
ls
output!Here's my suggestion:
Edit:
Sorry. The above works only if $fn and $2 are numeric. You'll have to replace -$1 with $op, and prepend a selector in front of the loop.
op="<"
orop=">"
depending on $1 islt
orgt
, respectively.Unfortunately for this technique,
/usr/bin/test
doesn't supportSTRING > STRING
, however the shell builtintest
does so we have to invoke the shell in order to be able to usefind -exec
and avoid a loop:The question remains whether spawning a shell repeatedly is more efficient than running a loop. However, the chief advantage to using this technique is that you can do recursive processing without a pipe.
You can create a function that uses this technique and allows you to use
gt
andlt
instead of having to pass quoted<
or>
:Usage:
Replace 003 with limit you want to put.