wget コマンド:オプションと -O、-P の保存先、-c でのダウンロード再開

テスト環境RHEL系AlmaLinux 9.8rootアカウント
ネットワークコマンドwget場所/usr/bin/wget

wget コマンドは、HTTP、HTTPS、FTP のアドレスにあるファイルを Linux のターミナルでダウンロードするコマンドです。AlmaLinux 9 サーバーで Apache Tomcat の圧縮ファイルをダウンロード中に 21% で中断し、-c オプションで再実行すると、受信済みの前半部分は飛ばして残りの 9.8M だけを続きからダウンロードし、受け取ったファイルは公式の SHA-512 チェックサムと一致しました。

以下の出力の保存パスはサンプル値です。ファイルはサンプルドメインの example.com と、Apache、GNU の公式配布サーバー(ftp.gnu.org)、kernel.org のトップページからだけダウンロードし、レスポンスヘッダーからキャッシュサーバー名を含む行は除きました。

1️⃣ wget コマンドの基本的な使い方と出力の読み方

コマンドの基本形は wget、オプション、アドレスの順です。オプションなしでアドレスだけを指定すると、現在のフォルダーにアドレス末尾のファイル名で保存し、アドレスがスラッシュで終わる場合は index.html という名前を使います。

cd /root/work
wget https://example.com/
--2026-10-29 01:07:46--  https://example.com/
Resolving example.com (example.com)... 2606:4700:10::ac42:93f3, 2606:4700:10::6814:179a, 172.66.147.243, ...
Connecting to example.com (example.com)|2606:4700:10::ac42:93f3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: 'index.html'

index.html              [ <=>                ]     577  --.-KB/s    in 0s

2026-10-29 01:07:46 (19.1 MB/s) - 'index.html' saved [577]

出力は名前解決、接続、レスポンス、サイズ、保存先、プログレスバー、結果の順に表示されます。レスポンス行が 200 OK ならサーバーがファイルを送っており、Length 行にはファイルサイズと種類が、最後の saved 行には保存したバイト数が表示されます。Resolving 行で IPv6 アドレスが先にあると、wget は IPv6 で先に接続します。

ダウンロードが終わると、wget は終了コードで結果を残します。-q でメッセージを消して終了コードだけを出力すると、成功したときは 0 が表示されます。

wget -q -O /root/work/dl/example.html https://example.com/
echo $?
0

存在しないアドレスをリクエストすると、404 レスポンスの後に [ERROR 404: Not Found.] の行が表示され、終了コードは 8 です。

wget -O /root/work/dl/nosuch.html https://example.com/nosuch.html
--2026-10-29 01:05:03--  https://example.com/nosuch.html
Resolving example.com (example.com)... 2606:4700:10::ac42:93f3, 2606:4700:10::6814:179a, 172.66.147.243, ...
Connecting to example.com (example.com)|2606:4700:10::ac42:93f3|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2026-10-29 01:05:03 ERROR 404: Not Found.
echo $?
8

スクリプトでは、ダウンロードが成功したかどうかをこのコードで確認します。コードは九種類で、この記事では 0、4、8 が出ました。

終了コード意味(マニュアル基準)この記事で出たとき
0問題なし-q でダウンロードに成功、--spider で確認した存在するアドレス
1一般的なエラー—
2オプションを解析できない—
3ファイルの読み書きエラー—
4ネットワークエラー応答のない 192.0.2.1 に -T 3 -t 2
5SSL 証明書の検証に失敗—
6ユーザー名とパスワードの認証に失敗—
7プロトコルエラー—
8サーバーがエラーレスポンス(404 など)を返した存在しないアドレスの 404、--spider の broken link

2️⃣ wget -O、-P の保存先と -o のログファイル

デフォルトの動作どおり現在のフォルダーに保存すると、スクリプトや cron で実行したときにファイルがどこにできるか分かりにくくなります。そのため保存先は、-P でフォルダーを、-O でファイルパスを直接書きます。

wget -P /root/work/dl/ https://example.com/
--2026-10-29 01:19:31--  https://example.com/
Resolving example.com (example.com)... 2606:4700:10::6814:179a, 2606:4700:10::ac42:93f3, 172.66.147.243, ...
Connecting to example.com (example.com)|2606:4700:10::6814:179a|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: '/root/work/dl/index.html'

