One way would be to use shell arrays:
IFS=$'\n' FILES=($(find /mnt/md0/capture/dcn/ -maxdepth 1 -name "*.pcap" -mtime +5))
#find the files and put the paths into an array "FILES"
This puts all of the files with paths into the shell array "FILES" and takes into consideration blanks in file names. The order will be in whatever order find
gives them.
To build the zip file name:
FIRSTNAME=${FILES[0]##*/}
LASTNAME=${FILES[${#FILES[@]}-1]##*/}
ZIPPREFIX="${FIRSTNAME%.*}-${LASTNAME%.*}"
#zip base name made from first and last file basenames
Here, FIRSTNAME=${FILES[0]##*/}
yields the name of the file with extension, and ${FIRSTNAME%.*}
removes the extension. It will strip off the path and file extension. (If you want to keep the file extension, you can use $FIRSTNAME
.) The value ${#FILES[@]}
is the number of files in the array. Thus, the somewhat messy-looking ${FILES[${#FILES[@]}-1]}
represents the last file name in the array. And finally the extra little bit of ##*/
in ${FILES[${#FILES[@]}-1]##/*}
removes the path, leaving the file name. Ultimately, ZIPPREFIX
will be the first base name, a hyphen (-
), and the last base name. You can use a different character other than hyphen if you wish.
zip -j /mnt/md0/capture/dcn/"$ZIPPREFIX"-$(date "+%b_%d_%Y_%H_%M_%S").zip "${FILES[@]}"
#zip them
This zips the files. Note that ${FILES[@]}
provides all of the array elements.
rm "${FILES[@]}"
#remove all the files