0

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*    
file20091021

If I run:

$ ls -A .*20091021*    
.dotfile20091021

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

1
  • Ever heard of 'grep' ?
    – Tim Post
    Oct 21, 2009 at 10:43

4 Answers 4

1

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)

4
ls -A1 | grep "20091021"
1
  • 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, 2009 at 16:45
2

You can simply do

ls -a *20091021 .*20091021

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

3
  • 1
    This relies on the shell for glob expansion.
    – Tim Post
    Oct 21, 2009 at 10:55
  • /bin/sh can point to many things, from distro to distro :)
    – Tim Post
    Oct 21, 2009 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. Oct 21, 2009 at 11:00
0

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
user@tower:~    

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .