ウェブサイト検索

Linux での 10 Wget (Linux ファイル ダウンローダー) コマンドの例


この記事では、HTTP などの広く使用されているプロトコルを使用してワールド ワイド ウェブ (WWW) からファイルを取得する wget ユーティリティをレビューします。 HTTPSFTPFTPS

Wget は、無料のコマンドライン ユーティリティおよびネットワーク ファイル ダウンローダーであり、ファイルのダウンロードを簡単にする次のような多くの機能が備わっています。

  • 大きなファイルをダウンロードしたり、完全な Web サイトまたは FTP サイトをミラーリングしたりできます。
  • 複数のファイルを一度にダウンロードします。
  • ダウンロードの帯域幅と速度制限を設定します。
  • プロキシ経由でファイルをダウンロードします。
  • 中止されたダウンロードを再開できます。
  • ディレクトリを再帰的にミラーリングします。
  • Windows だけでなく、ほとんどの UNIX 系オペレーティング システムでも実行できます。
  • 無人/バックグラウンド操作。
  • 永続的な HTTP 接続のサポート。
  • OpenSSL または GnuTLS ライブラリを使用した暗号化されたダウンロードのための SSL/TLS のサポート。
  • IPv4 および IPv6 ダウンロードのサポート。

Wget コマンドの構文

Wget の基本的な構文は次のとおりです。

wget [option] [URL]

まず、次のコマンドを使用して、wget ユーティリティが Linux ボックスにすでにインストールされているかどうかを確認します。

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

Linux に Wget をインストールする

Wget がインストールされていない場合は、図に示すように、Linux システムのデフォルトのパッケージ マネージャーを使用してインストールできます。

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]    

ここで使用されている -y オプションは、パッケージをインストールする前に確認プロンプトが表示されないようにするためのものです。 YUM および APT コマンドの例とオプションの詳細については、次の記事を参照してください。

  • パッケージ管理のための 20 の Linux YUM コマンド
  • Ubuntu/Debian & Mint での 15 の APT コマンド例
  • OpenSUSE Linux を管理するための 45 の Zypper コマンド例

1.Wgetでファイルをダウンロードする

このコマンドは 1 つのファイルをダウンロードし、現在のディレクトリに保存します。ダウンロード中のダウンロードの進行状況サイズ日付時間も表示されます。

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. 別の名前の Wget ダウンロード ファイル

-O (大文字) オプションを使用すると、異なるファイル名を持つファイルがダウンロードされます。ここでは、 以下に示すようにwget.zip ファイル名を付けました。

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. Wget HTTP および FTP プロトコルを使用して複数のファイルをダウンロードする

ここでは、HTTP プロトコルと FTP プロトコルを使用し、wget コマンドを同時に使用して複数のファイルをダウンロードする方法を示します。

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. Wget ファイルから複数のファイルをダウンロードする

複数のファイルを一度にダウンロードするには、ダウンロードする URL のリストを含むファイルの場所を指定して -i オプションを使用します。図に示すように、各 URL を別の行に追加する必要があります。

たとえば、次のファイル「download-linux.txt」ファイルには、ダウンロードする URL のリストが含まれています。

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
...

URL リストに特定の番号付けパターンがある場合は、中かっこを追加して、パターンに一致するすべての URL を取得できます。たとえば、バージョン 5.1.1 から 5.1.15 までの一連の Linux カーネルをダウンロードする場合は、次の手順を実行できます。

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 の未完了のダウンロードを再開する

大きなファイルのダウンロードの場合、ダウンロードが停止することがあります。その場合は、-c オプションを使用して、中断された同じファイルのダウンロードを再開できます。

ただし、-c を指定せずにファイルのダウンロードを開始すると、オプション wget によってファイルの末尾に .1 拡張子が追加され、新しいものとみなされます。ダウンロード。したがって、大きなファイルをダウンロードする場合は、-c スイッチを追加することをお勧めします。

# 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 ミラー Web サイト全体

オフラインで表示できるように Web サイト全体をダウンロード、ミラーリング、またはコピーするには、次のコマンドを使用して、すべてのアセット (JavaScript、CSS、画像) とともに Web サイトのローカル コピーを作成します。

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

上記コマンドの説明。

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. Wget バックグラウンドでファイルをダウンロードする

-b オプションを使用すると、ダウンロードの開始直後にバックグラウンドでダウンロードを送信でき、ログが 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 Set ファイルのダウンロード速度制限

オプション --limit-rate=100k を使用すると、ダウンロード速度制限が 100k に制限され、以下に示すようにログが wget.log の下に作成されます。

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’.

wget.log ファイルを表示して、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. Wget は FTP および HTTP 経由でパスワードで保護されたファイルをダウンロードします

パスワードで保護された FTP サーバーからファイルをダウンロードするには、次のようにオプション --ftp-user=username および --ftp-password=password を使用できます。

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

パスワードで保護された HTTP サーバーからファイルをダウンロードするには、次のようにオプション --http-user=username および --http-password=password を使用できます。

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

10. Wget で SSL 証明書チェックを無視する

HTTPS 経由でファイルをダウンロードするときに SSL 証明書チェックを無視するには、 --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 のバージョンとヘルプ

オプション --version および --help を使用すると、必要に応じてバージョンヘルプを表示できます。

wget --version
wget --help

この記事では、日常の管理タスクのためのオプションを備えた Linux のwget コマンドについて説明しました。もっと詳しく知りたければマンウィゲットしてみませんか?コメントボックスを通じて共有していただくか、何か見逃している場合はお知らせください。