News for hackers. Stuff that matters.
Re:Ask HN: Can I see your cheatsheet? by ww520 on Thursday June 30, 2022 @04:16AM (#31929932)

  - Find all files modified in the last 90 minutes  
    find . -type f -mmin -90  
  - Find all files and do something to each one
    find . -type f -mmin -90 | xargs ls -l  
  - Find all files modified before 2 days ago  
    find . -type f -mtime +2  
  - Removing files older than 7 days  
    find . -type f -mtime +7 -name '*.gz' -exec rm {} \;  
  - Find all files modified after last 2 days  
    find . -type f -mtime -2  
  - Find all files modified at the last 2 day mark  
    find . -type f -mtime 2
Re:Ask HN: Can I see your cheatsheet? by pmoriarty on Thursday June 30, 2022 @05:43AM (#31930398)
With zsh you don't need to use find, fd, or any other external tool.

zsh's built-in globbing features can be used instead.

  - Find all files modified in the last 90 minutes
    print -l **/*(mm-90)
  - Find all files and do something to each one
    print -l **/*(mm-90) | xargs ls -l
  - Find all files modified before 2 days ago
    print -l **/*(m+2)
  - Removing files older than 7 days
    zargs -- **/*(m+7) -- rm
  - Find all files modified after last 2 days
    print -l **/*(m-2)
  - Find all files modified at the last 2 day mark
    print -l **/*(m2)
In most of the above examples "print -l" is used to print the results to the terminal, but you can instead use whatever command you want (as in the "rm" example above).

Many more examples and tips can be seen here: [1]

[1] - http://www.zzapper.co.uk/zshtips.html

Re:Ask HN: Can I see your cheatsheet? by ptspts on Thursday June 30, 2022 @06:40AM (#31930740)
The zsh rm command doesn't with too many (say, 100 000) files, execve() fails with: Argument list too long. This works and it is relatively fast (runs rm in big batches): find ... | xargs -d '\n' rm --
Re:Ask HN: Can I see your cheatsheet? by tyingq on Thursday June 30, 2022 @08:05AM (#31931346)
Perl is also helpful to pipe to for any tool that outputs lists of files, like

  find . -type f -name '*foo' | perl -ne 'print;chomp;unlink'
And it has the same -0 switch as xargs to pair with find's -print0.

Not helpful in this case since you have other options, but maybe helpful for things like bulk touch, chmod, etc.

Re:Ask HN: Can I see your cheatsheet? by pmoriarty on Thursday June 30, 2022 @06:50AM (#31930800)
You can do the same with:

  zargs -- **/*(m+7) -- rm
I updated my answer above accordingly.
Re:Ask HN: Can I see your cheatsheet? by blaerk on Thursday June 30, 2022 @07:16AM (#31931002)
For you can skip xargs for that and just add the -delete argument to find, for readability and profit(!?)
Re:Ask HN: Can I see your cheatsheet? by xdfgh1112 on Thursday June 30, 2022 @04:30AM (#31930003)
There needs to be a new version of find with a more intuitive UI. (I suspect it exists, but I can't remember the name right now)
Re:Ask HN: Can I see your cheatsheet? by forgotpwd16 on Thursday June 30, 2022 @04:45AM (#31930087)
This is fd.

    fd --newer 90min
    fd --newer 90min -x ls -l {}
    fd --older 2d
    fd -g '*.txt' --older 7d -x rm {}
    fd --newer 2d
    fd --newer 3d --older 1d
Re:Ask HN: Can I see your cheatsheet? by ryannevius on Thursday June 30, 2022 @04:46AM (#31930090)
I've been pretty happy with `fd`: https://github.com/sharkdp/fd
Re:Ask HN: Can I see your cheatsheet? by pabs3 on Thursday June 30, 2022 @07:24AM (#31931053)
Please use `find -print0 | xargs -0` (or -exec) when running commands on files, otherwise xargs will mangle filenames in some cases.
Re:Ask HN: Can I see your cheatsheet? by ahoef on Thursday June 30, 2022 @07:25AM (#31931059)
Depending on how you name your files, you want to add -print0 to your find and -o to your xargs. If you have spaces in your file names you want that to carry over in your xargs.

Secondly, you may want to add -n 200 to your xargs so if you have very, very many files bash doesn't complain.

So:

Find all files and do something to each one

    find . -type f -mmin -90 -print0 | xargs -0 -n 1024 ls -l
Re:Ask HN: Can I see your cheatsheet? by goodevans on Thursday June 30, 2022 @05:14AM (#31930214)
Find all .txt files containing the word 'foo': find ./ -mount -type f -name "*.txt" -exec grep -iHn 'foo' {} \;

(the -mount stops it traversing into mounted shares)

Re:Ask HN: Can I see your cheatsheet? by pmoriarty on Thursday June 30, 2022 @05:54AM (#31930466)
With zsh:

  grep -iHn 'foo' **/*.txt(.)
Though I'm not sure how to prevent zsh from descending in to other filesystems, as you can with find.
Re:Ask HN: Can I see your cheatsheet? by unixhero on Thursday June 30, 2022 @04:42AM (#31930072)
Ooh thanks!!!