up vote 0 down vote favorite
1
share [g+] share [fb]

Suppose you are in a directory that contains many files and many subdirectories.

You want to get a directory listing of all the files beginning with the letter "d". You type

ls d*

and what you get back is mostly files in sub-directories (in particular, files in subdirectories that begin with "d").

How do you list only the files and directory names in your current directory?

link|improve this question

feedback

3 Answers

up vote 3 down vote accepted

Ah, I just found it on the 6th reading of the man page. It's the not-so-sensibly named "directory" parameter

ls -d d*
link|improve this answer
1  
It took you just 29 seconds to re-read the man page and write up this answer? That's quick :-) – balpha Aug 19 '09 at 18:14
Additionally, ls -d d*/ will only show the directories. – innaM Aug 19 '09 at 19:00
balpha - I discovered it before submitting the question, then realized that if it took me so long to figure this out, I might as well help a future searcher out and post the q and a here. – dggoldst Aug 20 '09 at 12:35
Fair enough; there's nothing wrong with it. – balpha Aug 20 '09 at 12:44
feedback

I believe another interesting solution to be,

ls | grep ^d

Offers the flexibility of regular expressions.

link|improve this answer
1  
And the complexity of regular expressions! ;-) – Chris Nava Aug 19 '09 at 20:02
That is an interesting soln, and I like the idea of using regexs, however, if you add additional parameters to ls it will break. E.g. "ls -l | grep ^d" – dggoldst Aug 20 '09 at 12:41
True, but not all parameters. ls -a | grep "^\." would print all "hidden" files. – Kuer Aug 20 '09 at 13:31
feedback

find . -maxdepth 1 -name d* -type f

Okay, using find here is a tad of overkill. Just a tad.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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