Ricerca nel sito web

10 esempi di comandi Wget (Linux File Downloader) in Linux


In questo articolo esamineremo l'utilità wget che recupera file dal World Wide Web (WWW) utilizzando protocolli ampiamente utilizzati come HTTP , HTTPS, FTP e FTPS.

Wget è un'utilità da riga di comando gratuita e un downloader di file di rete, dotato di molte funzionalità che semplificano il download dei file, tra cui:

  • Scarica file di grandi dimensioni o esegui il mirroring di siti Web o FTP completi.
  • Scarica più file contemporaneamente.
  • Imposta la larghezza di banda e il limite di velocità per i download.
  • Scaricare file tramite proxy.
  • Può riprendere i download interrotti.
  • Mirroring ricorsivo delle directory.
  • Funziona sulla maggior parte dei sistemi operativi simili a UNIX e su Windows.
  • Operazione incustodita/in background.
  • Supporto per connessioni HTTP persistenti.
  • Supporto per SSL/TLS per download crittografati utilizzando la libreria OpenSSL o GnuTLS.
  • Supporto per download IPv4 e IPv6.

Sintassi del comando Wget

La sintassi di base di Wget è:

wget [option] [URL]

Innanzitutto, controlla se l'utilità wget è già installata o meno nella tua macchina Linux, utilizzando il seguente comando.

rpm -q wget         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
dpkg -l | grep wget [On Debian, Ubuntu and Mint]

Installa Wget su Linux

Se Wget non è installato, puoi installarlo utilizzando il gestore pacchetti predefinito del tuo sistema Linux come mostrato.

