- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Compressing files with script (filter by month...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2016 01:37 AM
12-05-2016 01:37 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2016 06:27 AM
12-05-2016 06:27 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2016 11:36 AM - edited 12-05-2016 11:44 AM
12-05-2016 11:36 AM - edited 12-05-2016 11:44 AM
Solution>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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2016 02:24 PM
12-05-2016 02:24 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2016 09:58 PM
12-05-2016 09:58 PM
Re: Compressing files with script (filter by months)
Thank you very much for the advice
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2016 11:46 PM
12-10-2016 11:46 PM
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. :-)