How to get the list of files in a directory in a shell script?
In addition to the most-upvoted answer by @Ignacio Vazquez-Abrams, consider the following solutions which also all work, depending on what you are trying to do. Note that you can replace "path/to/some/dir"
with .
in order to search in the current directory.
1. List different types of files using find
and ls
References:
- For
find
, see this answer. See also my comment here.
- For
ls
, see linuxhandbook.com: How to List Only Directories in Linux
Tip: for any of the find
examples below, you can pipe the output to sort -V
if you'd like it sorted.
Example:
find . -maxdepth 1 -type f | sort -V
List only regular files (-type f
) 1 level deep:
# General form
find "path/to/some/dir" -maxdepth 1 -type f
# In current directory
find . -maxdepth 1 -type f
List only symbolic links (-type l
) 1 level deep:
# General form
find "path/to/some/dir" -maxdepth 1 -type l
# In current directory
find . -maxdepth 1 -type l
List only directories (-type d
) 1 level deep:
Note that for the find
example here, we also add -mindepth 1
in order to exclude the current directory, .
, which would be printed as .
at the top of the directory list otherwise. See here: How to exclude this / current / dot folder from find "type d"
# General form
find "path/to/some/dir" -mindepth 1 -maxdepth 1 -type d
# In current directory
find . -mindepth 1 -maxdepth 1 -type d
# OR, using `ls`:
ls -d
Combine some of the above: list only regular files and symbolic links (-type f,l
) 1 level deep:
Use a comma (,
) to separate arguments to -type
:
# General form
find "path/to/some/dir" -maxdepth 1 -type f,l
# In current directory
find . -maxdepth 1 -type f,l
2. Capture the output of any command into a bash indexed array, with elements separated by the newline char (\n
)
However, $search_dir
contains many files with whitespaces in their names. In that case, this script does not run as expected.
This is solved by telling bash to separate elements in the string based on the newline char \n
instead of the space char--which is the default IFS
(Internal Field Separator--see The Meaning of IFS
in Bash Scripting) variable used by bash. To do this, I recommend using the mapfile
command.
The bash script static code analyzer tool named shellscript
recommends using mapfile
or read -r
whenever you want to read in a string into a bash array, separating elements based on the newline char (\n
). See: https://github.com/koalaman/shellcheck/wiki/SC2206.
Update: to see examples of how to do this with both mapfile
and read -r
see my answer here: How to read a multi-line string into a regular bash "indexed" array. I now prefer to use read -r
instead of mapfile
, because mapfile
will KEEP any empty lines as elements in the array, if any exist, which I do NOT want, whereas read -r
[again, my preference now] will NOT keep empty lines as elements in the array.
(Back to my original answer:)
Here is how to convert a newline-separated string into a regular bash "indexed" array with the mapfile
command.
# Capture the output of `ls -1` into a regular bash "indexed" array.
# - includes both files AND directories!
mapfile -t allfilenames_array <<< "$(ls -1)"
# Capture the output of `find` into a regular bash "indexed" array
# - includes directories ONLY!
# Note: for other `-type` options, see `man find`.
mapfile -t dirnames_array \
<<< "$(find . -mindepth 1 -maxdepth 1 -type d | sort -V)"
Notes:
- We use
ls -1
(that's a "dash numeral_one") in order to put each filename on its own line, thereby separating them all by the newline \n
char.
- If you'd like to Google it,
<<<
is called a "here string" in bash.
- See
mapfile --help
, or help mapfile
, for help.
Full code example:
From file array_list_all_files_and_directories.sh in my eRCaGuy_hello_world repo:
echo "Output of 'ls -1'"
echo "-----------------"
ls -1
echo ""
# Capture the output of `ls -1` into a regular bash "indexed" array.
# - includes both files AND directories!
mapfile -t allfilenames_array <<< "$(ls -1)"
# Capture the output of `find` into a regular bash "indexed" array
# - includes directories ONLY!
# Note: for other `-type` options, see `man find` and see my answer here:
# https://stackoverflow.com/a/71345102/4561887
mapfile -t dirnames_array \
<<< "$(find . -mindepth 1 -maxdepth 1 -type d | sort -V)"
# Get the number of elements in each array
allfilenames_array_len="${#allfilenames_array[@]}"
dirnames_array_len="${#dirnames_array[@]}"
# 1. Now manually print all elements in each array
echo "All filenames (files AND dirs) (count = $allfilenames_array_len):"
for filename in "${allfilenames_array[@]}"; do
echo " $filename"
done
echo "Dirnames ONLY (count = $dirnames_array_len):"
for dirname in "${dirnames_array[@]}"; do
# remove the `./` from the beginning of each dirname
dirname="$(basename "$dirname")"
echo " $dirname"
done
echo ""
# OR, 2. manually print the index number followed by all elements in the array
echo "All filenames (files AND dirs) (count = $allfilenames_array_len):"
for i in "${!allfilenames_array[@]}"; do
printf " %3i: %s\n" "$i" "${allfilenames_array["$i"]}"
done
echo "Dirnames ONLY (count = $dirnames_array_len):"
for i in "${!dirnames_array[@]}"; do
# remove the `./` from the beginning of each dirname
dirname="$(basename "${dirnames_array["$i"]}")"
printf " %3i: %s\n" "$i" "$dirname"
done
echo ""
Here is the example output of the code block just above being run inside the eRCaGuy_hello_world/python dir of my eRCaGuy_hello_world repo:
eRCaGuy_hello_world/python$ ../bash/array_list_all_files_and_directories.sh
Output of 'ls -1'
-----------------
autogenerate_c_or_cpp_code.py
autogenerated
auto_white_balance_img.py
enum_practice.py
raw_bytes_practice.py
slots_practice
socket_talk_to_ethernet_device.py
textwrap_practice_1.py
yaml_import
All filenames (files AND dirs) (count = 9):
autogenerate_c_or_cpp_code.py
autogenerated
auto_white_balance_img.py
enum_practice.py
raw_bytes_practice.py
slots_practice
socket_talk_to_ethernet_device.py
textwrap_practice_1.py
yaml_import
Dirnames ONLY (count = 3):
autogenerated
slots_practice
yaml_import
All filenames (files AND dirs) (count = 9):
0: autogenerate_c_or_cpp_code.py
1: autogenerated
2: auto_white_balance_img.py
3: enum_practice.py
4: raw_bytes_practice.py
5: slots_practice
6: socket_talk_to_ethernet_device.py
7: textwrap_practice_1.py
8: yaml_import
Dirnames ONLY (count = 3):
0: autogenerated
1: slots_practice
2: yaml_import