sudo apt install wget -y      [On Debian, Ubuntu and Mint]
sudo yum install wget -y      [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
sudo emerge -a net-misc/wget  [On Gentoo Linux]
sudo pacman -Sy wget           [On Arch Linux]
sudo zypper install wget      [On OpenSUSE]    

L'opzione -y usata qui serve per impedire richieste di conferma prima di installare qualsiasi pacchetto. Per ulteriori esempi e opzioni di comandi YUM e APT leggi i nostri articoli su:

  • 20 comandi Linux YUM per la gestione dei pacchetti
  • 15 esempi di comandi APT in Ubuntu/Debian e Mint
  • 45 esempi di comandi Zypper per gestire OpenSUSE Linux

1. Scarica un file con Wget

Il comando scaricherà un singolo file e lo memorizzerà in una directory corrente. Mostra anche avanzamento del download, dimensione, data e ora durante il download.

wget http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz

--2021-12-10 04:15:16--  http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3565643 (3.4M) [application/x-gzip]
Saving to: ‘wget2-2.0.0.tar.gz’

wget2-2.0.0.tar.gz      100%[==========>]   3.40M  2.31MB/s    in 1.5s    

2021-12-10 04:15:18 (2.31 MB/s) - ‘wget2-2.0.0.tar.gz’ saved [3565643/3565643]

2. Scarica il file con un nome diverso

Utilizzando l'opzione -O (maiuscolo), scarica file con nomi di file diversi. Qui abbiamo dato il nome del file wget.zip come mostrato di seguito.

wget -O wget.zip http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz

--2021-12-10 04:20:19--  http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 446966 (436K) [application/x-gzip]
Saving to: ‘wget.zip’

wget.zip      100%[===================>] 436.49K   510KB/s    in 0.9s    

2021-12-10 04:20:21 (510 KB/s) - ‘wget.zip’ saved [446966/446966]

3. Scarica più file con protocollo HTTP e FTP

Qui vediamo come scaricare più file utilizzando il protocollo HTTP e FTP con il comando wget contemporaneamente.

wget http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz ftp://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz.sig

--2021-12-10 06:45:17--  http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3565643 (3.4M) [application/x-gzip]
Saving to: ‘wget2-2.0.0.tar.gz’

wget2-2.0.0.tar.gz      100%[==========>]   4.40M  4.31MB/s    in 1.1s    

2021-12-10 06:46:10 (2.31 MB/s) - ‘wget2-2.0.0.tar.gz’ saved [3565643/3565643]

4. Scarica più file da un file

Per scaricare più file contemporaneamente, utilizza l'opzione -i con la posizione del file che contiene l'elenco degli URL da scaricare. Ogni URL deve essere aggiunto su una riga separata come mostrato.

Ad esempio, il seguente file "download-linux.txt" contiene l'elenco degli URL da scaricare.

cat download-linux.txt 

https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
https://download.rockylinux.org/pub/rocky/8/isos/x86_64/Rocky-8.5-x86_64-dvd1.iso
https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-11.2.0-amd64-DVD-1.iso
wget -i download-linux.txt

--2021-12-10 04:52:40--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.248, 91.189.88.247, 91.189.91.124, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.248|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3071934464 (2.9G) [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64   4%[=>      ] 137.71M  11.2MB/s    eta 3m 30s
...

Se il tuo elenco di URL ha uno schema di numerazione particolare, puoi aggiungere parentesi graffe per recuperare tutti gli URL che corrispondono allo schema. Ad esempio, se desideri scaricare una serie di kernel Linux a partire dalla versione 5.1.1 fino alla 5.1.15, puoi procedere come segue.

wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.{1..15}.tar.gz

--2021-12-10 05:46:59--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz
Resolving mirrors.edge.kernel.org (mirrors.edge.kernel.org)... 147.75.95.133, 2604:1380:3000:1500::1
Connecting to mirrors.edge.kernel.org (mirrors.edge.kernel.org)|147.75.95.133|:443... connected.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not trusted.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not yet activated.
The certificate has not yet been activated
HTTP request sent, awaiting response... 200 OK
Length: 164113671 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.1.tar.gz’

linux-5.1.1.tar.gz      100%[===========>] 156.51M  2.59MB/s    in 61s     

2021-12-10 05:48:01 (2.57 MB/s) - ‘linux-5.1.1.tar.gz’ saved [164113671/164113671]

--2021-12-10 05:48:01--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.2.tar.gz
Reusing existing connection to mirrors.edge.kernel.org:443.
HTTP request sent, awaiting response... 200 OK
Length: 164110470 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.2.tar.gz’

linux-5.1.2.tar.gz     19%[===========]  30.57M  2.58MB/s    eta 50s

5. Wget Riprendi download incompleto

In caso di download di file di grandi dimensioni, a volte può capitare di interrompere il download, in tal caso possiamo riprendere il download dello stesso file da dove era stato interrotto con l'opzione -c.

Ma quando inizi a scaricare file senza specificare -c un'opzione wget aggiungerà l'estensione .1 alla fine del file, considerato come nuovo scaricamento. Pertanto, è buona norma aggiungere l'opzione -c quando scarichi file di grandi dimensioni.

# wget -c https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

--2021-12-10 05:27:59--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.247, 91.189.91.123, 91.189.91.124, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.247|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 3071934464 (2.9G), 2922987520 (2.7G) remaining [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64.iso        5%[++++++> ]   167.93M  11.1MB/s               
^C
[root@tecmint ~]# wget -c https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
--2021-12-10 05:28:03--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.248, 91.189.91.124, 91.189.91.123, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.248|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 3071934464 (2.9G), 2894266368 (2.7G) remaining [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64.iso        10%[+++++++=====> ] 296.32M  17.2MB/s    eta 2m 49s ^

6. Wget Mirror intero sito web

Per scaricare, eseguire il mirroring o copiare un intero sito Web per la visualizzazione offline, è possibile utilizzare il comando seguente che creerà una copia locale del sito Web insieme a tutte le risorse (JavaScript, CSS, Immagini).

wget --recursive --page-requisites --adjust-extension --span-hosts --convert-links --restrict-file-names=windows --domains yoursite.com --no-parent yoursite.com

Spiegazione del comando precedente.

wget \
     --recursive \ # Download the whole site.
     --page-requisites \ # Get all assets/elements (CSS/JS/images).
     --adjust-extension \ # Save files with .html on the end.
     --span-hosts \ # Include necessary assets from offsite as well.
     --convert-links \ # Update links to still work in the static version.
     --restrict-file-names=windows \ # Modify filenames to work in Windows as well.
     --domains yoursite.com \ # Do not follow links outside this domain.
     --no-parent \ # Don't follow links outside the directory you pass in.
         yoursite.com/whatever/path # The URL to download

7. Ottieni il download dei file in background

Con l'opzione -b puoi inviare un download in background immediatamente dopo l'avvio del download e i log vengono scritti nel file wget.log.

wget -b wget.log https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

Continuing in background, pid 8999.
Output will be written to ‘wget.log’.

8. Wget Imposta i limiti di velocità di download dei file

Con l'opzione --limit-rate=100k, il limite di velocità di download è limitato a 100k e i log verranno creati in wget.log come mostrato di seguito.

wget -c --limit-rate=100k -b wget.log https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

Continuing in background, pid 9108.
Output will be written to ‘wget-log’.

Visualizza il file wget.log e controlla la velocità di download di wget.

tail -f wget-log 

 5600K .......... .......... .......... .......... ..........  0%  104K 8h19m
 5650K .......... .......... .......... .......... ..........  0%  103K 8h19m
 5700K .......... .......... .......... .......... ..........  0%  105K 8h19m
 5750K .......... .......... .......... .......... ..........  0%  104K 8h18m
 5800K .......... .......... .......... .......... ..........  0%  104K 8h18m
 5850K .......... .......... .......... .......... ..........  0%  105K 8h18m
 5900K .......... .......... .......... .......... ..........  0%  103K 8h18m
 5950K .......... .......... .......... .......... ..........  0%  105K 8h18m
 6000K .......... .......... .......... .......... ..........  0% 69.0K 8h20m
 6050K .......... .......... .......... .......... ..........  0%  106K 8h19m
 6100K .......... .......... .......... .......... ..........  0% 98.5K 8h20m
 6150K .......... .......... .......... .......... ..........  0%  110K 8h19m
 6200K .......... .......... .......... .......... ..........  0%  104K 8h19m
 6250K .......... .......... .......... .......... ..........  0%  104K 8h19m
...

9. Scarica file protetti da password tramite FTP e HTTP

Per scaricare un file da un server FTP protetto da password, puoi utilizzare le opzioni --ftp-user=username e --ftp-password=password come mostrato.

wget --ftp-user=narad --ftp-password=password ftp://ftp.example.com/filename.tar.gz

Per scaricare un file da un server HTTP protetto da password, puoi utilizzare le opzioni --http-user=username e --http-password=password come mostrato.

wget --http-user=narad --http-password=password http://http.example.com/filename.tar.gz

10. Wget Ignora il controllo del certificato SSL

Per ignorare il controllo del certificato SSL durante il download di file tramite HTTPS, puoi utilizzare l'opzione --no-check-certificate:

wget --no-check-certificate https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz

--2021-12-10 06:21:21--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz
Resolving mirrors.edge.kernel.org (mirrors.edge.kernel.org)... 147.75.95.133, 2604:1380:3000:1500::1
Connecting to mirrors.edge.kernel.org (mirrors.edge.kernel.org)|147.75.95.133|:443... connected.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not trusted.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not yet activated.
The certificate has not yet been activated
HTTP request sent, awaiting response... 200 OK
Length: 164113671 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.1.tar.gz’
...

11. Wget Versione e Guida

Con le opzioni --version e --help puoi visualizzare la version e l'help secondo necessità.

wget --version
wget --help

In questo articolo abbiamo trattato i comandi wget di Linux con opzioni per le attività amministrative quotidiane. man wget se vuoi saperne di più? Si prega di condividere attraverso la nostra casella dei commenti o, se ci siamo persi qualcosa, fatecelo sapere.