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.

  • 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 by clicking the checkmark next to it. Thank you! – Jeff Schaller Jan 20 '19 at 12:58
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.

1

Use stat instead of ls. I.e.:

find . -type d -perm -a+rwx -exec stat -c "%Y %n" '{}' ';' | sort -rn | tail -n 5
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.
-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) ;)

  • 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: unix.stackexchange.com/a/50523/6622 • Moreover, I recommend reading stackoverflow.com/questions/896808/find-exec-cmd-vs-xargsAlso, 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 2:22
  • So, in remainder your note goes to "OpenBSD also has -r". macOS does not though. Added to answer. – poige Apr 24 '18 at 5: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?. Note that -execdir is not portable/standard yet and on BSDs has issues of its own. – Stéphane Chazelas Apr 24 '18 at 8: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 9:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.