GNU/Linux >> Linux Esercitazione >  >> Linux

Linux per principianti:altri 10 comandi per manipolare i file

Nel caso in cui ti fossi perso il primo segmento di questa serie in due parti, abbiamo esaminato 10 comandi per iniziare al terminale. Ora esploreremo 10 comandi aggiuntivi che puoi utilizzare per continuare il tuo percorso dalla riga di comando. In particolare, voglio esaminare i comandi utilizzati per manipolare i file. Copieremo, sposteremo e rinomineremo i file, oltre ad alcuni comandi per leggere i file in modi diversi.

Comandi da sapere

1. cp file1 file2

La copia comando viene utilizzato per copiare il contenuto di un file in un altro. Puoi anche usare questo comando per copiare un file da una directory in un'altra.

[tcarrigan@server community_content]$ ls -l
total 36
-rw-rw-r--. 1 tcarrigan tcarrigan    5 Feb  6  2020 article
-rw-rw-r--. 1 root      tcarrigan    5 Feb  6  2020 article2
-rw-rw-r--. 1 root      tcarrigan    5 Feb  6  2020 article3
-rw-rw-r--. 1 tcarrigan tcarrigan    5 Feb  6  2020 article4
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Apr  7  2020 podman_pulling
-rw-rw-r--. 1 tcarrigan tcarrigan 8824 Apr  7  2020 real_sysadmins
[tcarrigan@server community_content]$ cp podman_pulling article
[tcarrigan@server community_content]$ ls -l
total 40
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:11 article
-rw-rw-r--. 1 root      tcarrigan    5 Feb  6  2020 article2
-rw-rw-r--. 1 root      tcarrigan    5 Feb  6  2020 article3
-rw-rw-r--. 1 tcarrigan tcarrigan    5 Feb  6  2020 article4
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Apr  7  2020 podman_pulling
-rw-rw-r--. 1 tcarrigan tcarrigan 8824 Apr  7  2020 real_sysadmins

Come vedi, ho copiato il podman_pulling contenuto dell'articolo nell'articolo file. Ora, l'altra cosa che puoi fare con questo comando è copiare un file esistente in un'altra directory. Ad esempio, se volessi copiare il podman_pulling articolo nella mia home directory, farei qualcosa del genere:

[tcarrigan@server community_content]$ cp podman_pulling /home/tcarrigan/
** Navigate to /home/tcarrigan **
[tcarrigan@server ~]$ ls -l
total 8
drwxrwxr-x. 4 tcarrigan tcarrigan   50 Feb  6  2020 article_submissions
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Desktop
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Documents
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Downloads
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Music
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Pictures
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 podman_pulling
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Public
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Templates
drwxr-xr-x. 2 tcarrigan tcarrigan    6 Jan 27  2020 Videos

Puoi vedere quel podman_pulling è ora disponibile nel mio /home/tcarrigan directory.

[ Potrebbe piacerti anche: 10 pratici comandi di sistema:un riferimento ]

2. mv file1 file2

La mossa comando consente a un utente di spostare o rinominare un file. Per spostare un file, dovresti usare quanto segue:

[tcarrigan@server ~]$ mv podman_pulling article_submissions/my_articles/
[tcarrigan@server ~]$ ls -l article_submissions/my_articles/
total 20
-rw-rw-r--. 1 tcarrigan tcarrigan 4442 Apr  7  2020 Creating_physical_volumes
-rw-rw-r--. 1 tcarrigan tcarrigan 2744 Apr  7  2020 Creating_volume_groups
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 podman_pulling

Ho spostato il podman_pulling articolo dalla mia home directory su /article_submissions/my_articles . Ora, supponiamo di dover rinominare l'articolo da "podman_pulling " a "podman senza radici ." Come lo faremmo?

[tcarrigan@server my_articles]$ mv podman_pulling rootless_podman
[tcarrigan@server my_articles]$ ls -l
total 20
-rw-rw-r--. 1 tcarrigan tcarrigan 4442 Apr  7  2020 Creating_physical_volumes
-rw-rw-r--. 1 tcarrigan tcarrigan 2744 Apr  7  2020 Creating_volume_groups
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 rootless_podman

3. ln -s collegamento al file

Il link comando in questo contesto ci consentirà di creare un collegamento software o un collegamento simbolico a un altro file o directory. Per ulteriori informazioni su questo argomento, consulta il mio articolo precedente, Collegamento di Linux . Per ora, dimostrerò solo come utilizzare il comando soft link.

[tcarrigan@server ~]$ ln -s article_submissions/community_content/podman_pulling podman_article
[tcarrigan@server ~]$ ls -l
total 0
drwxrwxr-x. 4 tcarrigan tcarrigan 50 Feb  6  2020 article_submissions
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Desktop
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Documents
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Downloads
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Music
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Pictures
lrwxrwxrwx. 1 tcarrigan tcarrigan 52 Oct 27 13:51 podman_article -> article_submissions/community_content/podman_pulling
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Public
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Templates
drwxr-xr-x. 2 tcarrigan tcarrigan  6 Jan 27  2020 Videos

