0

I am trying to find and process all files with a given ending (.txt in the example below) in a directory. My current example finds all files containing .txt anywhere in the file name (e.g. also files with the ending .txt*, e.g. .txt.xls).

DATADIR=$1

for DATA in `ls $DATADIR`; do
  DATABASENAME=$(basename $DATA)
  echo "Basename of file $DATABASENAME"

  if [[ ${DATABASENAME} =~ .*txt ]];
  then
    DATAPATH="$DATADIR$DATABASENAME"

    echo "File path $DATAPATH"
  fi 
done 
scs
  • 567
  • 6
  • 22

1 Answers1

1

If I understand right, that is the for loop you want:

for file in *.txt ; do
Hans
  • 46
  • 4
  • To add the idea of @Hans to the example, replace the if with if [[ ${DATABASENAME} == *.txt ]]. ${DATABASENAME} =~ .*\.txt$ is also correct (the $ specifies the end of the word). – scs Jun 08 '16 at 09:52