Find all files in a directory that are not directories themselves
46,358
Solution 1
If you want test.log
, test2.log
, and file2
then:
find . -type f
If you do not want file2
then:
find . -maxdepth 1 -type f
Solution 2
If you need symlinks, pipes, device files and other specific elements of file system to be listed too, you should use:
find -maxdepth 1 -not -type d
This will list everything except directories.
Solution 3
using find is simple as:
find . -maxdepth 1 -type f
Related videos on Youtube
Author by
Alex
Programming pretty much sums up what I do with my life. Favorite Language: C++ Favorite Scripting Language: Python Favorite Editor: vim Favorite Food: Lasanga Favorite Person: Wife
Updated on July 09, 2022Comments
-
Alex almost 2 years
I am looking for a way to list all the files in a directory excluding directories themselves, and the files in those sub-directories.
So if I have:
./test.log ./test2.log ./directory ./directory/file2
I want a command that returns: ./test.log ./test2.log and nothing else.
-
amrox almost 15 yearsYou are right, misread the question. John Kugelman posted a more complete answer.
-
sherrellbc about 3 yearsThis is exactly what I was looking for. I wanted to list all types except directories. I was doing something like
find / -group xxx | xargs ls -l
. This wouldls -l <dir>
and thenls -l <file>
each file in the dir. But using-not -type d
excluded dirs from the output. Note that! -type d
also works (and claims to be POSIX compliant per the man page)