Link to home
Start Free TrialLog in
Avatar of srini_pendy

asked on

Shell Script- gzip

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

Avatar of Hanno P.S.
Hanno P.S.
Flag of Germany image

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.

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}

Open in new window

Avatar of srini_pendy


Thanks, can you please add the following

1). Exclude already existing gz files.
2).Exclude .sh files
Avatar of Hanno P.S.
Hanno P.S.
Flag of Germany image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The script looks good but the regex should be
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".
Hi Simon,

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