Files
mkdocs/docs/Linux/tail-head.md
2023-11-14 20:35:51 +01:00

172 lines
3.3 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Commandes tail - head
### Tail
#### Par défaut, tail affiche les 10 dernières lignes du fichier.
```bash
$ tail /var/log/messages
```
#### Afficher les 15 dernières lignes du fichier.
```bash
$ tail -n15 /var/log/messages
$ tail -15 /var/log/messages
$ tail -n 15 /var/log/messages
```
#### L'option -f affiche le fichier au fil de l'eau
```bash
$ tail -f /var/log/messages
```
Supprimer la 1ere ligne
```bash
$ tail -n+2
$ awk NR\>1
```
### Head
#### Afficher les 5 premières lignes du fichier.
```bash
$ head -5 /var/log/messages
$ head -n5 /var/log/messages
$ head -n 5 /var/log/messages
```
Supprime la dernière ligne
```bash
$ head -n -1
$ awk 'NR>1{print p}{p=$0}'
```
### MultiTail
[Multitail](https://vanheusden.com/multitail/manual.php)
#### Installation:
```bash
$ brew install multitail
```
Permet d'afficher plusieurs fichiers en meme temps:
```bash
$ multitail error_log access_log
```
Permet d'afficher 2 fichiers côte à côte:
```bash
$ multitail -s 2 error_log access_log
# sur 3 colonnes
$ multitail -s 3
```
ou **'v'** en cours d'exécution
Faire défiler (sur 100 lignes): **'b'**
Puis aller au début / à la fin: **gg** / **G**
Quitter le mode défilement: **'q'**
Pour changer le nb de lignes:
**-m <valeur>** pour le prochain fichier
**-M <valeur>** pour tous les fichiers
Quitter MultiTail: **'q'** ou **'ctrl-q'**
#### Fusionner 2 fichiers (ils s'affichent ensemble dans une seul fenêtre):
```bash
$ multitail error_log -I access_log
```
Fusionner 2 fichiers et afficher chacun d'une couleur différente:
```bash
$ multitail -ci green httpd/error_log -ci yellow -I php-fpm.log
```
Afficher le fichier en couleur:
```bash
$ multitail -F /usr/local/etc/multitail.conf -cS apache /usr/local/var/log/httpd/error_log
```
Les colors schemes sont tirés du fichier multitail.conf. Multitail recherche son fichier de config dans le répertoire courante, dans /etc. Sinon on peut spécifier le fichier avec l'option -F (`-F /usr/local/etc/multitail.conf`)
#### Visualiser la sortie de programmes externes:
```bash
$ multitail -l "ping localhost"
```
Visualiser un fichier log et un programme externe:
```bash
$ multitail error_log -l "ping localhost"
```
```bash
$ multitail error_log -s 2 -sn 1,3 -l "ping localhost" -l "ping localhost" -l "ping localhost"
```
#### Filtrer à l'aide de regexp (grep):
```bash
$ multitail -e '200' /usr/local/var/log/httpd/access_log
::1 - - [21/Dec/2019:11:39:17 +0100] "GET /info.php HTTP/1.1" 200 111908
::1 - - [21/Dec/2019:11:47:48 +0100] "GET /info.php HTTP/1.1" 200 111908
$ multitail -e '404' /usr/local/var/log/httpd/access_log
192.168.1.24 - - [02/Dec/2019:10:27:58 +0100] "GET /apple-touch-icon.png HTTP/1.1" 404 196
::1 - - [18/Dec/2019:06:17:28 +0100] "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 196
```
`-e 'test'`: n'affiche que les lignes qui contiennent 'test'
```bash
$ multitail -ev "200" /usr/local/var/log/httpd/access_log
192.168.1.24 - - [30/Nov/2019:08:47:26 +0100] "GET /wordpress/wp-admin/plugins.php HTTP/1.1" 302 -
192.168.1.24 - - [30/Nov/2019:08:47:28 +0100] "GET /favicon.ico HTTP/1.1" 404 196
```
`-ev 'test'`: n'affiche que les autres lignes
### lnav
[lnav](http://lnav.org/) ([doc](https://lnav.readthedocs.io/en/latest/))
Installation:
```bash
$ brew install lnav
```