Super User is a question and answer site for computer enthusiasts and power users. 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

ls on a dotfile

My directory contains:

$ ls -A    
.dotfile20091021   file20091020       file20091021

I want to list ALL files containing 20091021 in their name.

If I run:

$ ls -A *20091021*    

If I run:

$ ls -A .*20091021*    

Can I get both files in a single "ls" command?

share|improve this question

migrated from Oct 21 '09 at 10:58

This question came from our site for professional and enthusiast programmers.

Ever heard of 'grep' ? – Tim Post Oct 21 '09 at 10:43
up vote 1 down vote accepted

According to this link there is a bash option to glob dot files:

shopt -s dotglob

If you use bash then all you would need to do after that is:

ls -A *20091021*

(Cannot test it here)

share|improve this answer
ls -A1 | grep "20091021"
share|improve this answer
I thinkg this is perhaps the best one, as it only relies on the sstandard ls output, and the grep tool, both of which are widely available. JF – jfmessier Oct 21 '09 at 16:45

You can simply do

ls -a *20091021 .*20091021

(Sticking strictly to your request that you want to do this in a single ls)

share|improve this answer
This relies on the shell for glob expansion. – Tim Post Oct 21 '09 at 10:55
/bin/sh can point to many things, from distro to distro :) – Tim Post Oct 21 '09 at 10:55
It's not so much that it might not work so much as it might give errors if the glob doesn't match anything. – Matthew Scharley Oct 21 '09 at 11:00

You are relying on (whatever shell you are using) to do glob expansion.

ls -lah | grep \.foo

.. might help you sort that out in a portable way.

For instance:

user@tower:~ ls -lah | grep \.mozilla
drwx------  3 root root 4.0K 2007-11-14 06:17 .mozilla

Behold, the power of pipes :) Expanding that is an exercise for the reader. shopt is not portable and on systems that use a strictly POSIX shell (such as dash) it breaks. Better to write it simply, and portably .. or you'll upload your tried and true script to a box running some other distro and watch it break, horribly.

NB, each shell does glob expansion in its own way, broadly determined by its license. Please, please, please avoid bashisms. Ubuntu spent tens of thousands of dollars getting rid of those in their product.

share|improve this answer

Your Answer


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.