Skip to content

Instantly share code, notes, and snippets.

@i97506051502
Created June 4, 2017 22:50
Show Gist options
  • Save i97506051502/ce18919bb6b75d21484696b68e387433 to your computer and use it in GitHub Desktop.
Save i97506051502/ce18919bb6b75d21484696b68e387433 to your computer and use it in GitHub Desktop.
listing all file extensions under directory that is assigned by first argument.
#!/bin/bash
set -ue
echo -n '' > /tmp/tmp.extensions.list
if [[ ${#} -ne 1 ]]; then
echo 'Usage:'
echo '$ bash listing_all_file_extensions.sh /path/to/directory'
else
while read directory; do
# 方法その一
while read file_path; do
file_extension="${file_path##*.}"
# 拡張子がないファイルはフルパスが返ってくるのでリストには載せない。
echo "${file_extension}" | grep -v '^/' >> /tmp/tmp.extensions.list
done < <(find "${directory}" -type f)
# 方法その二
# 拡張子がないファイルはフルパスが返ってくるのでリストには載せない。
# find "${directory}" -type f | sed 's/^.*\.\([^\.]*\)$/\1/' | grep -v '^/' >> /tmp/extensions.list
done < <(find "${1}" -type d)
fi
sort /tmp/tmp.extensions.list | uniq > /tmp/extensions.list
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment