Translate

Archives

Case Insensitive File Listings

In general, Linux filenames are case-sensitive. So how case you list the files in your current directory in a case-insensitive way?

Technically, you are looking to perform case-insensitive globbing.

Here are some ways:

# touch DEMO demo DeMo
# ls -l [Dd][Ee][Mm][Oo]
-rw-r--r--. 1 root root 0 Mar 24 11:39 demo
-rw-r--r--. 1 root root 0 Mar 24 11:39 DeMo
-rw-r--r--. 1 root root 0 Mar 24 11:39 DEMO

# ls -l | grep -i demo
-rw-r--r--. 1 root root 0 Mar 24 11:39 demo
-rw-r--r--. 1 root root 0 Mar 24 11:39 DeMo
-rw-r--r--. 1 root root 0 Mar 24 11:39 DEMO

# ls -l ~(i:demo)
-rw-r--r--. 1 root root 0 Mar 24 11:39 demo
-rw-r--r--. 1 root root 0 Mar 24 11:39 DeMo
-rw-r--r--. 1 root root 0 Mar 24 11:39 DEMO
#


The first method works on all modern shells and is very efficient. The second method invokes another process in most shells, and the third method only works in the Korn shell.

The zsh shell has the CASE_GLOB and EXTENDED_GLOB options which perform a similar function. Refer to the zsh manpages for more details.

Another method is to use the find utility with the iname option:

find . -maxdepth 1 -iname 'demo' -ls
131632    0 -rw-r--r--   1 root     root            0 Mar 24 11:39 ./DeMo
131622    0 -rw-r--r--   1 root     root            0 Mar 24 11:39 ./demo
130963    0 -rw-r--r--   1 root     root            0 Mar 24 11:39 ./DEMO

Comments are closed.