Tar multiple files in for loop

856 views Asked by At

I have this loop which loops through directories and looks for specific files ($path is read from script prompt):

for FILE in $path/*/logs/*2016-*; do 
######note that I don't have quotes around $path/*/logs/*2016-* in order for parameter expansion to work

  JOB=${FILE%/*/*}   
  JOB_NAME=${JOB##*/}

  cd $path/$JOB_NAME/logs
  tar zcvf 2016_logs.tgz "$FILE"
done

What is happening is it compresses each file and overrides tar file with each loop so I only get one file in my tar archive.

I need a single tgz file in $path/*/logs, with all the files that matched "2016-" in that directory

UPDATE: I was able to fix my issue using this code:

for DIR in $path/*/logs ; do
    cd $DIR

    FILES=$(find ./ -type f -name "*${year}*" -print)
    if [[ -n $FILES ]];then
        tar -cvzf "${year}_logs.tgz" ${FILES}
    fi
done
else
  echo "file not found"
fi
1

There are 1 answers

4
dash-o On

The current code loops over FILES, and then it places a TGZ version of the file into 2016_logs.tgz in the same folder. It will be more effective to loop over the FOLDERS, and build each TGZ file from the individual data files.

Bash solution (shopt)

shopt -s nullglob
for DIR in $path/*/logs ; do
    files=$(shopt -s nullglob ; cd $DIR && echo *2016-*)
    [ "$files" ] || continue
    (cd $DIR && tar cvzf 2016_logs.tgz $files)
done