Gestione efficace dei file utilizzando i comandi head, tail e cat in Linux

Ci sono diversi comandi e programmi forniti da Linux per visualizzare il contenuto dei file. Lavorare con i file è uno dei compiti più impegnativi, che la maggior parte degli utenti informatici, che siano principianti, utenti regolari, utenti avanzati, sviluppatori, amministratori, ecc., svolge. Lavorare con i file in modo efficace ed efficiente è un’arte.

Manage Files in Linux

Oggi, in questo articolo, parleremo dei comandi più popolari chiamati head, tail e cat, la maggior parte di noi è già a conoscenza di tali comandi, ma in pochi li implementano quando necessario.

1. Comando head

Il comando head legge le prime dieci righe di un qualsiasi file dato. La sintassi di base del comando head è:

head [options] [file(s)]

Ad esempio, il seguente comando visualizzerà le prime dieci righe del file chiamato ‘/etc/passwd‘.

# head /etc/passwd 

root:x:0:0:root:/root:/bin/bash 
daemon:x:1:1:daemon:/usr/sbin:/bin/sh 
bin:x:2:2:bin:/bin:/bin/sh 
sys:x:3:3:sys:/dev:/bin/sh 
sync:x:4:65534:sync:/bin:/bin/sync 
games:x:5:60:games:/usr/games:/bin/sh 
man:x:6:12:man:/var/cache/man:/bin/sh 
lp:x:7:7:lp:/var/spool/lpd:/bin/sh 
mail:x:8:8:mail:/var/mail:/bin/sh 
news:x:9:9:news:/var/spool/news:/bin/sh

Se vengono forniti più di un file, head mostrerà le prime dieci righe di ciascun file separatamente. Ad esempio, il seguente comando mostrerà dieci righe di ciascun file.

# head /etc/passwd /etc/shadow

==> /etc/passwd <== root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin ==> /etc/shadow <==
root:$6$85e1:15740:0:99999:7:::
bin:*:15513:0:99999:7:::
daemon:*:15513:0:99999:7:::
adm:*:15513:0:99999:7:::
lp:*:15513:0:99999:7:::
sync:*:15513:0:99999:7:::
shutdown:*:15513:0:99999:7:::
halt:*:15513:0:99999:7:::
mail:*:15513:0:99999:7:::
uucp:*:15513:0:99999:7:::

Se si desidera recuperare un numero maggiore di righe rispetto al valore predefinito di dieci, allora si utilizza l’opzione ‘-n‘ insieme a un numero intero che indica il numero di righe da recuperare. Ad esempio, il seguente comando visualizzerà le prime 5 righe dal file ‘/var/log/yum.log‘.

# head -n5 /var/log/yum.log

Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch

In realtà, non c’è bisogno di utilizzare l’opzione ‘-n‘. Basta il trattino e specificare l’intero senza spazi per ottenere lo stesso risultato del comando precedente.

# head  -5 /var/log/yum.log

Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch

Il comando head può anche visualizzare qualsiasi numero desiderato di byte utilizzando l’opzione ‘-c‘ seguita dal numero di byte da visualizzare. Ad esempio, il seguente comando visualizzerà i primi 45 byte del file specificato.

# head -c45 /var/log/yum.log

Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el

2. Comando tail

Il comando tail ti consente di visualizzare le ultime dieci linee di qualsiasi file di testo. Simile al comando head sopra, il comando tail supporta anche le opzioni ‘n’ numero di linee e ‘n’ numero di caratteri.

La sintassi di base del comando tail è:

# tail [options] [filenames]

Ad esempio, il seguente comando stamperà le ultime dieci linee di un file chiamato ‘access.log‘.

# tail access.log 

1390288226.042      0 172.16.18.71 TCP_DENIED/407 1771 GET http://download.newnext.me/spark.bin? - NONE/- text/html
1390288226.198      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.210   1182 172.16.20.44 TCP_MISS/200 70872 GET http://mahavat.gov.in/Mahavat/index.jsp pg DIRECT/61.16.223.197 text/html
1390288226.284     70 172.16.20.44 TCP_MISS/304 269 GET http://mahavat.gov.in/Mahavat/i/i-19.gif pg DIRECT/61.16.223.197 -
1390288226.362    570 172.16.176.139 TCP_MISS/200 694 GET http://p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-v4.metric.gstatic.com/v6exp3/redir.html pg 
1390288226.402      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.437    145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.445      0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.605      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.808      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html

Se vengono forniti più file, tail stamperà le ultime dieci linee di ciascun file come mostrato di seguito.

# tail access.log error.log

