33

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

ls .

^ will include directories in the listing.

8 Answers 8

31

Try this one:

find . -maxdepth 1 -not -type d
5
  • 6
    keep it simple: $ find . -type f
    – jamzed
    Mar 10, 2012 at 19:14
  • 11
    No. (1) it would be recursive, (2) it would not list symlinks. @Steve asked for equivalent of ls . without directories — and here it is. Mar 10, 2012 at 19:20
  • 2
    Adding -ls or -exec ls -l {} \; would make it like ls -l without directories.
    – Ladadadada
    Mar 10, 2012 at 20:06
  • find . -maxdepth 1 -type f -o -type l could also add includes of symlinks beside files. Jul 29, 2020 at 3:47
  • This was perfect for my needs except for leading ./ in the listing, but easily removed with cut -c 3-
    – SAFX
    May 10, 2023 at 16:20
23

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.

2
  • 1
    '-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 :)
    – jirib
    Mar 10, 2012 at 20:30
  • 2
    @JiriXichtkniha what is the POSIX alternative to -not? Sep 18, 2016 at 22:11
5

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.

1
  • This answer was already provided over a year before by jamzed, (making this visual noise.) Mar 24, 2019 at 10:45
5

try this:

$ find . -maxdepth 1 -type f

or this:

$ ls -p | egrep -v /$

$ ls -la | egrep -v ^d

3

The "find" solutions above lose some of the capabilities of ls - for example: list only files, sorted in descending modification time.

The "ls -p | grep" answers do not elegantly deal with other elements of ls such as -R should they be desired

The following answer, while more verbose, in my opinion reflects the truest ls behaviour and most flexibility for selecting "files only"

ls -t . | while read line; do
  if [ -f $line ]; then
    echo $line
  fi 
done

Simply replace the ls switches as desired and the result will be returned with files only. If links and other items are also required then minor rework would need to be done to the test for inclusion.

0
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.

2
  • This doesn't work if any of the filenames to be listed contain spaces.
    – Psychonaut
    Jan 17, 2018 at 15:27
  • ls -t .|while read f;do [ -f "$f" ]&& echo $f;done is about 3x faster in my tests Mar 24, 2019 at 10:43
0

I was on a system where the find binary did not support -not and I ended up using:

ls|while read f;do [ -f "$f" ]&& echo $f;done

Advantages: Does not rely on anything other than sh ls echo
Is compatible with bash and dash
Can be changed to match other things, (such as directories) by changing -f
Is fastest than some of the answers and uses less RAM and CPU

0

ls -pd $PWD/* |grep -v /$ |xargs ls -l --color=always

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .