Search code examples
bashunzip

Bash unzip file and add timestamp in name


I have zip with structure

temp.zip

- file.csv
- readme.txt
- license.txt

How unzip temp.zip, with add timestamp in name, result:

file.142345687.csv
readme.142345687.txt
license.142345687.txt

Solution

  • List files using the -l option, then extract them one-by-one using the -p option:

    unzip -l -q -q temp.zip | awk '{print $NF}' | while read file
    do
      unzip -p temp.zip "${file}" > "${file%.*}.$(date +%s).${file##*.}"
    done
    

    where

    • -q -q options ask for a silent output (in easy-to-parse columns);
    • awk's $NF points to the last column;
    • ${file%.*} deletes the shortest match of .* from back of the filename;
    • ${file##*.} deletes the longest match of *. from the front of the filename;
    • $(date +%s) outputs seconds since 1970-01-01 00:00:00 UTC