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

Thanks
srini_pendyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
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

0
srini_pendyAuthor Commented:
Thanks, can you please add the following

1). Exclude already existing gz files.
2).Exclude .sh files
0
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
a) *.gz files are already excluded
b) to exclude more file extensions, modiufy code as follows:

#!/bin/ksh
date=`date '+%Y%m%d%H%M%S'`         # the date string for prefix
for file in `ls | egrep -v '*.gz|*.sh'`; 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

To see what thge egrep does, simply run
   ls | egrep -v '*.gz|*.sh'
on the command line.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simon3270Commented:
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".
0
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
Hi Simon,

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

Thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.