Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

help with a script to gzip/move files

SOLVED
Go to Solution
Super Advisor

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

8 REPLIES
Acclaimed Contributor Acclaimed Contributor

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

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 Acclaimed Contributor

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 Acclaimed Contributor

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

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

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 Acclaimed Contributor

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

Re: help with a script to gzip/move files

Hi
Thank you, worked fine.

Nandinho