I was trying to find all symbolic links in /usr/lib and use file on the first entry in the list, which I delimited with head -n 1.

Why does find /usr/lib -maxdepth 1 -type l | file $(head -n 1) work but find /usr/lib -maxdepth 1 -type l | head -n 1 | file does not?

It complains that I am not using file correctly. Probably because it lacks an argument, but - programmatically/syntactically - why can’t file grab it’s argument from the pipe?

  • MummifiedClient5000@feddit.dk
    link
    fedilink
    English
    arrow-up
    15
    ·
    17 hours ago

    When you use the pipe you’re passing stdout from the left command to stdin on the right command.

    file takes a filename as an argument, it does not read stdin (by default).

    The first command works because head does read from stdin and then echoes the first line, but as an argument to file using command substitution - the $( … ) bit. Command substitution is neat but this is not really the best use-case for it.

    A better way is to use find with -print0 which makes it use a zero-byte instead of a newline character (otherwise the command will fail in case a filename contains a newline - newlines and spaces in filenames break a lot of scripts if you aren’t careful) and -quit, which makes find exit after the first match. And then pass it to xargs which is a utility that transfers stdin to command arguments - In this case the file binary - and handles zero-bytes as seperators when using the -0 arg.

    $ find /usr/lib -maxdepth 1 -type l -print0 -quit | xargs -0 file

    Removing -quit from the find command also works as expected (ie. on each found link).

    • emotional_soup_88@programming.devOP
      link
      fedilink
      English
      arrow-up
      7
      ·
      17 hours ago

      Thanks! So this was more about my lack of understanding of how the file command works… But anyway, this gave me some new stuff to study! Especially the -print0 and -quit options and the xargs command.