Shell Script- gzip

srini_pendy
srini_pendy used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hanno P.S.IT Consultant and Infrastructure Architect

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
Commented:
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.
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

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial