Posted in Linux commands

Following log files

tail is a well known command to show the last lines of a file:

$ tail bmp280.log
2017-04-02T11:32:03 1491125523 20.62 1014.21
2017-04-02T11:33:02 1491125582 20.63 1014.23
2017-04-02T11:34:03 1491125643 20.62 1014.23
2017-04-02T11:35:03 1491125703 20.62 1014.23
2017-04-02T11:36:02 1491125762 20.63 1014.19
2017-04-02T11:37:03 1491125823 20.63 1014.23
2017-04-02T11:38:02 1491125882 20.63 1014.24
2017-04-02T11:39:04 1491125944 20.63 1014.25
2017-04-02T11:40:03 1491126003 20.63 1014.26
2017-04-02T11:41:02 1491126062 20.64 1014.27

If the default of ten lines is too few or too much you can adjust this with the parameter -n:

$ tail -n 25 bmp280.log
...

But the real power of tail are the parameters -f und -F. These parameters follow the end of the file while the file grows:

$ tail -f *.log
==> bmp280.log <==
2017-04-02T11:39:04 1491125944 20.63 1014.25
==> temp_humi.log <==
2017-04-02T11:39:05 1473343255 26.36 63.13 1 3.06
==> bmp280.log <==
2017-04-02T11:40:04 1491126004 20.63 1014.25
==> temp_humi.log <==
2017-04-02T11:40:05 1491126005 26.36 63.13 1 3.06

If the file does not exist yet, the parameter -F tells tail to wait until the file exists:

$ tail -F sensor_01.log
tail: cannot open ‘sensor_01.log’ for reading: No such file or directory

The message is just a warning. As soon as the log file is written, the lines appear.

Sometimes the lines with the filenames are difficult to find. But here grep is your friend:

$ tail -f *.log | grep -E --color '|==>.*<=='

Now the lines with the file names are coloured.

Posted in Linux commands

ssh: bypass /etc/hosts and DNS

A common little problem in my daily work is to log on to another machine with a cryptic filename, e.g. machine007.domain.com. First I have find out, that the web server is on that machine. How easy would it be to type just ssh webserver! The sysadmins don’t want to put this neither into the name service nor into /etc/hosts. But there is help.

Continue reading “ssh: bypass /etc/hosts and DNS”

Posted in Linux commands

“find” with Style, Part 1: executing commands

How to find files and do some action with them? E. g. you want to find log files and want to see their content. Or you want to delete .o files.
Every now and then you can see examples like

find . -name ‘*.log’ -exec less {} \;

What happens here? For every found file find will start a less with this single file.
Now suppose you have several log files you want to browse through.

Every time find finds a file a new less is started. In this case the additional load is negligible, but imagine you want to delete thousands of .o files (this will be handled in a later blog post).

The better way is to terminate the -exec with +:

find . -name ‘*.log’ -exec less {} +

What happens here? Find collects the file names (until the maximum of a command line is reached) and and starts one command with many file names. With this less example you can check this by opening the next or previous file – which is not possible with the first example above.