We help IT Professionals succeed at work.

Shell Script- gzip

srini_pendy
srini_pendy asked
on
616 Views
Last Modified: 2017-04-07
I'm looking for working shell script for the following - (AIX Unix)

1).Cron Job runs every hours.
2).All the new files to be gzip with a prefix of current time stamp i.e.
TARA.xml becomes 20170404161501_TARA.xml.gz

Thanks
Comment
Watch Question

Hanno P.S.IT Consultant and Infrastructure Architect
CERTIFIED EXPERT

Commented:
I use the command
   ls | grep -v '*.gz'
to get a list of all files (in current) directory that are not already gzip-compressed.
The command
   date +%Y%m%d%H%M%S
Formats the actual date and time.

#!/bin/ksh
date=`date '+%Y%m%d%H%M%S'`         # the date string for prefix
for file in `ls | grep -v '*.gz'`; do
   if [ -f "$file" ] ; then         # if this is a normal file (not directory or special file)
      mv $file ${date}_${file}      # prepend prefix
      gzip -9  ${date}_${file}
   fi
done

Open in new window

Author

Commented:
Thanks, can you please add the following

1). Exclude already existing gz files.
2).Exclude .sh files
IT Consultant and Infrastructure Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
The script looks good but the regex should be
      '\.gz$|\.sh$'
The * isn't necessary, you escape the dot to check for a real dot (otherwise "." means "any character"), and you need the "$" to tie this to the end of the string (otherwise it would match, for example, "any character, followed by a g and a z" in the middle of a file name). "*.gz" is how you would write a "glob" string, for the shell to expand, in a command such as "ls *.gz".
Hanno P.S.IT Consultant and Infrastructure Architect
CERTIFIED EXPERT

Commented:
Hi Simon,

thanks for clearifying -- I saw this yesterday already, when reviewing my code.
Unfortunately, I did not have a system to check ... :-(

Thx