ConorHolt
ConorHolt

Reputation: 291

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

Upvotes: 2

Views: 1000

Answers (2)

Mustafa DOGRU
Mustafa DOGRU

Reputation: 4112

you can try something like this;

#!/bin/bash
unzip temp.zip
for n in $(unzip -Z -1 "temp.zip"); do 
  e=${n#*.}
  fn="${n%.*}"
  DATE=`date +%s`
  newFileName="$filename.$DATE.$e"
  mv "$n" "$newFileName"
done

Upvotes: 0

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21502

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

Upvotes: 3

Related Questions