joanx737
joanx737

Reputation: 367

Load filenames into array without directory name or extension

I'm trying to load filenames to an array in bash. Currently, I'm using:

testarr=(csv/*.csv)

Which is giving me elements like

csv/filename.csv

How can I add only 'filename' to the array?

Thanks!

Upvotes: 2

Views: 640

Answers (2)

Fredi
Fredi

Reputation: 34

This can be done also using basename -a:

testarr=($(basename -a csv/*.csv))

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295579

Easiest thing is first to add the entire name, then strip the parts you don't want using parameter expansion:

testarr=( csv/*.csv )            # load literal filenames
testarr=( "${testarr[@]##*/}" )  # strip off directory names
testarr=( "${testarr[@]%.csv}" ) # strip off extensions

Upvotes: 6

Related Questions