Scenario / Questions
How do I exclude directories when listing files in the current directory?
^ will include directories in the listing.
Find below all possible solutions or suggestions for the above questions..
Try this one:
find . -maxdepth 1 -not -type d
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.
$ find . -type f -maxdepth 1
$ ls -p | egrep -v /$
$ ls -la | egrep -v ^d
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.
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.
ls $(file --no-pad -F' ' * | grep -v directory | cut -d' ' -f1)
With this you can still use any other options
ls usually takes.
-v to list only directories.
Or… replace directory with any other filetype that
file understands and reports, like ASCII, empty, ELF, and so on.
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
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
Is fastest than some of the answers and uses less RAM and CPU
Kubernetes Free Online Tutorial, Kubernetes Beginner Tutorial
DevOps Free Online Tutorial, DevOps Beginner Tutorial
Ansible Free Online Tutorial, Ansible Beginner Tutorial
Docker Free Online Tutorial, Docker Beginner Tutorial
Openstack Free Online Tutorial, Openstack Beginner Tutorial
Disclaimer: This has been sourced from a third party syndicated feed through internet. We are not responsibility or liability for its dependability, trustworthiness, reliability and data of the text. We reserves the sole right to alter, delete or remove (without notice) the content in its absolute discretion for any reason whatsoever.