I have an array and loop printing out the filename+extension, but i just want to print the filename. How can I do that?
tffilearray=(`find ./ -maxdepth 1 -name "*.json"`)
for filepath in "${tffilearray[@]}"; do
echo $filepath
done
I have an array and loop printing out the filename+extension, but i just want to print the filename. How can I do that?
tffilearray=(`find ./ -maxdepth 1 -name "*.json"`)
for filepath in "${tffilearray[@]}"; do
echo $filepath
done
Got it!
for filepath in "${tffilearray[@]}"; do basename $filepath .json; done
As Cyrus pointed out, using a shell wildcard is cleaner than find
(since you don't need to search multiple directories or apply a complex search rule); also, you can apply the shell pattern rule (from the duplicate question) to the entire array at once:
tffilearray=(*.json) # Get all the *full* filenames
tffilearray=("${tffilearray[@]%.*}") # Trim off the extensions
[I'm marking this Community Wiki to avoid points on a mostly-duplicate question.]