1

I am trying to find the top 5 most recent directories that have been modified with certain permissions.

My find command is like this

find -d -perm -a+rwx

I’m trying to combine it with

ls -lt

to take the results of find and put them in a long list with the most recently modified file on top.

Nothing I tried worked; I ended up getting something saying total=0.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
johndoe
  • 11
  • 2
  • 1
    what version of find are you using? Are you sure you don't want `-type d` instead of `-d`? – jesse_b Apr 21 '18 at 21:37
  • If any of the answers solved your problem, please [accept it](https://unix.stackexchange.com/help/someone-answers) by clicking the checkmark next to it. Thank you! – Jeff Schaller Jan 20 '19 at 12:58

4 Answers4

2

zsh-ixly:

ls -ltd -- **/*(/omf777[1,5])

This performs an ls (a l long listing, again sorted by modification time t, and with the -d flag to list only the directory and not its contents) on the first 5 ([1,5]) files returned by the recursive zsh glob pattern that matches directories (/) with 777 permissions (f777), ordered by modification time (om).

Note that hidden files and directories are ignored. Add the D glob qualifier to consider them.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
1

Use stat instead of ls. I.e.:

find . -type d -perm -a+rwx -exec stat -c "%Y %n" '{}' ';' | sort -rn | tail -n 5
Ralph Rönnquist
  • 3,183
  • 1
  • 10
  • 11
1

If you have Bash (or Zsh) and GNU tools installed, you can handle literally any filename:

while IFS= read -r -d '' -u 9
do
    printf '%q\n' "${REPLY#* }"
done 9< <(
  find . -type d -perm -a+rwx -printf '%T@ %p\0' |
    sort --general-numeric-sort --zero-terminated |
    head --lines=5 --zero-terminated)

Reading outside in, this does the following:

  1. Find directories which are open to the world.
  2. For each directory, print the modification timestamp and file path followed by a NUL character.
  3. Sort the list numerically, that is, according to the timestamp since that is the first column.
  4. Get the first five zero-terminated entries.
  5. Read this list one by one.
  6. Strip the timestamp from each entry.
  7. Print a quoted version of each filename.
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
l0b0
  • 50,672
  • 41
  • 197
  • 360
-1

find is traditionally/conventionally paired with xargs:

find …something… -print0 | xargs -0 …dosomething…

Specifically to your q-n piping should be done to xargs -0 ls -dlt

☝️Note: BSDs'1 version of xargs won't run if there were no input piped; but with GNU you'd better use -r (man xargs would reveal)

__

1 — As indicated in comments OpenBSD is an exception (as it's often the case w/ OpenBSD) ;)

poige
  • 6,195
  • 2
  • 30
  • 57
  • 1
    That depends on which BSD. That's true of FreeBSD or NetBSD, but not of OpenBSD or macOS IIRC (POSIX `xargs` does require the command to be run once upon empty input and doesn't specify the `-r` nor `-0` options). Using `-exec cmd {} +` is a more standard/reliable way to run `cmd` with as many of the files _found_ by `find` as possible with each invocation. – Stéphane Chazelas Apr 23 '18 at 16:34
  • • `find`'s `exec`-and-company are things known to me long ago: https://unix.stackexchange.com/a/50523/6622 • Moreover, I recommend reading https://stackoverflow.com/questions/896808/find-exec-cmd-vs-xargs • _Also_, you'd better check manual and _avoid using_ `-exec` preferring `-execdir` instead (when you're inclined to avoid `xargs` anyways) – poige Apr 24 '18 at 02:22
  • So, in remainder your note goes to "OpenBSD also has -r". _macOS does not though_. Added to answer. – poige Apr 24 '18 at 05:03
  • To clarify, I was not the one downvoting. The fact that find and xargs are often used together (even though without the non-standard -0/-print0 that's not reliable), that you generally want `-r` where available (and need to resort to things like `xargs -0 sh -c '[ "$#" -eq 0 ] || exec cmd "$@"' sh` otherwise) are good points. See also [Why is looping over find's output bad practice?](//unix.stackexchange.com/q/321697). Note that `-execdir` is not portable/standard yet and on BSDs has issues of its own. – Stéphane Chazelas Apr 24 '18 at 08:22
  • Well, actually `-r` is implied with BSD's version in despite of being absent there, so no need for SHELL wrap arounds. `-r` is needed only on GNU version and OpenBSD's as you noted. One might use `ifne` BTW. As to `-execdir` well, I by myself did prefer all those `-exec`'s until becoming more familiar with `xargs`. I don't think your point on `-print0`/`-0` makes any sense; any modern UNIX-like has support for it. Even OpenBSD does. ;-P _To clarify, I was not the one downvoting._ — thanks, but I think it's nevermind anyways. – poige Apr 24 '18 at 09:10