Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I exclude directories when listing files in the current directory?

ls .

^ will include directories in the listing.

share|improve this question

5 Answers 5

up vote 6 down vote accepted

Try this one:

find . -maxdepth 1 -not -type d
share|improve this answer
1  
keep it simple: $ find . -type f –  jamzed Mar 10 '12 at 19:14
5  
No. (1) it would be recursive, (2) it would not list symlinks. @Steve asked for equivalent of ls . without directories — and here it is. –  Alexander Gladysh Mar 10 '12 at 19:20
1  
Adding -ls or -exec ls -l {} \; would make it like ls -l without directories. –  Ladadadada Mar 10 '12 at 20:06

Though it's an old post, but i thought this might help..

$ ls -l |grep -v ^d

It will list all the files including symlinks,character and block files.

share|improve this answer
ls $(file --no-pad  -F' '  * | grep -v directory | cut -d' ' -f1)

With this you can still use any other options ls usually takes. Or... remove -v to list only directories. Or... replace directory with any other filetype that file understands and reports, like ASCII, empty, ELF, and so on.

share|improve this answer

To get it exactly equivalent to ls . you need to not show hidden dirs.

find . -maxdepth 1 -not -type d -and -not -name '.*'

and that still leaves you with './' prefixed to each filename. That's not really an issue, but I think it's kinda ugly. I went with:

ls -p | grep -v '/$'

And that will get you a listing that looks the same, and you can add additional ls arguments too. Add a --color=always and you'll get your dircolors back, or -a to see hidden files.

I like Alexander's answer because he's actually depending on a filesystem characteristic of the file in question so it won't get fooled ever. My answer will get fooled by a file that has a '/' as the last character in it's name. But that seems like it's asking for trouble.

share|improve this answer
    
'-not' is GNU find related, as it is not POSIX compliant, it's better not to teach these options as an user could have "bad" habbits and feel lost on other UNIXes :) –  Jiri Xichtkniha Mar 10 '12 at 20:30

try this:

$ find . -type f -maxdepth 1

or this:

$ ls -p | egrep -v /$

$ ls -la | egrep -v ^d

share|improve this answer

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.