Find all files in a directory that are not directories themselves
Asked Answered
D

8

49

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.

Dejadeject answered 19/8, 2009 at 15:16 Comment(0)
H
83

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
Hurless answered 19/8, 2009 at 15:22 Comment(0)
T
22

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.

Tallahassee answered 27/12, 2015 at 13:7 Comment(1)
This 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 would ls -l <dir> and then ls -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)Prone
N
3

using find is simple as:

find . -maxdepth 1 -type f
Noblenobleman answered 19/8, 2009 at 15:22 Comment(0)
S
1

find only regular files

Use the -type f option with find to find only regular files. OR, to be even more-inclusive, use -not -type d to find all file types except directories.

When listing all files, I like to also sort them by piping to sort -V, like this:

# find only regular files
find . -type f | sort -V

# even more-inclusive: find all file types _except_ directories
find . -not -type d | sort -V

From man find:

-type c

File is of type c:

  • b - block (buffered) special
  • c - character (unbuffered) special
  • d - directory
  • p - named pipe (FIFO)
  • f - regular file
  • l - symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.
  • s - socket
  • D - door (Solaris)

To search for more than one type at once, you can supply the combined list of type letters separated by a comma , (GNU extension).

How to store the output of find (a multi-line string list of files) into a bash array

To take this one step further, here is how to store all filenames into a bash indexed array called filenames_array, so that you can easily pass them to another command:

# obtain a multi-line string of all filenames
filenames="$(find . -type f | sort -V)"
# read the multi-line string into a bash array
IFS=$'\n' read -r -d '' -a filenames_array <<< "$filenames"

# Now, the the variable `filenames_array` is a bash array which contains the list 
# of all files! Each filename is a separate element in the array.

Now you can pass the entire array of filenames to another command, such as echo for example, by using "${filenames_array[@]}" to obtain all elements in the array at once, like this:

echo "${filenames_array[@]}"

OR, you can iterate over each element in the array like this:

echo "Files:"
for filename in "${filenames_array[@]}"; do
    echo "  $filename"
done

Sample output:

Files:
  ./file1.txt
  ./file2.txt
  ./file3.txt

References:

  1. I was reminded of find . -type f from the main answer by @John Kugelman here.
  2. I borrowed the part to read the multi-line string into a bash array from my own answer here: How to read a multi-line string into a regular bash "indexed" array
Sopping answered 30/4, 2022 at 17:41 Comment(0)
T
0
find . -type f
Thesda answered 19/8, 2009 at 15:22 Comment(1)
You are right, misread the question. John Kugelman posted a more complete answer.Thesda
S
0
find /some/directory -type f
Shaughnessy answered 19/8, 2009 at 15:23 Comment(0)
A
0
$ find . -type f -print

Each file will be on its own line. You must be in the directory you want to search.

Agnomen answered 19/8, 2009 at 15:24 Comment(0)
G
-1

One more option

ls -ltr | grep ^-

to list all files, while

ls -ltr | grep ^d

to list all directories

Geryon answered 7/12, 2009 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.