Find all files in a directory that are not directories themselves

Viewed 48810

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.

8 Answers

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
Related