This command is a part in a row of piped inotify workers (do you say that?) etc. I thought it would fit the one question per post policy better than asking for a dissection of the whole (very useful !) command to find out the used inotify

xargs -I '{}' -- ps --no-headers -o '%p %U %c' -p '{}'

According to the docs the syntax is

xargs [options] [command [initial-arguments]]

-I replace-str Replace occurrences of replace-str in the initial- arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L

Why are the braces in ticks?

Then the square brackets are filled with the output from ps command, right?

  1. --no-headers - Is quite obvious
  2. -o - Order option...
  3. %p - process id (pid)
  4. %U - user id
  5. %c - simple name of executable

-p pidlist Select by PID. This selects the processes whose process ID numbers appear in pidlist. Identical to p and --pid.

What does this second '{}' pid list do and what values does it have? $foo ? What does select mean in this context?

Just for information

The preceding code is:

for foo in /proc/*/fd/*; do readlink -f $foo; done |grep inotify |cut -d/ -f3 |

Author: envygeeks , https://github.com/atom/atom/issues/2082


  • I can't work out where you are stuck. Tell us what you are stuck on. Nov 23 at 20:13
  • There are issues with your "preceding code". (a) There are two race conditions that may throw errors: processes can vanish in the period between shell evaluating the wildcards, and readlink querying the link; and also before the ps can be run. (b) A process can have multiple files connected to inotify, so the same pid can be reported several times. I also think -o is the user-defined output format option, not the (BSD) ordering option. Nov 23 at 22:13

1 Answer 1

  1. Why are the braces in ticks?

    To prevent shell mangling it - in some shells it may be a special character sequence that will get interpreted; and you may decide to use something else than just '{}'.

  2. What does this second '{}' pid list do and what values does it have?

    The second '{}' is actually inside of the command that xargs will run - this is the place where {} will be replaced by whatever xargs reads on stdin

  3. $foo?

    No. Try to understand what the command generates loop over all open file descriptors (the for cycle) and get full path to them (the readlink part), which includes PID of the owning process. Then take this list, filter out those referring to inotify (the grep part) and extract the PIDs from paths (cut). Each line is what xargs uses as a replacement for the placeholder.

  4. What does select mean in this context?*


  • 1
    The -I '{}' specifies the token in the ps command which will be substituted with the input value just read into xargs from stdin. The -I also has the effect of processing the input as lines, not words. xargs has no knowledge of the command it is going to run: it just merges the prototype command (starting with ps) with the variables from stdin (in this case they are process ids). Nov 23 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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