Take the 2-minute tour ×
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.

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

6 Answers 6

up vote 24 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

and then your command.

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

Alternatively, with zsh:

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

(that is turn case insensitive globbing on 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
4  
@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

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

You can use grep -i for case insensitive matching like this:

ls | grep -i '*abc*'

share|improve this answer
1  
This won't work if the filename has some "funky" characters in it like newline (yes it can happen!). It's almost always a bad idea to parse the output of ls –  rahmu Sep 21 '12 at 8:56
3  
Have you even try to do ls | grep -i '*abc*', because it does not work. –  jfgagne Sep 21 '12 at 18:09
    
jfgagne is quite correct, grep matches patterns not globs, so it'd need to be grep -i '^.*abc.*$' or the less-silly grep -i 'abc'. But follow rahmu's advice, and don't parse ls's output. –  derobert Sep 21 '12 at 18:39

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

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

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

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

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.