GNU/Linux >> Linux Esercitazione >  >> Linux

Utilizzo di Cat Command in Linux con esempi

In Linux, il comando cat è uno dei comandi più comunemente usati. cat che sta per "concatenare", può leggere, scrivere e concatenare il contenuto del file allo standard output. Il comando cat viene solitamente utilizzato per visualizzare il contenuto di uno o più file di testo, combinare file aggiungendo il contenuto di un file a un altro e creare nuovi file.

In questo tutorial impareremo come utilizzare il comando cat al massimo delle sue potenzialità con esempi.

Sintassi generale

Sintassi:

$ cat [options] [filename/filenames]

Il [options] consente di utilizzare argomenti diversi per il comando.

[filename/filenames] è dove puoi specificare il nome di un file o più file che desideri visualizzare.

Crea un nuovo file

Usando il comando cat, puoi creare un nuovo file e aggiungere contenuto.

Sintassi:

$ cat > [filename]

Quando crei un file in questo modo, il cursore viene posizionato su una nuova riga in cui puoi digitare i tuoi contenuti. Dopo aver scritto il contenuto desiderato, puoi utilizzare Ctrl+D per terminare la modifica e salvarlo.

Ad esempio:

$ cat > Test1.txt

Uscita:

kali@kali:~/Desktop$ cat > Test1.txt
This is the first test file. 

Visualizza il contenuto di un file

Puoi usare il comando cat per visualizzare il contenuto di un file semplicemente digitando cat seguito dal nome del file.

Sintassi:

$ cat [filename]

Puoi usare more o less comando se si desidera visualizzare il file pagina per pagina.

Sintassi:

$ cat [filename] | more
$ cat [filename] | less

Ad esempio:

$ cat Test1.txt

Uscita:

kali@kali:~/Desktop$ cat Test1.txt 
This is the first test file.
kali@kali:~/Desktop$ 

Visualizza il contenuto di più file

Il comando cat può essere utilizzato per visualizzare il contenuto di più file alla volta. Puoi usare il comando cat seguito dall'elenco di nomi di file separati da spazi.

Sintassi:

$ cat [filename1] [filename2] ....

Esempio:

$ cat Test1.txt Test2.txt Test3.txt 

Uscita:

kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt 
This is the first test file.
This is second test file.
This is the third test file.
kali@kali:~/Desktop$ 

Copia contenuti da un file a un altro

Usando l'operatore (>) nel comando cat, possiamo copiare il contenuto da un file all'altro.

Sintassi:

$ cat [filename1] > [filename2]

Se il [filename2] non esiste, quindi il comando cat ne crea automaticamente uno nuovo e copia il file di [filename1] a [filename2] .

Ad esempio:

$ cat Test1.txt > Test2.txt

Uscita:

kali@kali:~/Desktop$ cat Test1.txt > Test2.txt 
kali@kali:~/Desktop$ cat Test2.txt 
This is the first test file.
kali@kali:~/Desktop$ 

Il comando copierà il contenuto da Test1.txt e lo sovrascrive in Test2.txt . Invece di sovrascriverlo, puoi anche aggiungere un file di testo di origine al file di testo di destinazione utilizzando l'operatore (>>).

Sintassi:

$ cat [filename1] >> [filename2]

Ad esempio:

$ cat Test1.txt >> Test2.txt

Uscita:

kali@kali:~/Desktop$ cat Test2.txt 
This is the second test file.
kali@kali:~/Desktop$ cat Test1.txt 
This is the first test file.
kali@kali:~/Desktop$ cat Test1.txt >> Test2.txt 
kali@kali:~/Desktop$ cat Test2.txt 
This is the second test file.
This is the first test file.
kali@kali:~/Desktop$ 

Visualizza il numero di riga in un file

Tutti i file non vuoti possono essere visualizzati usando il flag -b insieme al comando cat e al nome del file.

Sintassi:

$ cat -b [filename]

Ad esempio:

$ cat -b Test1.txt

Uscita:

kali@kali:~/Desktop$ cat -b Test1.txt 
  1  This is the first test file.
  2  This   3  is   4  test  5  file.
kali@kali:~/Desktop$ 

Se si desidera visualizzare anche le righe senza caratteri, è possibile utilizzare il flag -n insieme al comando cat e al nome del file.

Sintassi:

$ cat -n [filename]

Ad esempio:

$ cat -n Test1.txt

Uscita:

kali@kali:~/Desktop$ cat -n Test1.txt 
      1  This is the first test file.
      2
      3  This 
      4  is 
      5  test
      6
      7
      8  file.
      9
 kali@kali:~/Desktop$ 

Concatena uno o più file

Se vuoi visualizzare il contenuto di più file contemporaneamente, usa il comando cat per concatenarli.

Sintassi:

$ cat [filename1] [filename2]...

Ad esempio:

$ cat Test1.txt Test2.txt Test3.txt

Uscita:

kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt 
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ 

Puoi anche combinare più file in un unico file creando un nuovo file o aggiornandone uno esistente utilizzando l'operatore (>).

