Sunday, 22 January 2023

How to run ls command case insensitive mode on Linux/Unix

Sometimes you need to use the ls command on your Linux or Unix machine and match the pattern, interpreting upper- and lowercase letters as the same. In other words, I tell ls to find and list files regardless of their case insensitive. For instance, I am going to tell ls command command to ignore the case so that it can match files such as:
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

  1. Open the terminal application
  2. Type the shopt command to enable nocaseglob:
  • shopt -s nocaseglob
  • Now use the ls command. For example, ignore case distinctions in filenames and match all file cases (upper and lower cases):
  • ls *pattern*
    ls -l *pattern*
  • ls -l *service*.pdf

  • Disable the nocaseglob option:
    1. shopt -u nocaseglob
    How to run ls command case insensitive mode on Linux or Unix bash config

    When the nocaseglob bash option is set, the ls or any other Linux and Unix command will match filenames in a case-insensitive fashion when performing filename expansion.

    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