Skip to content

Instantly share code, notes, and snippets.

@ethanpost
Created August 8, 2018 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethanpost/1845ff526bdd6ca3dbc81fd8f97dd71b to your computer and use it in GitHub Desktop.
Save ethanpost/1845ff526bdd6ca3dbc81fd8f97dd71b to your computer and use it in GitHub Desktop.
Bash function returns files only from a directory. Can return full path. Does not return files in sub-directories. Uses find so it can handle a large number of files.
function file_list_files {
# Return the full path to the files in a directory. Does not include subdirectories.
# >>> file_list_files [-l] "directory"
# -l: Return full path (long listing).
typeset directory full_path file
if [[ "${1}" == "-l" ]]; then
shift && full_path=1
else
full_path=0
fi
directory="${1}"
if (( ${full_path} )); then
find "${directory}/." -name . -o -type d -prune -o -print | sed 's/\/\.\//\//'
else
while read file; do
[[ -f "${directory}/${file}" ]] && echo "${file}"
done < <(ls "${directory}")
fi
}
@ethanpost
Copy link
Author

Oh, works on Solaris and perhaps other OS's. Doesn't use maxdepth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment