zsh-users
 help / color / mirror / code / Atom feed
* List ALL files modified in last 24hrs OR list atleast 10 recent files
@ 2014-03-11 10:25 Amm
  2014-03-12  0:02 ` Ray Andrews
  2014-03-12  6:36 ` Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: Amm @ 2014-03-11 10:25 UTC (permalink / raw)
  To: zsh-users

Hello,

I am trying to combine two patterns.

1) List plain files modified in last 24hrs (older to newer)

ls -lU *(.mh-24Om) # thats 24, capital oh

This works fine


2) List 10 most recent files (older to newer)

ls -lU *(.Om[-10,-1])

This also works fine.


Now I want to combine (OR) these two such that:

Case 1)
If there are more than 10 files modified in 24hrs
then list them all (not just 10)

Case 2)

If there are, say 7, files modified in last 24hrs
then list those 7 + 3 other most recent files
(listing total 10 most recent files)


I referred to this page:
http://zsh.sourceforge.net/Doc/Release/Expansion.html
(Section 14.8.7 Glob Qualifiers)


I tried:
1) ls -lU *(.mh-24Om[-10,-1])
This (as expected) ANDs the result not OR.


2) ls -lU *(.mh-24,.Om[-10,-1])

Now as per above link:
Comma ORs the pattern so logically first one will pick
files of last 24hrs, next will pick most recent
10 files, both will be ORed (union)


But then in that link there is one line which says:

"Some qualifiers,
however, affect all matches generated,
independent of the sublist in
which they are given.
These are the qualifiers ...and brackets (‘[...]’)"

Which means [-10,-1] is applied at end, effectively
reducing list to 10 files. (even if there are more than
10 files modified in last 24hrs)



I tried several other ways but did not work.

Like:
ls -rlU --sort=time *(.mh-24) *(.Om[-10,-1])


But this causes duplicate entries, plus it fails
if there is nofile modified in last 24hrs.


I want to do it in a single statement not via multiple
commands.


So how do I achieve this? Any idea?

Thanks in advance.

Amm.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: List ALL files modified in last 24hrs OR list atleast 10 recent files
  2014-03-11 10:25 List ALL files modified in last 24hrs OR list atleast 10 recent files Amm
@ 2014-03-12  0:02 ` Ray Andrews
  2014-03-12  7:17   ` Amm
  2014-03-12  6:36 ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2014-03-12  0:02 UTC (permalink / raw)
  To: zsh-users

On 03/11/2014 03:25 AM, Amm wrote:
> Now I want to combine (OR) these two such that:
>
> Case 1)
> If there are more than 10 files modified in 24hrs
> then list them all (not just 10)
>
>
For that, why not just pipe to  'wc -l' to count the lines and then do 
some 'if' tests from there?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: List ALL files modified in last 24hrs OR list atleast 10 recent files
  2014-03-11 10:25 List ALL files modified in last 24hrs OR list atleast 10 recent files Amm
  2014-03-12  0:02 ` Ray Andrews
@ 2014-03-12  6:36 ` Bart Schaefer
  2014-03-12  7:13   ` Amm
  1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2014-03-12  6:36 UTC (permalink / raw)
  To: zsh-users

On Mar 11,  6:25pm, Amm wrote:
}
} Case 1)
} If there are more than 10 files modified in 24hrs
} then list them all (not just 10)
} 
} Case 2)
} If there are, say 7, files modified in last 24hrs
} then list those 7 + 3 other most recent files

So you don't really want to OR the patterns, you want to take the union
of the results.  You have the right idea here:

} ls -rlU --sort=time *(.mh-24) *(.Om[-10,-1])
} 
} But this causes duplicate entries, plus it fails
} if there is nofile modified in last 24hrs.

You can avoid the failure by adding an N to each of the glob qualifiers:

    ls -rlU --sort=time *(N.mh-24) *(N.Om[-10,-1])

Combining the -U (unsorted) and --sort=time options doesn't sense here?
Your original statement of the problem implies you want the files from
the last 24 hours sorted by name followed by any other recent files
sorted by time.  

To remove the duplicates, you can assign the results to an array, then
expand only the uniqe elements with ${(u)array}.  In recent versions of
zsh you can do this with an anonymous function:

    (){ ls -lU "${(u)@}" } *(N.mh-24Om) *(N.Om[-10,-1])

In older versions you'll have to define the function and then call it:
    
    _(){ ls -lU "${(u)@}" };_ *(N.mh-24Om) *(N.Om[-10,-1])

Aside:  It's somewhat unfortunate that:

    typeset -U var <=> ${(u)var}
    typeset -u var <=> ${(U)var}

I've forgotten how we ended up with that situation ...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: List ALL files modified in last 24hrs OR list atleast 10 recent files
  2014-03-12  6:36 ` Bart Schaefer
@ 2014-03-12  7:13   ` Amm
  0 siblings, 0 replies; 5+ messages in thread
From: Amm @ 2014-03-12  7:13 UTC (permalink / raw)
  To: zsh-users



On Mar 12, 12:06 PM Bart Schaefer wrote:

}} ls -rlU --sort=time *(.mh-24) *(.Om[-10,-1])
}} 
}} But this causes duplicate entries, plus it fails
}} if there is nofile modified in last 24hrs.


} You can avoid the failure by adding an N to each of the glob qualifiers:
}    ls -rlU --sort=time *(N.mh-24) *(N.Om[-10,-1])

Great I did not know about N flag. That solved one problem.



} Combining the -U (unsorted) and --sort=time options doesn't sense here?

Yep, -U was typing mistake for that example.



} Your original statement of the problem implies you want the files from
} the last 24 hours sorted by name followed by any other recent files
} sorted by time.

No everything sorted by time.


} To remove the duplicates, you can assign the results to an array, then
} expand only the uniqe elements with ${(u)array}.  In recent versions of
} zsh you can do this with an anonymous function:

}    (){ ls -lU "${(u)@}" } *(N.mh-24Om) *(N.Om[-10,-1])



Actually today I was trying typeset like this.
lst() {
    typeset -U plst
    plst=(*(.Om[-10,-1]))
    plst=($plst *(.mh-24))
    ls -lr --sort=time $plst
}
But now I can combine your two suggestions of N and (u)
plus anonymous function to make it a single command.


lst () {
    () {
        ls -lr --sort=time "${(u)@}"
    } *(N.mh-${1:-24}) *(N.Om[-${2:-10},-1])
}

Note: I have extended it so that I can pass parameters to
function. i.e. lst [hours] [nooflines]

Works perfect as per my requirement.


Thanks a lot for your solution.


Amm.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: List ALL files modified in last 24hrs OR list atleast 10 recent files
  2014-03-12  0:02 ` Ray Andrews
@ 2014-03-12  7:17   ` Amm
  0 siblings, 0 replies; 5+ messages in thread
From: Amm @ 2014-03-12  7:17 UTC (permalink / raw)
  To: zsh-users





----- Original Message -----
From: Ray Andrews <rayandrews@eastlink.ca>

> For that, why not just pipe to  'wc -l' to count
> the lines and then do  some 'if' tests from there?

Nope I had already said in previous e-mail that:
"I want to do it in a single statement not via multiple
commands."

Anyway I have found a solution from Bart.

Thanks for your suggestion.

Amm



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-03-12  7:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-11 10:25 List ALL files modified in last 24hrs OR list atleast 10 recent files Amm
2014-03-12  0:02 ` Ray Andrews
2014-03-12  7:17   ` Amm
2014-03-12  6:36 ` Bart Schaefer
2014-03-12  7:13   ` Amm

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).