Discussion Locked

piping ls -l to grep to return directies

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,
I am not a seasoned Unix user, and here is the task at hand that I've been working on. This is an instuction I've been asked to perform (Question posed by instructor):
To obtain a long directory listing of current directy and pipe to grep, and ONLY return directory listings.

..Here's what I've come up with... and I'm wondering if I'm way out there in left field>
ls -l |grep -v [-][r-][w-][e-][r-][w-][e-][r-][w-][e-]

The reason I selected to filter for any rows beginning with the -, is I tested to see if I could create a file name starting with - and I couldn't *but* I could create files named drwxrwxrwx and d-wx-wxr-x etc etc... but I do get an additional line telling me how many entries were in the directory listing. Naturally I could pipe to another filter...*but* I'm rather expecting that, that was not in the instructor's scheme of things ;-)...

Is there syntax I can use with grep to specify that the d is the first and only character in a line? I have read that UNIX doesn't look at 's or .. so..? :-)

Thanks any help is appreciated!

3 replies

by ken.cunningham

ls -l | grep ^d

seems to do what you want (ie anything with a 'd' at the beginning of the line (as denoted by the caret ^))

Hope this suffices. Best wishes.

by carol

Thank you so much!!

I'd read limited help on use of the carat, and did not realize it would search for start of line only which is exactly what I want :-)
-Yepitsme

This discussion is closed to new replies.