-1

I'm writing a script that takes an argument which is a directory .
i want to be able to construct list/array with all the files that have a certain extension in that directory and cut their extension . For example if i have directory containing :

  • aaa.xx
  • bbb.yy
  • ccc.xx

and im searching for *.xx .
my list/array would be : aaa ccc.
I'm trying to use the code in this thread example the accepted answer .

set tests_list=[]

for f in $1/*.bpt
do
   echo $f
   if [[ ! -f "$f" ]]
   then
      continue
   fi
   set tmp=echo $f | cut -d"." -f1
   #echo $tmp
   tests_list+=$tmp                                                         
done

echo ${tests_list[@]}

if i run this script i get that the loop only executes once with $f is tests_list=[]/*.bpt which is weird since $f should be a file name in that directory , and echo empty string.
i validated that i'm in the correct directory and that the argument directory have files with .bpt extensions .

Community
  • 1
  • 1
saeed hardan
  • 485
  • 2
  • 11
  • 24
  • possible duplicate of [How to iterate over files in directory with bash?](http://stackoverflow.com/questions/20796200/how-to-iterate-over-files-in-directory-with-bash) – tripleee Jan 14 '15 at 14:13
  • its not duplicate , did you read the whole question ? i need to build list with only the names of the files and cut all extensions . – saeed hardan Jan 14 '15 at 14:15

2 Answers2

3

This should work for you:

for file in *.xx ; do echo "${file%.*}" ; done

To expand this to a script that takes an argument as a directory:

#!/bin/bash

dir="$1"
ext='xx'

for file in "$dir"/*."$ext"
do
    echo "${file%.*}"
done

edit: switched ls with for - thanks @tripleee for the correction.

pgl
  • 7,551
  • 2
  • 23
  • 31
  • The `ls` is [useless](http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat) or [worse](http://mywiki.wooledge.org/ParsingLs). You mean `for file in *.xx` and `for file in "$dir"/*."$ext"` – tripleee Jan 14 '15 at 14:10
  • `for file in *.xx` will split filenames with spaces in them, so no I don't mean either of your suggestions. But, I would say `find` would be better. Also, you linked to useless use of cat, what's the relevance..? – pgl Jan 14 '15 at 14:35
  • That is completely incorrect. It will loop over the matching filenames one by one. If you experience a problem, is is because you fail to quote `"$file"` properly inside the loop (but you have proper quoting in your examples). – tripleee Jan 14 '15 at 14:48
  • UULS is easy to lump with UUCA and other similar antipatterns. But here, then: http://porkmail.org/era/unix/award.html#ls – tripleee Jan 14 '15 at 14:50
  • @tripleee - You are right, sorry! Apologies. I really did think `for ...` split words for some reason, I stand corrected. – pgl Jan 14 '15 at 15:23
1
filear=($(find path/ -name "*\.xx"))
filears=()
for f in ${filear[@]}; do filears[${#filears[@]}]=${f%\.*}; done 
Marc Bredt
  • 905
  • 5
  • 13
  • it kinda worked , but my files have spaces ,so echo ${filears[1]} prints a word in a file name not the whole file name . – saeed hardan Jan 13 '15 at 11:22
  • `${f%\.*}` just strips the file ending use $f to get the whole filename stored. for the space problem just change the IFS=$'\n' or whatever fits your needs. – Marc Bredt Jan 13 '15 at 11:38