-1

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
Jshee
  • 2,620
  • 6
  • 44
  • 60
  • 2
    Btw.: I suggest to remove first line and replace `for filepath in "${tffilearray[@]}"; do` with `for filepath in *.json; do`. – Cyrus Apr 02 '21 at 14:41

2 Answers2

0

Got it!

for filepath in "${tffilearray[@]}"; do  basename $filepath .json; done
Jshee
  • 2,620
  • 6
  • 44
  • 60
  • Please mark the question as duplicate since your answer is already mentioned in the proposed duplcate. – 0stone0 Apr 02 '21 at 14:42
0

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.]

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151