linux - Using 'find' to return filenames without extension

ID : 274477

viewed : 56

Tags : linuxbashshellunixlinux





Top 5 Answer for linux - Using 'find' to return filenames without extension

vote vote

90

To return only filenames without the extension, try:

find . -type f -iname "*.ipynb" -execdir sh -c 'printf "%s\n" "${0%.*}"' {} ';' 

or (omitting -type f from now on):

find "$PWD" -iname "*.ipynb" -execdir basename {} .ipynb ';' 

or:

find . -iname "*.ipynb" -exec basename {} .ipynb ';' 

or:

find . -iname "*.ipynb" | sed "s/.*\///; s/\.ipynb//" 

however invoking basename on each file can be inefficient, so @CharlesDuffy suggestion is:

find . -iname '*.ipynb' -exec bash -c 'printf "%s\n" "${@%.*}"' _ {} + 

or:

find . -iname '*.ipynb' -execdir basename -s '.sh' {} + 

Using + means that we're passing multiple files to each bash instance, so if the whole list fits into a single command line, we call bash only once.


To print full path and filename (without extension) in the same line, try:

find . -iname "*.ipynb" -exec sh -c 'printf "%s\n" "${0%.*}"' {} ';' 

or:

find "$PWD" -iname "*.ipynb" -print | grep -o "[^\.]\+" 

To print full path and filename on separate lines:

find "$PWD" -iname "*.ipynb" -exec dirname "{}" ';' -exec basename "{}" .ipynb ';' 
vote vote

85

Here's a simple solution:

find . -type f -iname "*.ipynb" | sed 's/\.ipynb$//1' 
vote vote

76

I found this in a bash oneliner that simplifies the process without using find

for n in *.ipynb; do echo "${n%.ipynb}"; done 
vote vote

70

If you need to have the name with directory but without the extension :

find .  -type f -iname "*.ipynb" -exec sh -c 'f=$(basename $1 .ipynb);d=$(dirname $1);echo "$d/$f"' sh {} \; 
vote vote

59

If there's no occurrence of this ".ipynb" string on any file name other than a suffix, then you can try this simpler way using tr:

find . -type f -iname "*.ipynb" -print | tr -d ".ipbyn" 

Top 3 video Explaining linux - Using 'find' to return filenames without extension







Related QUESTION?