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

Unix OSShell Scripting

Avatar of undefined
Last Comment
Hanno P.S.

8/22/2022 - Mon
Hanno P.S.

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


Thanks, can you please add the following

1). Exclude already existing gz files.
2).Exclude .sh files
Hanno P.S.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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".
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
Hanno P.S.

Hi Simon,

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