Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 9 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*
share|improve this answer
2  
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
1  
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
    
-d option will only list directories and not files.So above solution won't work –  CppLearner Jun 10 '12 at 5:49
show 1 more comment

I believe another interesting solution to be,

ls | grep ^d

Offers the flexibility of regular expressions.

share|improve this answer
2  
And the complexity of regular expressions! ;-) –  Chris Nava Aug 19 '09 at 20:02
1  
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
add comment

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

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

share|improve this answer
    
But it helped me list all direct directories of a specific user : find /home/ -maxdepth 1 -type d -user www-data. So thanks, and +1 –  Cyril N. Sep 4 '12 at 13:01
add comment

ls -ld: It will give the list of directories, without descending into subdirectories.

Example:

ls -ld Cust*
This command will provide a listing of the files and directories which start with Cust.

share|improve this answer
add comment

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.