Another useful command used for viewing files is tail. Using tail you can view the last lines of 1
a file and by default it shows the last 10 lines. 2
tail/etc/ password. 3
These are the last 10 lines of the file, so the last 10 users. If it seems too basic, imagine that 4
you are most of the time interested in the last events that took place. 5
For example, the command I've just executed has shown the last 10 created users. 6
Tail has several options that can be used to get the desired output. 7
If you want to read a custom number of lines of a file, use -n and the number. 8
For example, I want to see the last two groups that were created : tail -n 2 / and the path 9
to the final etc/group 10
This is where the groups are saved. 11
And these are the two groups that are created. 12
Or if I want to read the last lines, starting with a specific line number, then I can use a command 13
like this tail -n +20 14
and the path to the file, for example/var/log/syslog It will display the last lines of the 15
file, starting with the line number 20. 16
So from line 20 to the end. 17
Tail is really useful for reading a file in real time, like when you want to view the last n lines 18
of a log file for important system messages. You use tail to watch log files as they are updated. 19
Using the -f option, tail automatically prints new messages from an open file to the screen 20
in real time; for example, to actively watch as users log in and out 21
I'll run the following command: 22
tail -f and the path to the file where authentication tries are saved 23
/var/log/auth.log 24
These are the last 10 lines of the file and the contents of the file will be updated in real time. And 25
in another terminal I will login into a shell as root so 26
sudo su 27
I'm entering the wrong password on purpose. 28
See how tail has updated the file in real time. 29
We can see the authentication failure message: the user student couldn't become root. 30
Now, I'll authenticate successfully. 31
And this is the message: session opened for user root by student. 32
(student to root). When you are finished with tail press ctr+c 33
The last command I'm going to show you is head. It’s the complementary of tail command, and as the name 34
implies, prints the top N number of lines from a given file. 35
Head is used to display the beginning of a file: head /etc/passwd 36
By default, it reads the first 10 lines of a file, so the previous command has printed out the first 37
10 users. You can change the number of lines displayed by specifying -n and the number or simply 38
the number of lines you want to see. 39
head -n 3 /etc/group, these are the first three groups or head -7 /etc/group 40
the first seven groups. 41
And at the end, I'll introduce you to another Linux command, which has nothing to do with viewing 42
files but could be very useful. if you want to run a command repeatedly displaying its output and error 43
you use the watch command. 44
It allows you to watch the program output change over time. 45
I'm running ls repeatedly. 46
watch ls 47
This is the contents of the current working directory, the user's home directory. By default, the ls command 48
is run every two seconds and watch will run until interrupted; to interrupted hold control and press c. 49
So ctr+c 50
Two interesting options are -d to highlight the differences between successive updates and -n 51
and a number to specify the update interval. I'm running the ls-l command every 3 52
seconds, highlighting the differences, watch -n 3 53
-d ls -l 54
And in another terminal, I'm creating a file in the same directory 55
touch abc.txt 56
See how it has highlighted the differences between updates. 57
And the last example, I want to see how packets are sent and received by executing the ifconfig repeatedly. 58
watch -n 1 -d ifconfig 59
I'm closing this terminal. You can generate some network traffic to see it clearer. I'm opening the 60
Firefox browser and visit some websites. 61
OK, see the differences; see how the number of received and sent packets is updated!