Glen Knight

NYC Based IT Professional

Reading From Files Using The Linux CLI

We can read from files using the cat command and the output is written to STDOUT. We can also use a pipe and redirect output elsewhere

[glen.knight@expert-syn-srv-1 t1]$ cat f1
    hello

We can count the number of lines in a file using the command wc -l

[glen.knight@expert-syn-srv-1 t1]$ wc -l /etc/services
    11176 /etc/services

We can page through long files using the less command

[glen.knight@expert-syn-srv-1 t1]$ less !$

**Note: the !$ command represents the last argument used, which in this case is the file /etc/services

We can use the head command to read the beginning of a file and tail to read the bottom of a file. We can use the -n switch to specify the number of lines we want to read.

[glen.knight@expert-syn-srv-1 t1]$ head -n 5 /etc/services
    # /etc/services:
    # $Id: services,v 1.55 2013/04/14 ovasik Exp $
    #
    # Network services, Internet style
    # IANA services version: last updated 2013-04-10
[glen.knight@expert-syn-srv-1 t1]$ tail -n 5 /etc/services
    com-bardac-dw 48556/tcp # com-bardac-dw
    com-bardac-dw 48556/udp # com-bardac-dw
    iqobject 48619/tcp # iqobject
    iqobject 48619/udp # iqobject
    matahari 49000/tcp # Matahari Broker

Leave a Reply

Your email address will not be published. Required fields as marked *.