Novice_Techie
Novice_Techie

Reputation: 444

Gzip the files in unix

I have a file called Apple_PC.csv I need to gzip it such that the file name would be: Apple_PC_<yyyymmddhhmmss>.gz But when it is unzipped it should have original file : Apple_PC.csv

Tried this:

   gzip -q  /users/files/Apple_PC_$(date +%Y%m%d%H%M%S).gzip /users/files/Apple_PC.csv

It gives me error

gzip: Apple_PC_20171212105127.gzip: No such file or directory

I don't want my original file (Apple_PC.csv) in my directory available after successful gzip. Not sure what am i missing here.

Upvotes: 1

Views: 2484

Answers (1)

theherk
theherk

Reputation: 7576

Just compress it then move it.

gzip /users/files/Apple_PC.csv &&
    mv /users/files/Apple_PC.csv.gz \
       /users/files/Apple_PC_$(date +%Y%m%d%H%M%S).gzip

I should add, gzip uses the filename of the compressed file to generate the name of the decompressed file, so you should act accordingly. You'll need to rename it going in and going out. .gzip will not work. This is probably what you need.

gzip /users/files/Apple_PC.csv && 
    mv /users/files/Apple_PC.csv.gz \
       /users/files/Apple_PC_$(date +%Y%m%d%H%M%S).gz
# then to decompress
mv /users/files/Apple_PC_$(# match date here).gz \
   /users/files/Apple_PC.csv.gz &&
       gzip -d /users/files/Apple_PC.csv.gz

Alternatively, you could use stdin and stdout rather than moving files, but that may be another topic altogether.

There is also a --name option for preserving the original filename, but I am not sure how portable it is.

Upvotes: 1

Related Questions