How to match case insensitive patterns with ls?

I would like to list all files matching a certain pattern while ignoring the case.

For example, I run the following commands:

ls *abc*

I want to see all the files that have “abc” as a part of the file name, ignoring the case, like

-rw-r--r-- 1 mtk mtk 0 Sep 21 08:12 file1abc.txt
-rw-r--r-- 1 mtk mtk 0 Sep 21 08:12 file2ABC.txt

Note

I have searched the man page for case, but couldn’t find anything.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is actually done by your shell, not by ls.

In bash, you’d use:

shopt -s nocaseglob

and then run your command.

Or in zsh:

unsetopt CASE_GLOB

Or in yash:

set +o case-glob

and then your command.

You might want to put that into .bashrc, .zshrc or .yashrc, respectively.

Alternatively, with zsh:

setopt extendedglob
ls -d -- (#i)*abc*

(that is turn case insensitive globbing on a per-wildcard basis)

With ksh93:

ls -d -- ~(i:*abc*)

You want globbing to work different, not ls, as those are all files passed to ls by the shell.

Method 2

As explained by polemon, it is the shell (not ls) that extends *abc* to a list of files. This is called Pattern Matching.

Aside from changing the whole Pattern Matching behavior to ignore case, you could use another form of pattern matching than the *. The following would do what you want in bash:

ls *[aA][bB][cC]*

From bash man:

[…] Matches any one of the enclosed characters.

This allows more fine grain matching where you could use *[aA][bB]c* to match abc or ABc but not abC or ABC. Or an example in French, where I could want to match all instances of the e character:

ls *[eéèêëEÉÈÊË]*

Method 3

You can also add -i (–ignore-case) option to grep to get and the below output.

[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dffe2e2f9cde1e2eeece1e5e2fef9">[email protected]</a> ~]# ls -l | grep -i abc
-rw-r--r--  1 root root    0 Feb 25 20:41 fileabc.txt
-rw-r--r--  1 root root    0 Feb 25 20:41 fileABC.txt

Method 4

I don’t think this is possible with ls, but you could use find instead:

find . -maxdepth 1 -iname '*abc*'

Method 5

You can GREP the file using -i which adds the insensitive option.

For example to find both abc and ABC you can run this command

    [<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71031e1e05311c051a">[email protected]</a> bin]# ls -l | grep -i abc
    640K -rw-r--r-- 1 mtk mtk 639K Sep 21 08:12 file1abc.txt
    676K -rw-r--r-- 1 mtk mtk 674K Sep 21 08:12 file2ABC.txt

Method 6

use the following code:

LC_COLLATE=en_US ; export LC_COLLATE
ls *abc*
LC_COLLATE=C ; export LC_COLLATE

Method 7

In tcsh, set an alias in .aliases

alias lsnc "ls BS!* | sort -f "

(“BS” = one “backslash”; I had to use this to get it to show up in my browser.)

and use lsnc instead of ls

In bash, try an alias in .bash_aliases like

lsnc() { ls $1 | sort -f ; }

With options to ls, e.g., ls -l, here is a simple fix:

tcsh:

alias lsncl "ls -l BS!* | sort -f -k 9"

bash:

lsncl() { ls -l $1 | sort -f -k 9 ; }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x