How to get a linux directory listing for files beginning with a certain letter that doesn't descend into subdirectories?

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?

5 Answers

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

I believe another interesting solution to be,

ls | grep ^d

Offers the flexibility of regular expressions.

4

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

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

1

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.

1

ls -a | grep "^."

ls -a: it will demonstrate all files and folders (hidden files and unhidden files together)

grep "^.": it will filter the result, and it will choose to show you just the files starts with a point.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like