help with a script to gzip/move files (706 Views)
Reply
Super Advisor
NDO
Posts: 545
Registered: ‎12-10-2003
Message 1 of 9 (706 Views)
Accepted Solution

help with a script to gzip/move files

Hi

 

I need help on a script that runs through a dir (not recursvely) gzip files based on month, them send these gziped files to a subdir in the same directory.

Your help will be appreciated

 

Nandinho

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 2 of 9 (698 Views)

Re: help with a script to gzip/move files

>runs through a dir (not recursively) gzip files based on month

 

Month of modification or encoded in the filenames?

 

>them send these gzipped files to a subdir in the same directory.

 

Something like:

#!/usr/bin/ksh

#runs through a dir (not recursively) gzip files based on month
#them send these gzipped files to a subdir in the same directory.

if [ $# -ne 1 ]; then
   echo "Usage: $0 month-to-backup" 1>&2
   exit 1
fi

\ll -og | awk -v month=$1 '$4 == month { print $7 }' | while read file; do
   echo gzip $file
   echo mv $file.gz Subdir
done

 

Remove the echo if this matches what you want to do.

 

Super Advisor
NDO
Posts: 545
Registered: ‎12-10-2003
Message 3 of 9 (693 Views)

Re: help with a script to gzip/move files

Hi

 

 

Thanks a lot, but please if its is not asking too much is it possible to put some comments on each line for better understanding of the script?

 

 

Nandinho

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 4 of 9 (686 Views)

Re: help with a script to gzip/move files

> is it possible to put some comments on each line

#!/usr/bin/ksh

#runs through a dir (not recursively) gzip files based on month
#them send these gzipped files to a subdir in the same directory.

if [ $# -ne 1 ]; then     # make sure only one parm
   echo "Usage: $0 month-to-backup" 1>&2
   exit 1
fi

SUBDIR=Subdir  # Set subdirectory name
MONTH=$1       # month parm
\ll -og |    # list files in current directory, leaving out owner/group
  awk -v month=$MONTH '  # use -v to pass shell variable to awk variable
$4 == month { print $7 }' | # select files (field 7) with month (field 4)
  while read file; do   # for each file that matches month ($1)
   echo gzip $file      # gzip
   echo mv $file.gz $SUBDIR  # move to $SUBDIR directory
done

 

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 9 (685 Views)

Re: help with a script to gzip/move files


Nandinho wrote:

Thanks a lot, but please if its is not asking too much is it possible to put some comments on each line for better understanding of the script?


Dennis provided more than "help".  He wrote a simple, straight-forward script to solve your problem.
You will be better served if *you* consult the manpages for the commands he used and try to understand what each piece of the script does.  You may need to begin by reading a beginner's guide to shell scripting if you don't have a good grasp of the basics.
If there is a specific line or two of the script that you are struggling over then ask about that after you have made some attempt to understand it.  My comments are not intended to dismiss your question but rather to help you grow.
Regards!
...JRF...

 

Super Advisor
NDO
Posts: 545
Registered: ‎12-10-2003
Message 6 of 9 (681 Views)

Re: help with a script to gzip/move files

Hi James!

 

Thanks a lot for your inputs, and advise, I will be doing that

 

 

Nandinho

Super Advisor
NDO
Posts: 545
Registered: ‎12-10-2003
Message 7 of 9 (673 Views)

Re: help with a script to gzip/move files

Hi Dennis

I have the following script:
touch -mt 201108010000 /tmp/ref1
touch -mt 201108012359 /tmp/ref2
find /somedir -type f \( -newer /tmp/ref1 -a ! -newer /tmp/ref2 \)> file_list
scp -p $(< file_list) root@10.100.48.11:/another_dir/

How do I count the number of files sent, and get this number and send it to an email address?

Nandinho
Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 8 of 9 (668 Views)

Re: help with a script to gzip/move files

>How do I count the number of files sent, and get this number and send it to an email address?

 

echo "Files sent: $(wc -l < file_list)" | mailx -s "Files transferred" abc@def.com

Super Advisor
NDO
Posts: 545
Registered: ‎12-10-2003
Message 9 of 9 (663 Views)

Re: help with a script to gzip/move files

Hi
Thank you, worked fine.

Nandinho
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.