Sintassi:

$ cat [filename1] [filename2]... > [filename3]

Ad esempio:

$ cat Test1.txt Test2.txt Test3.txt > Test4.txt

Da Test4.txt non esiste, crea e un nuovo file chiamato Test4.txt e concatena il contenuto di Test1.txt , Test2.txt e Test3.txt in Test4.txt .

Uscita:

kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt > Test4.txt
kali@kali:~/Desktop$ cat Test4.txt 
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ 

Visualizza fine di ogni riga in un file

Puoi determinare la fine di una riga in un file usando il comando cat. A volte ci sono caratteri nascosti come spazi alla fine di una riga che possono causare errori o scoprire problemi. Puoi usare il comando cat con il flag -E per mostrare dollaro ($) come carattere di fine riga.

Sintassi:

$ cat -E [filename]

Ad esempio:

$ cat -E Test4.txt

Uscita:

kali@kali:~/Desktop$ cat -E Test4.txt 
This is the first test file.$
$
$
This is the second test file.$
$
$
This is the first test file.$
$
$
This is the third test file.$
kali@kali:~/Desktop$ 

Riduci le righe vuote

Quando visualizzi il contenuto di un file, potrebbe essere fastidioso vedere molte righe vuote. Il comando cat insieme a -s può essere utilizzato per rimuovere righe vuote ripetute dall'output. L'opzione -s nel comando cat mostra solo una riga vuota e comprime quelle ripetute.

Sintassi:

$ cat -s [filename]

Ad esempio:

$ cat -s Test4.txt

Uscita:

kali@kali:~/Desktop$ cat Test4.txt 
This is the first test file.





This is the second test file.




This is the first test file.




This is the third test file.

kali@kali:~/Desktop$ cat -s Test4.txt 
This is the first test file.

This is the second test file.

This is the first test file.

This is the third test file.
kali@kali:~/Desktop$  

Il primo output è senza utilizzare l'opzione -s e il secondo output è dopo aver utilizzato l'opzione -s.

Mostra schede

L'opzione -T insieme al comando cat visualizza il contenuto del file e lo spazio di tabulazione all'interno del testo.
Gli spazi di tabulazione sono indicati dal simbolo ^I.

Sintassi:

$ cat -T [filename]

Ad esempio:

$ cat -T Test4.txt

Uscita:

kali@kali:~/Desktop$ cat Test4.txt 
         This is the first test file.
This is the second test file.
         This is the first test file.
This is the     third test file.

kali@kali:~/Desktop$ cat -T Test4.txt 
^IThis is the first test file.
This is the second test file.
^IThis is the first test file.
This is the ^Ithird test file.
kali@kali:~/Desktop$ 

Il primo output è senza utilizzare l'opzione -T e il secondo output è dopo aver utilizzato l'opzione -T.

Visualizza il contenuto di un file in ordine inverso

Il comando tac è l'inverso del comando cat. tac visualizzerà l'output nell'ordine inverso del contenuto del file di testo.

Sintassi:

$ tac [filename]

Ad esempio:

$ tac Test4.txt

Uscita:

kali@kali:~/Desktop$ cat Test4.txt 
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.

kali@kali:~/Desktop$ tac Test4.txt 
This is the third test file.
This is the first test file.
This is the second test file.
This is the first test file.

Il primo output si ottiene con il comando cat e il secondo output si ottiene con il comando tac.

Usa il comando help se vuoi saperne di più sul comando cat o se hai qualche confusione.

$ cat --help

Uscita:

kali@kali:~$ cat --help
Usage: cat [OPTION]… [FILE]…
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
 -A, --show-all           equivalent to -vET
   -b, --number-nonblank    number nonempty output lines, overrides -n
   -e                       equivalent to -vE
   -E, --show-ends          display $ at end of each line
   -n, --number             number all output lines
   -s, --squeeze-blank      suppress repeated empty output lines
   -t                       equivalent to -vT
   -T, --show-tabs          display TAB characters as ^I
   -u                       (ignored)
   -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
       --help     display this help and exit
       --version  output version information and exit
Examples:
   cat f - g  Output f's contents, then standard input, then g's contents.
   cat        Copy standard input to standard output.
kali@kali:~$ 

Conclusione

In questo tutorial, abbiamo appreso del comando cat, del suo utilizzo con varie opzioni ed esempi. Cat è un comando utile che ti consente di creare e visualizzare più tipi di file di testo. È possibile visualizzare più file contemporaneamente in più modi utilizzando il comando cat.


Linux
  1. Ordina il comando in Linux con esempi

  2. Comando JQ in Linux con esempi

  3. Esempi importanti di comandi Cat in Linux

  4. 14 Utili esempi di comandi "cat" in Linux

  5. ln Esempi di comandi in Linux

Comando Linux WC con esempi

Utilizzo del comando Linux mv con esempi

Esempi di comandi di Linux cat

Comando di montaggio Linux con esempi

Comando gawk Linux con esempi

Linux make Command con esempi