Created
August 8, 2018 00:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, works on Solaris and perhaps other OS's. Doesn't use maxdepth.