==> access.log <== 1390288226.042      0 172.16.18.71 TCP_DENIED/407 1771 GET http://download.newnext.me/spark.bin? - NONE/- text/html 1390288226.198      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.210   1182 172.16.20.44 TCP_MISS/200 70872 GET http://mahavat.gov.in/Mahavat/index.jsp pg DIRECT/61.16.223.197 text/html 1390288226.284     70 172.16.20.44 TCP_MISS/304 269 GET http://mahavat.gov.in/Mahavat/i/i-19.gif pg DIRECT/61.16.223.197 - 1390288226.362    570 172.16.176.139 TCP_MISS/200 694 GET http://p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-v4.metric.gstatic.com/v6exp3/redir.html pg  1390288226.402      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.437    145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.445      0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.605      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.808      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html ==> error_log <==
[Sun Mar 30 03:16:03 2014] [notice] Digest: generating secret for digest authentication ...
[Sun Mar 30 03:16:03 2014] [notice] Digest: done
[Sun Mar 30 03:16:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.0-fips configured -- resuming normal operations

Allo stesso modo, è possibile anche stampare le ultime poche linee utilizzando l’opzione ‘-n‘ come mostrato di seguito.

# tail -5 access.log

1390288226.402      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.437    145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.445      0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.605      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.808      0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html

È inoltre possibile stampare il numero di caratteri utilizzando l’argomento ‘-c’ come mostrato di seguito.

# tail -c5 access.log

ymantec.com:443 - NONE/- text/html

3. Comando cat

Il comando ‘cat‘ è lo strumento più ampiamente utilizzato, universale. Copia l’input standard all’output standard. Il comando supporta lo scrolling, se il file di testo non si adatta allo schermo attuale.

La sintassi di base del comando cat è:

# cat [options] [filenames] [-] [filenames]

L’uso più frequente di cat è per leggere il contenuto dei file. Tutto ciò che è necessario fare per aprire un file per la lettura è digitare cat seguito da uno spazio e il nome del file.

# cat /etc/passwd 

root:x:0:0:root:/root:/bin/bash 
daemon:x:1:1:daemon:/usr/sbin:/bin/sh 
bin:x:2:2:bin:/bin:/bin/sh 
sys:x:3:3:sys:/dev:/bin/sh 
sync:x:4:65534:sync:/bin:/bin/sync 
games:x:5:60:games:/usr/games:/bin/sh 
man:x:6:12:man:/var/cache/man:/bin/sh 
lp:x:7:7:lp:/var/spool/lpd:/bin/sh 
…

Il comando cat viene anche utilizzato per concatenare un numero di file insieme.

# echo 'Hi Tecmint-Team' > 1 
# echo 'Keep connected' > 2 
# echo 'Share your thought' > 3 
# echo 'connect us [email protected]' > 4
# cat 1 2 3 4 > 5
# cat 5 

Hi Tecmint-Team 
Keep connected 
Share your thought 
connect us [email protected]

Può essere utilizzato anche per creare file. È possibile farlo eseguendo il comando cat seguito dall’operatore di redirezione dell’output e dal nome del file da creare.

# cat > tecmint.txt

Tecmint is the only website fully dedicated to Linux.

È possibile avere un terminatore personalizzato per il comando ‘cat’. Ecco come è implementato.

# cat > test.txt << end 

I am Avishek 
Here i am writing this post 
Hope your are enjoying 
end
# cat test.txt 

I am Avishek 
Here i am writing this post 
Hope your are enjoying

Non sottovalutare il potere del comando ‘cat’ che può essere utile per copiare file.

# cat avi.txt

I am a Programmer by birth and Admin by profession
# cat avi.txt > avi1.txt
# cat avi1.txt

I am a Programmer by birth and Admin by profession

Ora qual è l’opposto di cat? Sì, è ‘tac’. ‘tac’ è un comando in Linux. È meglio mostrare un esempio di ‘tac’ piuttosto che parlarne.

Creare un file di testo con i nomi di tutti i mesi, in modo che una parola appaia su ogni riga.

# cat month

January
February
March
April
May
June
July
August
September
October
November
December
# tac month

December
November
October
September
August
July
June
May
April
March
February
January

Per ulteriori esempi sull’uso del comando cat, fare riferimento al 13 Utilizzi del Comando cat

Questo è tutto per ora. Tornerò presto con un altro Articolo Interessante, degno di essere Conosciuto. Nel frattempo, rimanete sintonizzati e connessi a Tecmint. Non dimenticate di fornirci il vostro prezioso feedback nella nostra sezione commenti.

Source:
https://www.tecmint.com/view-contents-of-file-in-linux/