17 Ways to Using ‘find’ to return filenames without extension in Linux

I found many ways to return the almost same results. I just note down here as my cheatsheet to use next time.

find . -name "*.sql" -execdir sh -c 'printf "%s\n" "${0%.*}"' {} ';'
find "$PWD" -type f -iname "*.sql" -execdir basename {} .sql ';'
find . -type f -iname "*.sql" -exec basename {} .sql ';'
find . -name '*.sql' -exec bash -c 'printf "%s\n" "${@%.*}"' _ {} +
find . -iname '*.sql' -execdir basename -s '.sh' {} +
find . -name "*.sql" -exec sh -c 'printf "%s\n" "${0%.*}"' {} ';'
find "$PWD" -type f -iname "*.sql" -print | grep -o "[^\.]\+"
find "$PWD" -type f -iname "*.sql" -exec dirname "{}" ';' -exec basename "{}" .sql ';'
find . -type f -iname "*.sql" | sed 's/\.sql$//1'
find .  -type f -iname "*.sql" -exec sh -c 'f=$(basename $1 .sql);d=$(dirname $1);echo "$d/$f"' sh {} \;
find . -type f -iname "*.sql" | grep -oP '.*(?=[.])'
find . | perl -a -F/ -lne 'print $F[-1] if /.*.sql/g'
find . -type f -iname "*.sql" -print | tr -d ".sql"
find . -type f -exec basename {} \;|perl -pe 's/(.*)\..*$/$1/;s{^.*/}{}'
find . -type f -exec basename {} \;|perl -pe 's/(.*)\..*$/$1/;s{^.*/}{}'|sort|uniq
find . -type f -iname '*.sql' -exec basename -s '.sql' {} +
for n in *.sql; do echo "${n%.sql}"; done

Tags:

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.