wget 指令:選項與 -O、-P 儲存位置、-c 續傳

測試環境RHEL 系列AlmaLinux 9.8root 帳號
網路指令wget位置/usr/bin/wget

wget 指令是在 Linux 終端機下載 HTTP、HTTPS、FTP 網址上檔案的指令。在 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 續傳與速度限制、重試

即使指定了儲存位置,大檔案在下載途中仍可能中斷。以 --limit-rate=1m 把速度限制在每秒 1MB 下載 Tomcat 9.0.123 壓縮檔,在 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 只在伺服器上的檔案比本機檔案新時才下載。用 -N 重新請求同一個 .sha512,結果是 [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

用 -P 請求同樣的 404,就不會建立檔案。

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 在背景執行。依手冊,只加 -b 而不加 -o 時,會在目前的資料夾產生 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]

Shell 提示字元會立刻回來,下載則以背景程序繼續。結束後,記錄的最後一行會留下 [saved]。

選項整理在下方兩個分頁。

選項複製說明
-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-agentAGENT以 AGENT 名稱告知 HTTP 伺服器
--headerHEADER-LINE每個 HTTP 請求多送一行標頭
--no-check-certificate不以憑證機構核對伺服器憑證
-4, --inet4-only只以 IPv4 位址連線
-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 指令模擬器

在下方模擬器中,可以親手輸入搭配 -O、-P、-o、-c、-N、-i、--spider 選項的 wget。可下載的網址和輸出都是與本文相同的範例值,還能試到用 -c 續傳在 21% 中斷的 Tomcat 檔案、再用 sha512sum 比對的流程。

wget 指令模擬器wget · root 帳號
[root@server work]#
用鍵盤 ↑↓ 鍵可叫出先前輸入過的指令。

6️⃣ wget 指令實務範例

  • ↪️ 直接解開下載的 tar.gz 與略過已下載的檔案、一開始常犯的錯誤

🔲 直接解開下載的 tar.gz 與略過已下載的檔案

下載安裝檔並直接解開時,不儲存壓縮檔,而是交給 tar。用 wget -qO- 下載 hello-2.12.1.tar.gz 並交給 tar -xzf -,結果以結束代碼 0 結束,hello-2.12.1 資料夾解開到 src 資料夾中。

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] 下載時,Shell 會把 & 符號當成背景執行,傳給 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 上,用 [dnf install wget] 安裝 appstream 儲存庫的 wget 套件。測試的伺服器上 wget 1.21 套件的來源也是 appstream。Debian 系列則用 [apt install wget] 安裝同名套件。

依官方文件,用 --user 和 --password 傳遞帳號,只針對 FTP 另外指定時,使用 --ftp-user 和 --ftp-password。手冊中寫道,寫在命令列上的密碼透過 ps 指令任何人都看得到。因此要用 --ask-password 在執行時輸入,或放在限制權限、其他帳號無法讀取的 .netrc 檔案中。

ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Prove your humanity: 7   +   5   =