Bash extracting file basename from long path
It's better to use an array to hold the filenames. A string variable will not handle filenames which contain spaces.
Also, you don't need to use the basename
command. Instead use bash's built-in string manipulation.
Try this:
files=( /very/long/path/to/various/files/*.file )for file in "${files[@]}"do filename="${file##*/}" filenameWithoutExtension="${filename%.*}" echo "$filenameWithoutExtension"done
Solution with basename
for your question
for file in $filesdo file_name=$(basename $file) file_name_witout_ext=${file_name%.file}done
edit (generic way to extract filename without (single) extension)
for file in $filesdo file_name=$(basename $file) file_name_witout_ext=${file_name%.*}done
Another thing that can happen is to have filename like "archive.tar.gz". In this case you will have two (or multiple extension). You can then use a more greddy operator
for file in $filesdo file_name=$(basename $file) file_name_witout_ext=${file_name%%.*}done