Il comando head stampa le prime righe di un file di testo. Per default mostra le prime 10 righe di ogni file sullo standard output. È utile anche quando viene usato insieme ad altri comandi tramite pipe.
1. Leggere da standard input
Se non indichi un file, head legge dall’input standard e mostra ciò che digiti.
2. Mostrare le prime 10 righe
head /etc/crontab3. Mostrare le prime N righe
head -n 11 /etc/crontabEquivale anche a:
head --lines 11 /etc/crontab4. Mostrare più file
Con più file, head stampa le prime righe di ciascuno e aggiunge header con il nome del file.
head /etc/crontab /etc/groupPer sopprimere gli header:
head -q /etc/crontab /etc/groupPer mostrare sempre gli header:
head -v /etc/crontab /etc/groupPuoi anche usare wildcard:
head -1 -q /etc/*.conf5. Stampare i primi N byte
head -c 70 /etc/groupPuoi usare unità più grandi:
head -c 2kB /etc/groupPer iniziare da un byte specifico:
head -c +50 /etc/groupPer escludere le ultime N righe puoi usare un numero negativo:
head -n -20 /var/log/syslog6. head con pipe
cat /var/log/dpkg.log | head -n 5 -v | sort -rPuoi anche combinare head e tail per estrarre una fascia di righe:
head -n20 /var/log/bootstrap.log | tail -n 10head funziona in modo opposto a tail: invece di mostrare le ultime righe, mostra le prime. Grazie alle pipe può essere integrato facilmente in pipeline di elaborazione testo.