ls *service*.pdf
SERVICE.pdf
DELL-Service-M6700.pdf
hp-printer-Service-Manual.pdf
In this quick tip, I explain how to match case insensitive patterns with the ls command on your Linux, macOS, *BSD and Unix-like system.
How to run ls command case insensitive mode on Linux
The ls command currently doesn’t have a case-insensitive match option. However, the workaround is available with various command line options and shell configs to find files in a case insensitive mode using the ls command.
Bash nocaseglob option to match files in a case-insensitive mode
- Open the terminal application
- Type the shopt command to enable nocaseglob:
shopt -s nocaseglob
ls *pattern*
ls -l *pattern*
ls -l *service*.pdf
shopt -u nocaseglob
ZSH and ls command to list files in case insensitive mode
According to ZSH documentation:
Make globbing (filename generation) sensitive to case. Note that other uses of patterns are always sensitive to case. If the option is unset, the presence of any character which is special to filename generation will cause case-insensitive matching. For example, cvs(/) can match the directory CVS owing to the presence of the globbing flag (unless the option BARE_GLOB_QUAL is unset).
In other words, use the following command on ZSH
unsetopt CAse_glob #<--Make sensitive to file case
ls -l *service*.pdf
setopt CAse_glob #<--Turn it off
Using find command to find files in case-insensitive mode
The find command on Linux, *BSD, and macOS supports finding and listing files using the -iname option. The syntax is:
find /dir/ -iname "*service*.pdf" -ls
OR
find /dir/ -iname "*service*.pdf" -print
The -iname option will match files in case insensitive mode. The -ls option list current file in ls -dils format on screen:
4456842 5752 -rw-rw-r-- 1 vivek vivek 5888599 Aug 23 14:36 ./SERVICE.pdf 4456851 6376 -rw-rw-r-- 1 vivek vivek 6527261 Aug 23 14:36 ./hp-printer-Service-Manual.pdf 4456846 11544 -rw-rw-r-- 1 vivek vivek 11819062 Aug 23 14:36 ./DELL-Service-M6700.pdf
Using ls | grep command to ignore case
The final solution is to use shell pipes. For example, run the ls command and send output to the grep command or egrep command as follows:
ls -l | grep -i "service"
# match service or manual to ignore case distinctions #
ls -l | egrep -i "service|manual"
The -i option is passed to the grep command to perform case insensitive search. The egerp command is used to match either “service” or “manual” words. See about searching multiple words or string pattern using grep/egerp command for more info.
Summing up
This page explained a few options for case-insensitive fashion file
listing when performing filename expansion using bash/zsh and standard
Unix utilities. However, I strongly suggest that you read the following
manual pages using the man command or grep command:
man grep
man bash
man zsh
man find
help shopt
No comments:
Post a Comment