Bash – Find files alphabetically before a given string

bashfilter

If I have a directory full of files and sub directories. What is the best way to list just the regular files which fall alphabetically before a given string?

Currently the best I can do using bash is the following:

for x in `find . -maxdepth 1 -type f | sort`
do
   if [[ "$x" > './reference' ]]
   then
      break
   fi

   echo $x
done

I feel like there is a more concise way to do this, but I'm not sure what it is. Any ideas?

Best Answer

if you need all of them

 find . -maxdepth 1 -type f | sort |  awk '$0 > "./reference"'

if you need the first

 find . -maxdepth 1 -type f | sort |  awk '$0 > "./reference"{print;exit}'
Related Question