ahh

~$ which cat
you are viewing a single comment's thread
view the rest of the comments
[–] 86 points 1 week ago* (6 children)

cat is misunderstood a lot. The purpose of cat is to combine multiple files together (it's literally short for "concatenate"), like cat *.log to combine all log files together.

If you're just using it for a single file, then you should just redirect the file to stdin. These two command lines behave similarly:

cat foo.txt | some-command
some-command < foo.txt

For head, tail, grep and some other commands, you can just pass in the file name directly as an argument (e.g. grep whatever foo.txt).

  • source
  • hideshow 12 child comments
  • [–] 12 points 6 days ago (2 children)

    Fair, but in general I find it way more useful to use cat for piping.

    In most cases I'll do something like

    cat file | program
    cat file | program | grep
    cat file | program | grep | cut
    

    This gets more complicated if the file is at the end of the command.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 8 points 6 days ago (1 child)

    It wouldn't be at the end, since you'd still be piping into program

    program <file | grep | cut
    
  • source
  • parent
  • hideshow 2 child comments
  • [–] 14 points 6 days ago (3 children)

    And <file program | grep | cut works too

  • source
  • parent
  • hideshow 6 child comments
  • [–] 11 points 6 days ago (1 child)

    I still like doing cat file.txt | grep thing because I often re-run it to find something else like cat file.txt | grep thing2 - and if I do that with grep, the pattern sits awkwardly in the middle instead of at the end

  • source
  • parent
  • hideshow 2 child comments
  • [–] 75 points 1 week ago (1 child)
  • [–] [S] 18 points 1 week ago (2 children)

    awww what's a cat without a head and a tail?

  • source
  • parent
  • hideshow 4 child comments
  • [–] 18 points 1 week ago* (last edited 1 week ago) (4 children)

    If I just want to look at the file, is there a better way than cat foo.txt? I guess I can't just do < foo.txt

  • source
  • parent
  • hideshow 8 child comments
  • [–] 13 points 6 days ago* (last edited 6 days ago) (1 child)

    Admittedly I still use cat for this.

    This also works too, but it's more verbose:

    echo "$(<foo.txt)"
    

    It's mentioned in the Bash man pages:

    The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

    EDIT: I was just informed that simply <foo.txt works too.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 6 points 6 days ago (1 child)

    how in the world is this not a useless use of echo and equivalent to <foo.txt

  • source
  • parent
  • hideshow 2 child comments
  • [–] [S] 16 points 1 week ago (1 child)

    yeah sure but the command is literally written for the humor.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 19 points 1 week ago (2 children)

    I know, but I just wanted to mention this since a lot of new Linux users end up using cat this way :)

  • source
  • parent
  • hideshow 4 child comments