Head Linux ✓ Comando linux head con ejemplos prácticos

head-linux

Head Linux, para que sirve?:

El comando headde linux, es un comando utilizado para mostrar las primeras líneas de cualquier fichero. Normalmente lo usamos para visualizar los primeros log de error, para analizar cualquier problema en la ejecución de una web, para visualizar el syslog, archivo log de sistema, etc… La forma más simple de usarlo es:

head /var/log/syslog

Nos muestra las primeras 10 lineas del archivo syslog.

Head en linux, mostrar las primeras -n líneas:

Si queremos mostrar un número concreto de líneas de un archivo, usaremos el comando head -n archivo:

head -n4 /var/log/syslog

Nos va a mostrar por pantalla las primeras 4 líneas del archivo syslog.

Head -v linux verbose, muestra el header con el nombre del fichero:

Si queremos mostrar un header con el nombre del archivo, utilizamos la opción -v (verbose):

head -vn1 /var/log/syslog

head -vn1 /var/log/syslog
==> /var/log/syslog <==
Jun 19 06:22:29 ns357 postfix/smtpd[28955]: disconnect from unknown[212.70.149.82] ehlo=1 auth=0/1 rset=1 quit=1 commands=3/4

Comando head linux en orden inverso:

Si queremos mostrar en primero la última línea del archivo, utilizaremos sort para invertir el orden de salida:

head -n4 /var/log/syslog | sort -r

Jun 19 06:22:30 ns357 named[788]: ../../../../lib/isc/unix/socket.c:2104: unexpected error:
Jun 19 06:22:30 ns357 named[788]: internal_send: 46.229.168.163#55796: Invalid argument
Jun 19 06:22:30 ns357 named[788]: client 46.229.168.163#55796 (inmodemo.casas-madrid.com): error sending response: invalid file
Jun 19 06:22:29 ns357 postfix/smtpd[28955]: disconnect from unknown[212.70.149.82] ehlo=1 auth=0/1 rset=1 quit=1 commands=3/4

Vemos por la hora, que primero muestra la última línea del archivo.

EAD(1) User Commands HEAD(1)

NAME
head – output the first part of files

SYNOPSIS
head [OPTION]… [FILE]…

DESCRIPTION
Print the first 10 lines of each FILE to standard output. With more
than one FILE, precede each with a header giving the file name.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options
too.

-c, –bytes=[-]NUM
print the first NUM bytes of each file; with the leading ‘-‘,
print all but the last NUM bytes of each file

-n, –lines=[-]NUM
print the first NUM lines instead of the first 10; with the
leading ‘-‘, print all but the last NUM lines of each file

-q, –quiet, –silent
never print headers giving file names

-v, –verbose
always print headers giving file names

-z, –zero-terminated
line delimiter is NUL, not newline

–help display this help and exit

–version
output version information and exit

NUM may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000,
M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P,
E, Z, Y.

AUTHOR
Written by David MacKenzie and Jim Meyering.

REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report head translation bugs to <http://translationproject.org/team/>

COPYRIGHT
Copyright © 2016 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
tail(1)

Full documentation at: <http://www.gnu.org/software/coreutils/head>
or available locally via: info ‘(coreutils) head invocation’

Si queremos mostrar las últimas líneas en vez de las primeras líneas de un archivo, usaremos el comando Tail de linux.

head
Esta web utiliza cookies propias para su correcto funcionamiento. Al hacer clic en el botón Aceptar, aceptas el uso de estas tecnologías y el procesamiento de tus datos para estos propósitos. Más información
Privacidad
Abrir chat
¿Podemos ayudarte?