find files their name is smaller or greater than a given parameter
Solution 1:
Never ever iterate over ls
output!
Here's my suggestion:
for fn in *; do test "$fn" -$1 "$2" && echo "$fn"; done
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="<"
or op=">"
depending on $1 is lt
or gt
, respectively.
Solution 2:
Unfortunately for this technique, /usr/bin/test
doesn't support STRING > STRING
, however the shell builtin test
does so we have to invoke the shell in order to be able to use find -exec
and avoid a loop:
find $PWD -type f -exec sh -c 'test "{}" "<" "$PWD/N00.P004"' \; -print
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
and lt
instead of having to pass quoted <
or >
:
my_finder () {
local op=$1
case "$op" in
"gt") op='>';;
"lt") op='<';;
*) echo "Invalid operator"; return 1;;
esac
find $PWD -type f -exec sh -c 'test "{}" "$op" "$PWD/$2"' \; -print
}
Usage:
$ my_finder gt N00.P003
/home/tzury/Desktop/sandbox/N00.P004
/home/tzury/Desktop/sandbox/N01.P000
/home/tzury/Desktop/sandbox/N01.P001
/home/tzury/Desktop/sandbox/N01.P002
Solution 3:
for num in {001..003} ;do ls N00.P"$num"; done
Replace 003 with limit you want to put.