Examples for head, tail, cat in linux?



head

You can use head to display the first ten lines of a file.
root@linux:~$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
root@linux:~$
The head command can also display the first n lines of a file.
root@linux:~$ head -4 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh

Head can also display the first n bytes.
root@linux:~$ head -c4 /etc/passwd
rootroot@linux:~$
tail

Similar to head, the tail command will display the last ten lines of a file.
root@linux:~$ tail /etc/services
vboxd 20012/udp
binkp 24554/tcp # binkp fidonet protocol
asp 27374/tcp # Address Search Protocol
asp 27374/udp
csync2 30865/tcp # cluster synchronization tool
dircproxy 57000/tcp # Detachable IRC Proxy
tfido 60177/tcp # fidonet EMSI over telnet
fido 60179/tcp # fidonet EMSI over TCP
# Local services
root@linux:~$

You can give tail the number of lines you want to see.
$ tail -3 count.txt
six
seven
eight
cat

The cat command is one of the most universal tools. All it does is copy standard
input to standard output. In combination with the shell this can be very powerful and
diverse. Some examples will give a glimpse into the possibilities. The first example
is simple, you can use cat to display a file on the screen. If the file is longer than the
screen, it will scroll to the end.
root@linux:~$ cat /etc/resolv.conf
nameserver 194.7.1.4
root@linux:~$
concatenate

cat is short for concatenate. One of the basic uses of cat is to concatenate files into
a bigger (or complete) file.
root@linux:~$ echo one > part1
root@linux:~$ echo two > part2
root@linux:~$ echo three > part3
root@linux:~$ cat part1 part2 part3
one
two
three
root@linux:~$

create files

You can use cat to create flat text files. Type the cat > winter.txt command as shown
in the screenshot below. Then type one or more lines, finishing each line with the
enter key. After the last line, type and hold the Control (Ctrl) key and press d.
root@linux:~/test$ cat > winter.txt
It is very cold today!
root@linux:~/test$ cat winter.txt
It is very cold today!
root@linux:~/test$

The Ctrl d key combination will send an EOF (End of File) to the running process
ending the cat command

custom end marker

You can choose an end marker for cat with << as is shown in this screenshot. This
construction is called a here directive and will end the cat command.
root@linux:~/test$ cat > hot.txt <<stop
> It is hot today!
> Yes it is summer.
> stop
root@linux:~/test$ cat hot.txt
It is hot today!
Yes it is summer.
root@linux:~/test$
copy files

In the third example you will see that cat can be used to copy files. We will explain
in detail what happens here in the bash shell chapter.
root@linux:~/test$ cat winter.txt
It is very cold today!
root@linux:~/test$ cat winter.txt > cold.txt
root@linux:~/test$ cat cold.txt
It is very cold today!
root@linux:~/test
#tac

Just one example will show you the purpose of tac (as the opposite of cat).
root@linux:~/test$ cat count
one
two
three
four
root@linux:~/test$ tac count
four
three
two
one
root@linux:~/test$
strings

With the strings command you can display readable ascii strings found in (binary)
files. This example locates the ls binary then displays readable strings in the binary
file (output is truncated).
root@linux:~$ which ls
/bin/ls
root@linux:~$ strings /bin/ls
/lib/ld-linux.so.2
librt.so.1
__gmon_start__
_Jv_RegisterClasses
clock_gettime
libacl.so.1
...

No comments:

Post a Comment