user2338197
user2338197

Reputation: 1

Echo files in a directory case-insensitively?

Currently, in my bash script I print out a list of all the files in a directory. The thing is, all the files starting with a capital are printed first, then the lower case. How can I combine to make one alphabetized list? This is a list of more or less system files that I cannot rename to all be the same case.

EDIT: I need to run an if on each file so that's why I use the loop. I can't just display them all.

example:

for file in *.txt
do
    if grep -Fxq "$file" disabled.dat
  then
     echo -e "$GREEN${file}$NC"
  else
     echo "${file}"
  fi
done

outputs:

Apple.txt
Banana.txt
Pear.txt
aardvark.txt
snake.txt
zebra.txt

I'm new to bash so all help will be greatly appreciated. Thanks!

Upvotes: 0

Views: 123

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799110

Change the collation order used by your operating system.

$ LC_COLLATE=C bash -c 'echo *.txt'
Apple.txt Banana.txt Pear.txt aardvark.txt snake.txt zebra.txt
$ LC_COLLATE=en_US bash -c 'echo *.txt'
aardvark.txt Apple.txt Banana.txt Pear.txt snake.txt zebra.txt

Upvotes: 6

Related Questions