command: cat

Discussion in 'Beginner Tutorials' started by Rob, Nov 3, 2011.

  1. Rob

    Rob Administrator Staff Member

    Messages:
    575
    Likes Received:
    224
    Trophy Points:
    43
    The cat command will allow you to see an entire file in your terminal quickly. This is more useful for smaller files unless you're grepping for certain things.

  2. Rob

    Rob Administrator Staff Member

    Messages:
    575
    Likes Received:
    224
    Trophy Points:
    43
    cat command examples:

    show the contents of /etc/passwd while searching for the user 'rob'
    Code:
    [root@server ~]# cat /etc/passwd|grep rob
    rob:x:500:500::/home/rob:/bin/bash
    Fareed Ahmed likes this.
  3. MustangV10

    MustangV10 New Member

    Messages:
    87
    Likes Received:
    3
    Trophy Points:
    0
    So cat basically lets you view the contents of a file, correct?
  4. flotwig

    flotwig New Member

    Messages:
    23
    Likes Received:
    1
    Trophy Points:
    0
    That's correct.
    It's especially useful for "piping" the contents of files into other commands. For example:
    "filter-program" would receive the contents of somefile.bin as standard input. It's difficult to give a real-world example, but it really is a useful command.
  5. grim76

    grim76 Active Member Staff Writer

    Messages:
    177
    Likes Received:
    48
    Trophy Points:
    28
    Wouldn't it be easier to:

    $ grep -i rob /etc/passwd
  6. flotwig

    flotwig New Member

    Messages:
    23
    Likes Received:
    1
    Trophy Points:
    0
    Yeah, but some utilities don't accept filenames as arguments, just standard input. Also, cat can concatenate several files together, which not all utilities support natively.
  7. DarthVader3257

    DarthVader3257 New Member

    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    I thought there were alternative ways in doing this? Sounds like it'll work, so I might give a shot soon. Usually there are other methods in completing this, though.
  8. Darwin

    Darwin New Member

    Messages:
    115
    Likes Received:
    3
    Trophy Points:
    0
    Ha so this would be useful for redirecting the contents of a file to another, however the first could be password protected and only certain info could be filtered to a particular user? Am I able to use this for that?
  9. Stefano Messicano

    Stefano Messicano New Member

    Messages:
    88
    Likes Received:
    3
    Trophy Points:
    0
    For an example of using the cat command:I have a file named Linux-Friends.txt and in this text file I have some friend’s names, phone numbers, and e-mail addresses that run Linux. So now I want to get someone’s phone number from this list. I can run cat Linux-Friends.txt in a Linux terminal and I will get the contents of this file listed on my screen.
  10. Piet

    Piet New Member

    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    3
    I have a good example for showing hosts:
    Code:
    13:39 root@forseti: /etc $ cat hosts
    
    127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1        localhost localhost.localdomain localhost6 localhost6.localdomain6
    
    This is the output directly in the console.
  11. Fareed Ahmed

    Fareed Ahmed New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    I am new member to linux, I made file rani
    there are different contents of file how can i check contents?
  12. kri

    kri New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    i din't know how to use cat command...can anyone tell me???
    and what are the uses of cat command(i.e what can we do with cat command?)
  13. harikrishna

    harikrishna New Member

    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    3
    1.cat command useful to create files

    By giving cat > filename

    2.cat command used to concatenate the files together.

    Example
    suppose you have created three files namely L1,L2,L3. and you want to view the entire content of three files in file L4 follow the syntax

    cat L1 L2 L3 > L4

    and you done

    3.cat command is used to view the content of file

    4.But the principle usage of cat is PIPING the contents of files into other commands.
    oenghus likes this.
  14. flunwyc

    flunwyc Member

    Messages:
    99
    Likes Received:
    61
    Trophy Points:
    18
    In such cases, man pages are invaluable
    Code:
    $ man cat
    zcat is good for dealing with gzipped files (e.g. kernel patches).
  15. Ravi

    Ravi New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    If I want to append(add) some records/ data to an existing .txt file which command should i use?
    i am new to using terminal
  16. Maverick1

    Maverick1 New Member

    Messages:
    24
    Likes Received:
    5
    Trophy Points:
    3
    lobo likes this.
  17. harikrishna

    harikrishna New Member

    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    3
    yes u can by giving a double append(strong append in linux terms) like cat >> existing file name
  18. randrums

    randrums New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    I really appreciate this website and all of the information everyone shares. This is what keeps linux strong.
  19. MES3OUD

    MES3OUD New Member

    Messages:
    6
    Likes Received:
    2
    Trophy Points:
    1
    Thanks man for this tutorial ;)
  20. Saptarshi Nag

    Saptarshi Nag Member

    Messages:
    42
    Likes Received:
    28
    Trophy Points:
    18
    Would you please elaborate 'cat'? :D

Share This Page