Operating System - HP-UX
1753727 Members
4667 Online
108799 Solutions
New Discussion

Compressing files with script

 
SOLVED
Go to solution
NDO
Super Advisor

Compressing files with script

Hi

 

I have the following script:

for files in `ls -lrt | grep -i 'Dec' | awk '{print $9}'`
do
gzip $files
done

But I would like also to includes more months, like Jan, Feb and Mar. Can I use "&&" between month

5 REPLIES 5
Steven Schweda
Honored Contributor

Re: Compressing files with script

> I have the following script:
>
> for files in `ls -lrt | grep -i 'Dec' | awk '{print $9}'`
> [...]

   Not a reliable method, I claim.

mba$ ls -l *.txt | grep -i 'Jan' | awk '{print $9}'
janfebmaraprmayjun.txt

mba$ ls -l *.txt | grep -i 'Jan'
-rw-r--r-- 1 sms staff 5 Dec 5 07:58 janfebmaraprmayjun.txt

   Removing the "-i" might help a little, and adding spaces to your
search string might help more, but it's still not a reliable method.

   "find" has options to let the user specify date-time bounds, which is
generally more reliable than parsing "ls" output -- seldom an easy task.

      man find

A search of this forum (or the Web) should find many examples.

   With "find", you could also specify things like, say:
       ! -name '*.gz'
to avoid trying to compress already-compressed files.

> But I would like also to includes more months, like Jan, Feb and Mar.
> Can I use "&&" between month

   If you insist on doing things that way, then "egrep" allows "extended
regular expressions (EREs)."

      man egrep

   You seem to want an OR condition. For example:

mba$ ls -l *.txt | egrep ' Mar | Jun | Oct '
-rw-r--r-- 1 sms staff 1172 Oct 18 2015 iv.txt
-rw-r--r-- 1 sms staff 1167 Mar 29 2016 iv2.txt
-rw-r--r--@ 1 sms staff 902 Jun 25 2015 ports.txt
-rw-r--r-- 1 sms staff 2169 Jun 19 2015 vim.txt

   But it's still a lame method.

Dennis Handly
Acclaimed Contributor
Solution

Re: Compressing files with script (filter by months)

>Can I use "&&" between month

 

Of course not.  You need to use the computer science OR, not AND.  :-)

 

The same cautions that Steven gave about finding months in the the names of files or user/groups.

Also, there is no need to use -tr, since the gzip order doesn't really matter.

Instead of using the egrep hammer, you can use grep with multiple -e:

for files in $(ll | grep -e Jan -e Feb -e Mar -e Dec -e  | awk '{print $9}'); do

 

Or do the filter in awk and check the Month field:

for files in $(ll | awk '$6 ~ "Jan|Feb|Mar|Dec" {print $9}'); do

 

> "find" has options to let the user specify date-time bounds, which is

 

Which would only work for one year at a time, not all Dec files, of course depending on how many predicates you add to find(1).

Steven Schweda
Honored Contributor

Re: Compressing files with script (filter by months)

> Also, there is no need to use -tr, [...]

   Yup.

> [...] you can use grep with multiple -e:
> for files in $(ll | grep -e Jan -e Feb -e Mar -e Dec -e | [...]

   Including spaces would still help here: -e ' Jan '

> > "find" has options to let the user specify date-time bounds, [...]
>
> Which would only work for one year at a time, not all Dec files, of
> course depending on how many predicates you add to find(1).

   Yup.

> I have the following script:
> [...]

   As usual, this may reveal many of the author's poor implementation
decisions, but it does little to reveal the original intent, that is,
the actual problem to be solved.  Advice on how better to grip a wrench
may help, but, when your actual goal is driving a nail, a better answer
may involve a hammer, rather than any advice involving the original
wrench.  If you ask about the wrench, and fail to mention the nail, then
you can expect sub-ideal advice.

NDO
Super Advisor

Re: Compressing files with script (filter by months)

Thank you very much for the advice

Dennis Handly
Acclaimed Contributor

Re: Compressing files with script (filter by months)

>when your actual goal is driving a nail

 

Then you probably want a screwdriver.

After all, when everything looks like a screw, you tend to use a screwdriver.  :-)