index.html              [ <=>                ]     577  --.-KB/s    in 0s

2026-10-29 01:19:31 (18.7 MB/s) - '/root/work/dl/index.html' saved [577]

-P はフォルダーだけを決め、ファイル名はアドレスから取ります。同じコマンドをもう一度実行すると、既存の index.html はそのまま残し、末尾に .1 を付けた新しいファイルとして保存します。

wget -P /root/work/dl/ https://example.com/
--2026-10-29 01:19:31--  https://example.com/
Resolving example.com (example.com)... 2606:4700:10::ac42:93f3, 2606:4700:10::6814:179a, 104.20.23.154, ...
Connecting to example.com (example.com)|2606:4700:10::ac42:93f3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: '/root/work/dl/index.html.1'

index.html.1            [ <=>                ]     577  --.-KB/s    in 0s

2026-10-29 01:19:32 (23.2 MB/s) - '/root/work/dl/index.html.1' saved [577]
wget -O /root/work/dl/example.html https://example.com/
--2026-10-29 01:04:06--  https://example.com/
Resolving example.com (example.com)... 2606:4700:10::ac42:93f3, 2606:4700:10::6814:179a, 172.66.147.243, ...
Connecting to example.com (example.com)|2606:4700:10::ac42:93f3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: '/root/work/dl/example.html'

/root/work/dl/examp     [ <=>                ]     577  --.-KB/s    in 0s

2026-10-29 01:04:07 (13.9 MB/s) - '/root/work/dl/example.html' saved [577]

-O はアドレスに関係なく書いたパスに保存し、同じファイルがあるとそのファイルを上書きします。プログレスバーの行の先頭には、保存パスが途中で切れた形で表示されます。

-O の後ろにハイフンを指定すると、ファイルの代わりに標準出力へ出すため、パイプで別のコマンドに渡せます。メッセージが混ざらないように -q も一緒に付けます。