Questo crea un collegamento nella mia home directory a /home/tcarrigan/article_submissions/community_content/podman_pulling file, il che significa che puoi leggere podman_article , e aprirà il file all'interno del community_content directory.

4. tocca file

Usa il tocco comando per creare o aggiornare un file (qui sto creando nuovo_file nei i miei_articoli directory).

[tcarrigan@server my_articles]$ touch new_file
[tcarrigan@server my_articles]$ ls -l
total 20
-rw-rw-r--. 1 tcarrigan tcarrigan 4442 Apr  7  2020 Creating_physical_volumes
-rw-rw-r--. 1 tcarrigan tcarrigan 2744 Apr  7  2020 Creating_volume_groups
-rw-rw-r--. 1 tcarrigan tcarrigan    0 Oct 28 16:47 new_file
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 rootless_podman

Nota che il timestamp del file creato è 16:47. Ora userò il touch comando per aggiornare il timestamp all'ora corrente (19:35).

[tcarrigan@server my_articles]$ touch new_file 
[tcarrigan@server my_articles]$ ls -l
total 20
-rw-rw-r--. 1 tcarrigan tcarrigan 4442 Apr  7  2020 Creating_physical_volumes
-rw-rw-r--. 1 tcarrigan tcarrigan 2744 Apr  7  2020 Creating_volume_groups
-rw-rw-r--. 1 tcarrigan tcarrigan    0 Oct 28 19:35 new_file
-rw-rw-r--. 1 tcarrigan tcarrigan 6404 Oct 26 19:17 rootless_podman

5. cat> file

Questo comando viene utilizzato per inserire l'output standard in un file. Inserirò "Hello World" nel file test_file .

[tcarrigan@server ~]$ cat > test_file
Hello World 

Usa Ctrl+D* per terminare la modifica del file. Per visualizzare il tuo lavoro, puoi utilizzare lo standard cat filename sintassi.

[tcarrigan@server ~]$ cat test_file 
Hello World

6. altro file

Il di più il comando consentirà all'utente di visualizzare il contenuto di un file una schermata alla volta. Qui esamineremo i /article_submissions/my_articles/creating_physical_volumes file.

#  How to create a physical volume in Linux using LVM
by Tyler Carrigan

Physical volumes `PV` are the base 'block' that you need in order to manipulate a disk using Logical Volume Manager `LVM`. Now, let's not rush ahead. What exactly is a physical volume? What in the world i
s LVM? In short, LVM is a type of storage virtualization that allows operators far more flexibility in storage management than standard partitioning. A physical volume is any physical storage device, such
 as an Hard Disk Drive `HDD`, Solid State Drive `SSD`, or partition, that has been initialized as a physical volume with LVM. Without properly initialized physical volumes, you cannot create Volume Groups
 or logical volumes. 

So lets get started! First, there are a few considerations.

Dont try to pinpoint the exact amount of space you need down to the nearest byte. The reason for this is that LVM places labels on the physical volumes `UUID` as well as metadata storage. While this doesn
t take up very much space, understand that if you initialize a 1Gb PV, you do **not** have 1Gb of *usable* space.
** Output Omitted **

Puoi andare riga per riga con Invio tasto o pagina per pagina con Spazio . Un'altra opzione utile è il numero opzione che consente di specificare il numero di righe visualizzate per pagina.

Per visualizzare 10 righe alla volta, puoi utilizzare more -10 filename . Puoi usare il + sign per specificare da quale riga partire:more +10 filename .

7. meno file

Il meno Il comando consente all'utente le stesse funzionalità di more . Tuttavia, è più veloce perché non carica l'intero file ma consente invece all'utente di analizzare il file utilizzando i tasti freccia. Ciò è particolarmente evidente nei file di registro di grandi dimensioni.

[tcarrigan@server my_articles]$ sudo less /var/log/messages
Oct 26 19:50:47 server dbus-daemon[939]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service' requested by ':1.15' (uid=0 pid=1155 comm="/usr/sbin/NetworkManager --no-daemon " label="system_u:system_r:NetworkManager_t:s0")
Oct 26 19:50:47 server systemd[1]: Starting Network Manager Script Dispatcher Service...
Oct 26 19:50:47 server dbus-daemon[939]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Oct 26 19:50:47 server systemd[1]: Started Network Manager Script Dispatcher Service.
** Output Omitted **

8. file principale

La testa Il comando consente a un utente di generare le prime 10 righe di qualsiasi file. Userò l'esempio di creating_physical_volumes .

