Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 need to see all the file 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 counldn't find anything.

share|improve this question
up vote 34 down vote accepted

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.

share|improve this answer
    
In bash, how to reset to original setting, If I use shport -s nocaseglob ? – mtk Sep 21 '12 at 17:11
6  
@mtk: To set an option, you use shopt -s; to unset it, you use shopt -u. Alternatively, you can wrap everything in a subshell by using ( ) so that the setting doesn't affect the parent shell: (shopt -s nocaseglob ; ls *abc*). – ruakh Sep 21 '12 at 19:19
    
Cool. That perfectly helps. Thanks :) – mtk Sep 22 '12 at 13:54
    
So there isn't any portable ways for this in POSIX or SUS or something like that (except for [aA][bB][cC])? – Timothy Gu Nov 21 '14 at 16:01
    
Excellent solution, I can't believe I hadn't come across shopt command in the last 15 yrs of using bash! – Reza Sanaie May 28 '15 at 19:12

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ÉÈÊË]*
share|improve this answer
    
Unfortunately, that approach quickly becomes unwieldy as the pattern gets longer. But for short things, it works. – derobert Sep 21 '12 at 18:37
    
@derobert: totally true, especially is (and it is probably the case) the shell has optimized the matching ignoring case by putting all the filenames and the match request in lower cases before comparing. However, the [...] can be useful in many cases, and I though it was worth mentioning. – jfgagne Sep 22 '12 at 7:24

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

find . -maxdepth 1 -iname '*abc*'
share|improve this answer

use the following code:

LC_COLLATE=en_US ; export LC_COLLATE
ls *abc*
LC_COLLATE=C ; export LC_COLLATE
share|improve this answer
    
What is the LC_COLLATE variable for ? can you explain further. – mtk Oct 21 '12 at 10:26
    
click on the link to know about this @mtk – user1678213 Nov 4 '12 at 19:08

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

[root@localhost ~]# 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
share|improve this answer

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

    [root@mtk bin]# ls -rlasth | grep -a 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
share|improve this answer
    
Luis Perez I think you haven't used -i option anywhere in you command. – Mongrel Feb 25 at 15:29

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 ; }
share|improve this answer
    
Are you sure you are not missing a '=' after lsnocase? – Anthon Aug 29 '13 at 21:09
    
Anthon: The (revised) lsnc and lsncl work for me under tcsh and bash on both Cygwin and Ubuntu. Note that for bash, I'm using a function, not an "alias" per se. Lester – Lester Ingber Aug 31 '13 at 0:07
    
Anthon: Yes, you likely can use alias in bash, like alias lsncl='ls -l $1 | sort -f -k 9' – Lester Ingber Aug 31 '13 at 0:13
    
This answer does not address the specific question which wants to pass the files passed to ls to be treated as a case-insensitive. If the shell passes passes too few files to ls, there's nothing ls and sort can can to fix the situation. As noted in other answers, the problem needs to solved in the shell, not with an ls syntax. – Mark Stosberg Sep 14 '15 at 18:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.