wget -q -O - https://example.com/ | head -n 4
<!doctype html><html lang=en><head><meta charset=utf-8><link rel=icon href=data:,><meta name=viewport content="width=device-width,initial-scale=1"><title>Example Domain</title><style>html{color-scheme:light dark;background:light-dark(#eee,#222)}body{font:16px/1.6 system-ui,sans-serif;max-width:26em;margin:auto;padding:25vh 2em 2em;text-align:center}</style></head><body><p>This domain is for use in documentation examples without needing permission. This is not a service; avoid relying on it for testing and monitoring purposes.</p><script src=/s.js></script></body></html>

example.com の HTML は一行なので、head も一行だけ出力します。

名前が似ている大文字の -O と小文字の -o は役割が違います。小文字の -o はメッセージを書き込むログファイルを指定します。そのため画面には何も表示されず、接続から保存までの行がログファイルに残ります。

wget -o /root/work/log/wget.log -P /root/work/dl/ https://example.com/
echo $?
0
cat /root/work/log/wget.log
--2026-10-29 01:08:11--  https://example.com/
Resolving example.com (example.com)... 2606:4700:10::6814:179a, 2606:4700:10::ac42:93f3, 104.20.23.154, ...
Connecting to example.com (example.com)|2606:4700:10::6814:179a|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: '/root/work/dl/index.html'

     0K                                                        25.6M=0s

2026-10-29 01:08:11 (25.6 MB/s) - '/root/work/dl/index.html' saved [577]

ログファイルには、プログレスバーの代わりに 0K で始まるドットの進捗行が入ります。四つのオプションが内容とメッセージをそれぞれどこへ送るかは、下のダイアグラムでボタンを押して比べられます。

💾 wget の保存オプションが変える行き先
解説ダイアグラム
💾 保存オプション
# wget -P /root/work/dl/ https://example.com/wget -O /root/work/dl/example.html https://example.com/wget -q -O - https://example.com/wget -o /root/work/log/wget.log -P /root/work/dl/ https://example.com/
🌐 アドレス
🔗https://example.com/ダウンロードするファイル
➜
⬇️ wget
📦内容受信したファイルのバイト
💬メッセージ接続、プログレスバー、結果の行
➜
📂 行き先
📦 内容/root/work/dl/index.html/root/work/dl/example.html画面(標準出力)/root/work/dl/index.html
💬 メッセージ画面画面-q でオフ/root/work/log/wget.log
-P はフォルダーだけを決めます。ファイル名はアドレスの末尾から決まって index.html として保存され、同じ名前があると末尾に .1 が付きます。
-O はファイルパスを丸ごと決めます。アドレスに関係なく example.html として保存され、既存のファイルは上書きされます。
-O の後ろにハイフンを指定すると、内容がファイルの代わりに画面に出ます。パイプで別のコマンドに渡すときに使います。メッセージが混ざらないように -q も一緒に付けます。
小文字の -o はメッセージの行き先を変えます。ファイルは -P のフォルダーにそのまま保存され、画面に出ていた接続の行と進捗の行はログファイルへ送られます。

3️⃣ wget -c でのダウンロード再開と速度制限、再試行

保存先を決めても、大きなファイルはダウンロードの途中で切れることがあります。Tomcat 9.0.123 の圧縮ファイルを --limit-rate=1m で毎秒 1MB までに制限してダウンロードし、21% で Ctrl+C を押して中断しました。

wget --limit-rate=1m -P /root/work/ https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz
--2026-10-29 01:07:47--  https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 2a04:4e42::644, 151.101.2.132
Connecting to dlcdn.apache.org (dlcdn.apache.org)|2a04:4e42::644|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13233352 (13M) [application/x-gzip]
Saving to: '/root/work/apache-tomcat-9.0.123.tar.gz'

che-tomcat-9.0.123.  21%[===>                ]   2.65M  1024KB/s
^C
ls -l /root/work/
total 2928
-rw-r--r-- 1 root root 2997039 Oct 29 01:07 apache-tomcat-9.0.123.tar.gz

中断したファイルは 2,997,039 バイトで残っています。同じコマンドに -c を付けて再実行すると、このサイズの続きからリクエストします。-c なしで同じアドレスをもう一度ダウンロードすると、続きからではなく、名前の後ろに .1 を付けた新しいファイルを最初からダウンロードします。

wget -c -P /root/work/ https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz
--2026-10-29 01:07:50--  https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 2a04:4e42::644, 151.101.2.132
Connecting to dlcdn.apache.org (dlcdn.apache.org)|2a04:4e42::644|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 13233352 (13M), 10236313 (9.8M) remaining [application/x-gzip]
Saving to: '/root/work/apache-tomcat-9.0.123.tar.gz'

apache-tomcat-9.0.1 100%[++++===============>]  12.62M  --.-KB/s    in 0.04s

2026-10-29 01:07:50 (262 MB/s) - '/root/work/apache-tomcat-9.0.123.tar.gz' saved [13233352/13233352]

レスポンスが [206 Partial Content] に変わり、Length 行には全体 13M のうち残りの 9.8M が表示されます。バーの中の + 記号は、すでに受信済みの前半部分です。マニュアルによると、-c は Range リクエストを受け付ける HTTP サーバーと FTP サーバーでだけ動作します。

ls -l /root/work/
total 15168
-rw-r--r-- 1 root root 13233352 Oct  6 19:11 apache-tomcat-9.0.123.tar.gz

ダウンロードし終えたファイルの時刻は Oct 6 19:11 です。wget は、サーバーが送った Last-Modified の時刻を受信したファイルにそのまま付けます。

続きからダウンロードしたファイルが元のファイルと同じかどうかは、Apache が一緒に配布している .sha512 ファイルで確認しました。

wget -q -P /root/work/ https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz.sha512
cd /root/work
sha512sum -c apache-tomcat-9.0.123.tar.gz.sha512
apache-tomcat-9.0.123.tar.gz: OK

[OK] が出れば、中断してから続きを受信したファイルも、公式の配布ファイルとバイト単位まで同じです。

-N は、サーバーのファイルがローカルのファイルより新しいときだけダウンロードします。同じ .sha512 を -N でもう一度リクエストした結果は [304 Not Modified] のレスポンスで、ダウンロードはスキップされました。

wget -N -P /root/work/ https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz.sha512
--2026-10-29 01:05:14--  https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz.sha512
Resolving dlcdn.apache.org (dlcdn.apache.org)... 2a04:4e42::644, 151.101.2.132
Connecting to dlcdn.apache.org (dlcdn.apache.org)|2a04:4e42::644|:443... connected.
HTTP request sent, awaiting response... 304 Not Modified
File '/root/work/apache-tomcat-9.0.123.tar.gz.sha512' not modified on server. Omitting download.

応答しないサーバーには、-T で待つ秒数を、-t で試行回数を指定します。デフォルト値は読み取りタイムアウト 900 秒、試行 20 回なので、そのままにすると長く止まったままになります。ドキュメントの例示用に予約されたアドレス 192.0.2.1 に -T 3 -t 2 を指定した結果、応答がなく二回試行した後、終了コード 4 で終わりました。

wget -T 3 -t 2 -O /root/work/dl/t.html http://192.0.2.1/
--2026-10-29 01:05:25--  http://192.0.2.1/
Connecting to 192.0.2.1:80... failed: Connection timed out.
Retrying.

--2026-10-29 01:05:29--  (try: 2)  http://192.0.2.1/
Connecting to 192.0.2.1:80... failed: Connection timed out.
Giving up.
echo $?
4

-O でダウンロード中に 404 やタイムアウトで失敗しても、そのパスには空のファイルが残ります。上の二つの失敗の後に ls で確認した二つのファイルは、どちらも 0 バイトでした。

ls -l /root/work/dl/nosuch.html /root/work/dl/t.html
-rw-r--r-- 1 root root 0 Oct 29 01:05 /root/work/dl/nosuch.html
-rw-r--r-- 1 root root 0 Oct 29 01:05 /root/work/dl/t.html

同じ 404 を -P でリクエストすると、ファイルは作られません。

wget -q -P /root/work/dl/ https://example.com/nosuch2.html
echo $?
8
ls -l /root/work/dl/nosuch2.html
ls: cannot access '/root/work/dl/nosuch2.html': No such file or directory

ls で確認すると、nosuch2.html がないという [No such file or directory] が表示されます。-O は失敗しても空のファイルを残すため、スクリプトで -O を使うときは、終了コードが 0 かどうかを確認してからそのファイルを読みます。

4️⃣ wget のオプション一覧と -i での複数ファイル、-b でのバックグラウンドダウンロード

ダウンロードするファイルが複数あるときは、アドレスを一行に一つずつ書いたファイルを -i で渡します。-nv を加えると、ファイルごとに結果を一行だけ出力します。

cat /root/work/urls.txt
https://example.com/
https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz.sha512
wget -nv -i /root/work/urls.txt -P /root/work/list/
2026-10-29 01:05:14 URL:https://example.com/ [577] -> "/root/work/list/index.html" [1]
2026-10-29 01:05:14 URL:https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz.sha512 [158/158] -> "/root/work/list/apache-tomcat-9.0.123.tar.gz.sha512" [1]
FINISHED --2026-10-29 01:05:14--
Total wall clock time: 0.3s
Downloaded: 2 files, 735 in 0s (14.7 MB/s)

最後の三行は -i でダウンロードした全体のまとめで、ダウンロードしたファイルの数とサイズの合計が表示されます。

時間のかかるダウンロードは、-b でバックグラウンドで実行します。マニュアルによると、-o なしで -b だけを指定すると現在のフォルダーに wget-log ファイルができるため、ログのパスも一緒に書きました。

wget -b -o /root/work/log/bg.log -P /root/work/bg/ https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz
Continuing in background, pid 3623651.
tail -n 4 /root/work/log/bg.log
 12900K .......... .......... ...                             100%  423M=0.07s

2026-10-29 01:05:22 (183 MB/s) - '/root/work/bg/apache-tomcat-9.0.123.tar.gz' saved [13233352/13233352]

シェルのプロンプトはすぐに戻り、ダウンロードはバックグラウンドのプロセスとして続きます。終わるとログの最後の行に [saved] が残ります。

オプションは下の 2 つのタブにまとめました。

オプションコピー説明
-O, --output-documentFILE保存するファイルパスを指定
-P, --directory-prefixPREFIX保存するフォルダーだけを指定
-q, --quietメッセージをオフにする
-o, --output-fileLOGFILEメッセージをログファイルへ
-c, --continue中断したファイルを続きからダウンロード
-N, --timestamping新しいときだけダウンロード
-nc, --no-clobberダウンロード済みのファイルがあればダウンロードしない
-T, --timeoutSECONDS待つ秒数を指定
-t, --triesNUMBER試行回数を指定
-i, --input-fileFILE複数のアドレスを一度に
-nv, --no-verboseファイルごとに結果を一行だけ出力
-b, --backgroundバックグラウンドでダウンロード
--spiderダウンロードせずにアドレスだけを確認
-S, --server-responseレスポンスヘッダーを表示
-r, --recursiveリンクをたどってダウンロード
-l, --levelDEPTHリンクをたどる深さを指定
--limit-rateAMOUNT速度制限
オプションコピー説明
--userUSERFTP と HTTP のダウンロードに使うユーザー名
--passwordPASSWORDFTP と HTTP のダウンロードに使うパスワード
--ask-password接続のたびにパスワードを尋ねる(--password と同時には使えない)
--ftp-userUSERFTP サーバーのユーザー名
--ftp-passwordPASSWORDFTP サーバーのパスワード
-U, --user-agentAGENTHTTP サーバーに AGENT という名前で名乗る
--headerHEADER-LINEHTTP リクエストごとにヘッダーを一行追加で送る
--no-check-certificateサーバー証明書を認証局と照合しない
-4, --inet4-onlyIPv4 アドレスだけで接続
-w, --waitSECONDSダウンロードとダウンロードの間に待つ秒数
-m, --mirrorミラーリングに合うオプションをオンにする(再帰、時刻の比較、深さ無制限)
-np, --no-parent再帰でダウンロードするとき、親フォルダーへ上がらない
-nd, --no-directories再帰でダウンロードするとき、フォルダー構造を作らない
-k, --convert-linksダウンロード後、文書内のリンクをローカルで見られるように変換
-A, --acceptACCLISTダウンロードするファイル名の末尾やパターンのリスト(カンマ区切り)

5️⃣ wget でのリンク確認とレスポンスヘッダー

ダウンロードの前にアドレスが生きているかだけを確認するときは、--spider を使います。ファイルは保存せず、レスポンスコードだけを確認します。

wget --spider https://example.com/
Spider mode enabled. Check if remote file exists.
--2026-10-29 01:05:03--  https://example.com/
Resolving example.com (example.com)... 2606:4700:10::6814:179a, 2606:4700:10::ac42:93f3, 104.20.23.154, ...
Connecting to example.com (example.com)|2606:4700:10::6814:179a|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving.
echo $?
0
wget --spider https://example.com/nosuch.html
Spider mode enabled. Check if remote file exists.
--2026-10-29 01:05:04--  https://example.com/nosuch.html
Resolving example.com (example.com)... 2606:4700:10::ac42:93f3, 2606:4700:10::6814:179a, 172.66.147.243, ...
Connecting to example.com (example.com)|2606:4700:10::ac42:93f3|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
Remote file does not exist -- broken link!!!
echo $?
8

存在するアドレスは終了コード 0 で、存在しないアドレスは [broken link] の文言とともに 8 で終わります。スクリプトでリンクを点検するときは、このコードをそのまま使います。

-S を加えると、サーバーが送ったレスポンスヘッダーも一緒に表示されます。

wget -S --spider https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz
Spider mode enabled. Check if remote file exists.
--2026-10-29 01:03:54--  https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.123/bin/apache-tomcat-9.0.123.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 2a04:4e42::644, 151.101.2.132
Connecting to dlcdn.apache.org (dlcdn.apache.org)|2a04:4e42::644|:443... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 200 OK
  Connection: keep-alive
  Content-Length: 13233352
  Server: Apache
  Last-Modified: Tue, 06 Oct 2026 19:11:51 GMT
  ETag: "c9ecc8-65d30c56390be"
  Access-Control-Allow-Origin: *
  Content-Type: application/x-gzip
  Via: 1.1 varnish, 1.1 varnish
  Accept-Ranges: bytes
  Age: 2047
  Date: Thu, 29 Oct 2026 01:03:54 GMT
  X-Cache: HIT, HIT
  X-Cache-Hits: 400, 0
Length: 13233352 (13M) [application/x-gzip]
Remote file exists.

Content-Length がファイルサイズで、Last-Modified は -N がローカルのファイルと比べる時刻です。[Accept-Ranges: bytes] は、このサーバーが -c の再開リクエストを受け付けるという意味です。

リンクをたどって複数のファイルをダウンロードする -r は、-l で深さを決めます。example.com に -l 1 を指定した結果、ページをダウンロードした後に robots.txt を確認し、ページが読み込む s.js まで二つのファイルをダウンロードしました。

wget -r -l 1 -P /root/work/mirror/ https://example.com/
--2026-10-29 01:05:38--  https://example.com/
Resolving example.com (example.com)... 2606:4700:10::ac42:93f3, 2606:4700:10::6814:179a, 172.66.147.243, ...
Connecting to example.com (example.com)|2606:4700:10::ac42:93f3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: '/root/work/mirror/example.com/index.html'

     0K                                                        28.7M=0s

2026-10-29 01:05:38 (28.7 MB/s) - '/root/work/mirror/example.com/index.html' saved [577]

Loading robots.txt; please ignore errors.
--2026-10-29 01:05:38--  https://example.com/robots.txt
Reusing existing connection to [example.com]:443.
HTTP request sent, awaiting response... 404 Not Found
2026-10-29 01:05:38 ERROR 404: Not Found.

--2026-10-29 01:05:38--  https://example.com/s.js
Reusing existing connection to [example.com]:443.
HTTP request sent, awaiting response... 200 OK
Length: 1963 (1.9K) [text/javascript]
Saving to: '/root/work/mirror/example.com/s.js'

     0K .                                                     100% 17.6M=0s

2026-10-29 01:05:38 (17.6 MB/s) - '/root/work/mirror/example.com/s.js' saved [1963/1963]

FINISHED --2026-10-29 01:05:38--
Total wall clock time: 0.2s
Downloaded: 2 files, 2.5K in 0s (19.3 MB/s)
find /root/work/mirror -type f
/root/work/mirror/example.com/index.html
/root/work/mirror/example.com/s.js

-P のフォルダーの下にドメイン名のフォルダーができ、その中にサイトのパスどおりにファイルが保存されます。

🔲 wget コマンドシミュレーター

下のシミュレーターでは、wget を -O、-P、-o、-c、-N、-i、--spider のオプションとともに実際に入力できます。ダウンロードできるアドレスと出力は本文と同じサンプル値で、21% で中断した Tomcat のファイルを -c で続きからダウンロードし、sha512sum で照合する流れまで試せます。

wget コマンドシミュレーターwget · root アカウント
[root@server work]#
キーボードの↑↓で、前に入力したコマンドを呼び出せます。

6️⃣ wget コマンドの実務例

  • ↪️ ダウンロードした tar.gz をすぐ展開する方法とダウンロード済みファイルのスキップ、最初によくある間違い

🔲 ダウンロードした tar.gz をすぐ展開する方法とダウンロード済みファイルのスキップ

インストールファイルをダウンロードしてすぐ展開するときは、圧縮ファイルを保存せずに tar へ渡します。hello-2.12.1.tar.gz を wget -qO- でダウンロードして tar -xzf - に渡すと、終了コード 0 で終わり、src フォルダーに hello-2.12.1 フォルダーが展開されました。

mkdir src
wget -qO- https://ftp.gnu.org/gnu/hello/hello-2.12.1.tar.gz | tar -xzf - -C src; echo $?
0
ls src; ls src/hello-2.12.1 | head -5
hello-2.12.1
ABOUT-NLS
AUTHORS
COPYING
ChangeLog
ChangeLog.O

cron のように同じコマンドを繰り返すときは、-nc を付けます。-nc は、ダウンロード済みのファイルがあるとダウンロードしません。二回実行すると二回目に [already there; not retrieving.] の行が表示され、dl フォルダーのファイルは一つのままでした。

wget -q -nc -P dl https://ftp.gnu.org/gnu/hello/hello-2.12.1.tar.gz.sig; echo $?
0
wget -nc -P dl https://ftp.gnu.org/gnu/hello/hello-2.12.1.tar.gz.sig; echo $?
File 'dl/hello-2.12.1.tar.gz.sig' already there; not retrieving.

0
ls dl
hello-2.12.1.tar.gz.sig

🔲 最初によくある間違い

疑問符と & 記号を含むアドレスは引用符で囲みます。引用符なしで [?C=M&O=D] を付けてダウンロードすると、シェルが & 記号をバックグラウンド実行と解釈し、wget には ?C=M までしか渡りません。[C=M&O=D] は更新時刻の新しい順に並べるという値なので、引用符なしでダウンロードした一覧は古い hello-1.3 から、引用符で囲んでダウンロードした一覧は最新の hello-2.12.3 から表示されました。

wget -q -O list.html https://ftp.gnu.org/gnu/hello/?C=M&O=D
grep -o "hello-[0-9.]*tar.gz\"" list.html | head -3
hello-1.3.tar.gz"
hello-2.1.1.tar.gz"
hello-2.1.0.tar.gz"
wget -q -O list.html "https://ftp.gnu.org/gnu/hello/?C=M&O=D"
grep -o "hello-[0-9.]*tar.gz\"" list.html | head -3
hello-2.12.3.tar.gz"
hello-2.12.2.tar.gz"
hello-2.12.1.tar.gz"

-O と -P を一緒に指定すると、-O に書いたパスがそのまま使われます。-P dl を付けて実行すると、hello.sig は dl フォルダーではなく現在のフォルダーに保存されました。

wget -q -P dl -O hello.sig https://ftp.gnu.org/gnu/hello/hello-2.12.1.tar.gz.sig; echo $?; ls hello.sig dl
0
hello.sig

dl:
hello-2.12.1.tar.gz.sig
hello.html
kernel.html

🔲 wget の結果の読み方

アドレスが別のアドレスへ転送されると、レスポンス行が二回表示されます。http://kernel.org/ をダウンロードしてレスポンス行だけを抜き出すと、最初のレスポンスは [301 Moved Permanently] で、Location 行に転送先のアドレス https://kernel.org/ が [following] とともに表示されました。二回目のリクエストでは [200 OK] と Length 行が表示されました。

wget -O dl/kernel.html http://kernel.org/ 2>&1 | grep -E "HTTP request|Location|Length"
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://kernel.org/ [following]
HTTP request sent, awaiting response... 200 OK
Length: 20436 (20K) [text/html]

そのため、最初のレスポンスが 301 でもダウンロードに失敗したわけではなく、最後のレスポンス行と Length 行が実際にダウンロードしたファイルの結果です。

-O、-P、-c オプションは wget コマンドシミュレーターで直接入力して試せます。

🔢 FAQ & おすすめコンテンツ

どちらのコマンドもアドレスの内容を取得しますが、公式ドキュメントによると、wget は受信した内容をファイルに保存するのが基本で、-r でリンクをたどって複数のファイルをダウンロードできます。curl は特に指定しなければ、受信した内容を標準出力に出します。wget で同じ動作にするには -O - を使います。

AlmaLinux 9 では、appstream リポジトリの wget パッケージを [dnf install wget] でインストールします。テストしたサーバーの wget 1.21 パッケージの提供元も appstream でした。Debian 系では [apt install wget] で同じ名前のパッケージをインストールします。

公式ドキュメントによると、--user と --password でアカウントを渡し、FTP だけを別に指定するときは --ftp-user と --ftp-password を使います。マニュアルには、コマンドラインに書いたパスワードは ps コマンドで誰にでも見えると書かれています。そのため、--ask-password で実行時に入力するか、ほかのアカウントが読めないように権限を制限した .netrc ファイルに置きます。

ℹ️ 提携について
当サイトのコンテンツにはアフィリエイトリンクが含まれています。訪問者がこのリンクを通じて商品やサービスを購入すると、当サイトは販売元から手数料を受け取ります。この過程において、購入者が支払う金額(イベント割引適用で金額が下がります↓)は上昇しません。掲載されている価格・割引・在庫情報は作成時点のものであり、実際とは異なる場合がありますので、ご購入前に販売元にて最終確認をお願いいたします。商品の選定および評価は独自の基準に基づいて行われており、手数料の支払いの有無は紹介順や評価内容に影響を与えません。

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です

人間であることを証明してください: 5   +   8   =