[tcarrigan@server my_articles]$ head Creating_physical_volumes
#  How to create a physical volume in Linux using LVM
by Tyler Carrigan

Physical volumes `PV` are the base 'block' that you need in order to manipulate a disk using Logical Volume Manager `LVM`. Now, let's not rush ahead. What exactly is a physical volume? What in the world is LVM? In short, LVM is a type of storage virtualization that allows operators far more flexibility in storage management than standard partitioning. A physical volume is any physical storage device, such as an Hard Disk Drive `HDD`, Solid State Drive `SSD`, or partition, that has been initialized as a physical volume with LVM. Without properly initialized physical volumes, you cannot create Volume Groups or logical volumes. 

So lets get started! First, there are a few considerations.

Dont try to pinpoint the exact amount of space you need down to the nearest byte. The reason for this is that LVM places labels on the physical volumes `UUID` as well as metadata storage. While this doesnt take up very much space, understand that if you initialize a 1Gb PV, you do **not** have 1Gb of *usable* space. 

Also, although LVM allows you to create physical volumes using multiple partitions, it is recommended that you use a single partition for a PV. This is for a couple of reasons. 
[tcarrigan@server my_articles]$

9. file di coda

La coda Il comando consente all'utente di visualizzare le ultime 10 righe di qualsiasi file. Qui esamineremo /var/log/messages .

[tcarrigan@server my_articles]$ sudo tail /var/log/messages
[sudo] password for tcarrigan:  
Oct 28 20:17:47 server org.gnome.Shell.desktop[2053]: Window manager warning: last_user_time (9080279) is greater than comparison timestamp (9080269).  This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW.  Trying to work around...
Oct 28 20:17:47 server org.gnome.Shell.desktop[2053]: Window manager warning: W1 appears to be one of the offending windows with a timestamp of 9080279.  Working around...
Oct 28 20:17:48 server dbus-daemon[948]: [system] Activating via systemd: service name='net.reactivated.Fprint' unit='fprintd.service' requested by ':1.500' (uid=0 pid=5259 comm="sudo tail /var/log/messages " label="unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023")
Oct 28 20:17:48 server systemd[1]: Starting Fingerprint Authentication Daemon...
Oct 28 20:17:49 server dbus-daemon[948]: [system] Successfully activated service 'net.reactivated.Fprint'
Oct 28 20:17:49 server systemd[1]: Started Fingerprint Authentication Daemon.
Oct 28 20:17:53 server org.gnome.Shell.desktop[2053]: Window manager warning: last_user_time (9086680) is greater than comparison timestamp (9086677).  This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW.  Trying to work around...
Oct 28 20:17:53 server org.gnome.Shell.desktop[2053]: Window manager warning: W1 appears to be one of the offending windows with a timestamp of 9086680.  Working around...
Oct 28 20:18:00 server org.gnome.Shell.desktop[2053]: Window manager warning: last_user_time (9093426) is greater than comparison timestamp (9093424).  This most likely represents a buggy client sending inaccurate timestamps in messages such as _NET_ACTIVE_WINDOW.  Trying to work around...
Oct 28 20:18:00 server org.gnome.Shell.desktop[2053]: Window manager warning: W1 appears to be one of the offending windows with a timestamp of 9093426.  Working around...
[tcarrigan@server my_articles]$ 

10. file tail -f

Il -f variante della coda command è una versione completamente diversa del comando originale. Questo flag consente all'utente di vedere il file mentre viene scritto. Questo è incredibilmente utile per la risoluzione dei problemi di avvio/arresto per applicazioni e sistemi allo stesso modo.

Ora lo sai

Se hai seguito questo tutorial, dovresti avere altri 10 comandi nel tuo arsenale di terminali. Dallo spostamento, copia, creazione e aggiornamento a vari modi per leggere il contenuto dei file, dovresti sentirti a tuo agio spostandoti nel filesystem e manipolando i file e le directory che incontri. Se queste cose sembrano complicate, continua a esercitarti e sarai un utente più sicuro in pochissimo tempo. Per ulteriori suggerimenti, trucchi e strumenti, ricontrolla con noi e Abilita Sysadmin.

[ Scarica ora:una guida per l'amministratore di sistema allo scripting Bash. ] 


Linux
  1. Cheat sheet per i comandi Linux comuni

  2. 20 comandi Linux di base per principianti spiegati con esempi

  3. Linux per principianti:10 comandi per iniziare dal terminale

  4. Comandi di ricerca di Linux

  5. I migliori comandi di base di Linux per principianti

Linux lsof Command Tutorial per principianti (15 esempi)

8 comandi fondamentali di gestione dei file Linux per i nuovi utenti

Comandi di base di Linux per principianti

I 40 comandi utili del server Linux per principianti e professionisti

40 utili comandi git per amministratori e sviluppatori Linux

17 migliori comandi di rete e risoluzione dei problemi di Linux per principianti