GNU/Linux >> Linux Esercitazione >  >> Linux

Comandi di gestione dei file Linux per creare, copiare, spostare ed eliminare file

Questo articolo descrive alcuni comandi introduttivi per la gestione dei file di Linux® per visualizzare, creare, copiare, spostare ed eliminare file e directory.

Visualizza directory e file

Per visualizzare i file in una directory, usa ls comando.

Per visualizzare il contenuto di un file, usa il cat comando.

ls comando

Puoi usare ls comando per mostrare il contenuto di una directory. ls opzioni di comando, come ls -lah , fornire ulteriori informazioni. Includono un elenco di tutti i file (inclusi i file nascosti) in una visualizzazione a lungo elenco leggibile dall'uomo.

Sintassi :ls directoryname

L'esempio seguente mostra l'elenco di una directory vuota. ls non restituisce nessun file, mentre ls -lah restituisce file e directory nascosti. Le directory vengono visualizzate con una riga che inizia con d .

[root@server-01 testdir]# ls
[root@server-01 testdir]#
[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:46 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..

cat comando

Il cat comando mostra il contenuto di un file.

Sintassi :cat filename

L'esempio seguente mostra come visualizzare i contenuti di Importante file con il cat comando:

[root@server-01 testdir]# cat Important
DON'T DELETE THIS TEXT.

Crea un file

Puoi creare file utilizzando i seguenti comandi:

  • touch
  • cat >
  • >

touch comando

Il touch comando crea file vuoti.

Sintassi :touch newfilename

L'esempio seguente utilizza il tocco comando per creare i nuovi file, demo e campione.txt :

[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:49 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
[root@server-01 testdir]# touch demo
[root@server-01 testdir]# touch sample.txt
[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:50 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:50 demo
-rw-r--r--. 1 root root    0 Apr 14 01:50 sample.txt

Puoi anche utilizzare il tocco comando per creare più file con un unico comando.

L'esempio seguente utilizza il tocco comando per creare i nuovi file, campione1 , campione2 e campione3 :

[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:50 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:50 demo
-rw-r--r--. 1 root root    0 Apr 14 01:50 sample.txt
[root@server-01 testdir]# touch sample1 sample2 sample3
[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:52 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:50 demo
-rw-r--r--. 1 root root    0 Apr 14 01:52 sample1
-rw-r--r--. 1 root root    0 Apr 14 01:52 sample2
-rw-r--r--. 1 root root    0 Apr 14 01:52 sample3
-rw-r--r--. 1 root root    0 Apr 14 01:50 sample.txt

cat > comando

Il cat > comando crea un file non vuoto inserendo il contenuto dopo il segno maggiore di.

Sintassi :cat > text-to-be-added

L'esempio seguente usa cat > seguito dal contenuto per creare il nuovo file, test.txt , con una riga di contenuto:

[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:57 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
[root@server-01 testdir]# cat > test.txt
This is only a test.
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:57 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:57 test.txt

> comando

Sintassi :> newfilename

Il simbolo di reindirizzamento standard, > , crea un unico nuovo file senza alcun contenuto o sostituisce un file esistente con un file vuoto con lo stesso nome.

ATTENZIONE: Dovresti usare il simbolo di reindirizzamento con attenzione perché puoi sovrascrivere accidentalmente i file esistenti. Questi cambiamenti sono permanenti. Non è possibile recuperare i contenuti precedenti.

L'esempio seguente usa > per creare il nuovo file, example.txt :

[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 2 root root 4.0K Apr 14 01:59 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt
[root@server-01 testdir]# > example.txt
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 2 root root 4.0K Apr 14 02:04 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt

L'esempio seguente mostra come puoi riscrivere accidentalmente il contenuto del file esistente, Importante :

[root@server-01 testdir]# cat Important
DON'T DELETE THIS TEXT.
[root@server-01 testdir]# > Important
[root@server-01 testdir]# cat Important
[root@server-01 testdir]#

Nota: Come accennato in precedenza, il simbolo di reindirizzamento può essere riscritto, come mostrato nell'esempio precedente, senza alcuna opzione per recuperare i dati persi. Se riscrivi file critici, ciò può causare problemi catastrofici.

Tuttavia, puoi utilizzare due simboli di reindirizzamento, >> , per aggiungere contenuto alla fine di un file. Se non esiste alcun file, >> crea il file e aggiunge il contenuto. Se il file esiste già, >> aggiunge il nuovo contenuto alla fine del file.

Sintassi :>> filename

L'esempio seguente usa >> per aggiungere il contenuto di change.txt alla fine del file, edit.txt . Il >> il comando impedisce una riscrittura completa di edit.txt .

[root@server-01 testdir]# cat edit.txt
Examples
Are
[root@server-01 testdir]# cat change.txt
Great
[root@server-01 testdir]# cat change.txt >> edit.txt
[root@server-01 testdir]# cat edit.txt
Examples
Are
Great

Crea una directory

Usa mkdir* per creare una directory vuota.

Sintassi :mkdir new-dirname

L'esempio seguente usa mkdir per creare le nuove directory, cartella1 e cartella2 :

[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 2 root root 4.0K Apr 14 03:14 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
-rw-r--r--. 1 root root    0 Apr 14 02:10 Important
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt
[root@server-01 testdir]# mkdir folder1
[root@server-01 testdir]# mkdir folder2/
[root@server-01 testdir]# ls -lah
total 20K
drwxr-xr-x. 4 root root 4.0K Apr 14 03:15 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder2
-rw-r--r--. 1 root root    0 Apr 14 02:10 Important
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt

Puoi anche usare mkdir comando per creare più directory con un unico comando.

L'esempio seguente usa mkdir per creare le nuove directory, cartellaA , cartellaB e cartellaC :

[root@server-01 testdir]# mkdir folderA folderB folderC
[root@server-01 testdir]# ls -lah
total 32K
drwxr-xr-x. 7 root root 4.0K Apr 14 03:16 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder2
drwxr-xr-x. 2 root root 4.0K Apr 14 03:16 folderA
drwxr-xr-x. 2 root root 4.0K Apr 14 03:16 folderB
drwxr-xr-x. 2 root root 4.0K Apr 14 03:16 folderC
-rw-r--r--. 1 root root    0 Apr 14 02:10 Important
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
-rw-r--r--. 1 root root   21 Apr 14 01:58 test.txt

Copia un file o una directory

Il cp comando copia un file esistente in un nuovo file.

Sintassi :cp orig-filename new-filename

L'esempio seguente usa il cp comando per creare un nuovo file, samplecopy.txt , dal file esistente, sample.txt :

[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:19 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
[root@server-01 testdir]# cp sample.txt samplecopy.txt
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:29 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
-rw-r--r--. 1 root root    0 Apr 14 03:29 samplecopy.txt
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt

L'esempio seguente usa cp -r per creare una nuova directory, copiacartella1 , da una directory esistente, cartella1 :

[root@server-01 testdir]# cp -r folder1 copyfolder1
[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 4 root root 4.0K Apr 14 03:32 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 03:32 copyfolder1
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
-rw-r--r--. 1 root root    0 Apr 14 03:29 samplecopy.txt
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt

Sposta un file o una directory

Puoi usare il mv comando per spostare un file in un'altra directory.

Sintassi :mv filename destination

L'esempio seguente usa mv comando per spostare sample.txt dalla directory corrente alla cartella1 directory:

[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:58 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root   19 Apr 14 03:49 edit.txt
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:15 folder1
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt
[root@server-01 testdir]# mv sample.txt folder1/
[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:58 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root   19 Apr 14 03:49 edit.txt
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 folder1
[root@server-01 testdir]# cd folder1
[root@server-01 folder1]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 .
drwxr-xr-x. 3 root root 4.0K Apr 14 03:58 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 sample.txt

Come puoi vedere, sample.txt spostato dalla directory precedente e ora viene visualizzato nella cartella1 directory.

Puoi anche usare il mv comando per rinominare file o directory esistenti.

L'esempio seguente usa mv comando per rinominare la demo file in nuova demo :

[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 03:58 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root    0 Apr 14 01:57 demo
-rw-r--r--. 1 root root   19 Apr 14 03:49 edit.txt
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 folder1
[root@server-01 testdir]# mv demo newdemo
[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:11 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root   19 Apr 14 03:49 edit.txt
-rw-r--r--. 1 root root    0 Apr 14 02:04 example.txt
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 folder1
-rw-r--r--. 1 root root    0 Apr 14 01:57 newdemo

L'esempio seguente usa mv per riscrivere la demo file con il finale file. Questa azione sostituisce la demo .

[root@server-01 testdir]# cat demo
This is a Newer Version of Demo.
[root@server-01 testdir]# cat final
Demo Replaced by Final Version.
[root@server-01 testdir]# mv final demo
mv: overwrite ‘demo’? y
[root@server-01 testdir]# cat demo
Demo Replaced by Final Version.
[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:26 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root   32 Apr 14 04:24 demo
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1

ATTENZIONE: Utilizzando il mv il comando per sovrascrivere un file esistente è permanente. Non è possibile recuperare il file precedente.

Elimina un file

Usa il rm comando per rimuovere un file.

Sintassi *:nomefile rm

L'esempio seguente usa rm comando per rimuovere il file esistente, demo :

[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:26 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
-rw-r--r--. 1 root root   32 Apr 14 04:24 demo
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1
[root@server-01 testdir]# rm demo
rm: remove regular file ‘demo’? y
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:31 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1

ATTENZIONE: Usando il rm il comando per rimuovere un file esistente è permanente. Non è possibile recuperare il file precedente.

Elimina una directory vuota

Usa la rmdir comando per rimuovere una directory vuota.

Sintassi :rmdir nome directory

L'esempio seguente usa rmdir comando per rimuovere una directory vuota, cartella vuota :

[root@server-01 testdir]# ls -lah
total 16K
drwxr-xr-x. 4 root root 4.0K Apr 14 04:35 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 04:35 emptyfolder1
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1
[root@server-01 testdir]# rmdir emptyfolder1
[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:36 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1

Elimina una directory non vuota

La directory rm comando non può rimuovere una directory con contenuto al suo interno, come mostrato nell'esempio seguente:

 [root@server-01 testdir]# rmdir newfolder1
 rmdir: failed to remove ‘newfolder1’: Directory not empty

Tuttavia, puoi usare rm con l'opzione -r per rimuovere una directory con contenuto.

Sintassi: :rm -r nome directory

L'esempio seguente usa rm -r per rimuovere la directory non vuota, newfolder1 e il relativo file, sample.txt :

[root@server-01 testdir]# ls -lah
total 12K
drwxr-xr-x. 3 root root 4.0K Apr 14 04:36 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..
drwxr-xr-x. 2 root root 4.0K Apr 14 03:58 newfolder1
[root@server-01 testdir]# rm -r newfolder1
rm: descend into directory ‘newfolder1’? y
rm: remove regular empty file ‘newfolder1/sample.txt’? y
rm: remove directory ‘newfolder1’? y
[root@server-01 testdir]# ls -lah
total 8.0K
drwxr-xr-x. 2 root root 4.0K Apr 14 04:43 .
dr-xr-x---. 8 root root 4.0K Apr 14 01:47 ..

ATTENZIONE: Usando il rm il comando per rimuovere una directory esistente è permanente. Non è possibile recuperare la directory e i contenuti precedenti.


Linux
  1. Copia i file nel terminale Linux

  2. Come eliminare file e directory in Linux dalla riga di comando

  3. Come aggiungere una barra di avanzamento per copiare e spostare i comandi in Linux?

  4. Esempi di comandi Linux Objcopy per copiare e tradurre file oggetto

  5. Regole di controllo per il monitoraggio Copia, sposta, elimina e termina i comandi in Linux

Comando Ls in Linux (Elenca file e directory)

Come copiare file e directory in Linux

Come spostare file e directory in Linux (comando mv)

Comando Cp in Linux (copia file)

Elimina, copia, sposta e rinomina i file utilizzando la riga di comando in Linux

Come copiare file e directory nel terminale Linux