wget 指令用法與教學

wget 是 linux 中除了 curl 外另一個檔案下載的好用工具。
以下是 wget --help 後所得到所有指令用法,
含完整中英文指令集,供大家參考。

圖片來源:http://www.mymacosx.com/terminal/wget-replacement-macos.html

這邊舉一個砍站的指令當作例子,指令為:
wget -t0 -c -nH -np -m -P /localdir http://example.com/mirrors/ftp.redhat.com

# -t0: 設定重試次數。當連結中斷或超時,wget會重新連接。-t0代表把重試次數設為無窮多。
# -c:  設定續傳功能。
# -nH: 不建立該網站名稱的子目錄 /example.com/,而直接在當前目錄下建立鏡像的目錄結構。
# -np: 不遍歷父目錄,如果有連結連到目標資料夾的parent或其他目錄,不下載。
# -m:  鏡像,相當同時使用-r和-N。
# -r:  遞迴下載,把文件中所有的連結都下載回來。
# -N:  下載時檢查timestamp,只下載有更新的文件,如果檔案大小和最修改日期都一樣就不下載。
# -P:  指定下載到本機的某個目錄下。

PDF 下載:
Wget Manual

純文字:
GNU Wget 1.12,非互動式檔案下載工具。
用法: wget [選項]... [URL]...
長選項必須用的參數在使用短選項時也是必須的。

啟動:
  -V,  --version             顯示 Wget 版本並離開
  -h,  --help                印出這段說明文字
  -b,  --background          啟動後進入背景作業
  -e,  --execute=指令        執行 ‘.wgetrc’ 形式的指令

紀錄訊息及輸入檔案:
  -o,  --output-file=檔案      將紀錄訊息寫入<檔案>中
  -a,  --append-output=檔案    將紀錄訊息加入<檔案>末端
  -d,  --debug                 印出偵錯訊息
  -q,  --quiet                 安靜模式 (不輸出訊息)
  -v,  --verbose               詳細輸出模式 (預設使用這個模式)
  -nv, --non-verbose           關閉詳細輸出模式,但不啟用安靜模式
  -i,  --input-file=FILE       download URLs found in local or external FILE.
  -F,  --force-html            以 HTML 方式處理輸入檔
  -B,  --base=URL              resolves HTML input-file links (-i -F)
     relative to URL.

下載:
  -t,  --tries=次數               設定重試次數 (0 表示無限)
       --retry-connrefused        即使連線被拒仍然會不斷嘗試
  -O   --output-document=檔案     將資料寫入指定檔案中
  -nc, --no-clobber               不覆寫已經存在的檔案
  -c,  --continue                 繼續下載已下載了一部份的檔案
       --progress=方式            選擇下載進度的表示方式
  -N,  --timestamping             除非遠端檔案比較新,否則不下載遠端檔案
  -S,  --server-response          顯示伺服器回應訊息
       --spider                   不下載任何資料
  -T,  --timeout=秒數             指定所有時限為同一數值
       --dns-timeout=秒數         指定 DNS 查找主機的時限
       --connect-timeout=秒數     指定連線時限
       --read-timeout=秒數        指定讀取資料的時限
  -w,  --wait=秒數                每次下載檔案之前等待指定秒數
       --waitretry=秒數           每次重覆嘗試前稍等一段時間 (由 1 秒至指定秒數不等)
       --random-wait              每次下載之前隨機地指定等待的時間
       --no-proxy                 禁止使用代理伺服器
  -Q,  --quota=大小               設定下載資料的限額大小
       --bind-address=位址        使用本機的指定位址 (主機名稱或 IP) 進行連線
       --limit-rate=速率          限制下載速率
       --no-dns-cache             不記憶 DNS 查找主機的資料
       --restrict-file-names=OS   只使用作業系統能夠接受的字元作為檔案字元
       --ignore-case              ignore case when matching files/directories.
  -4,  --inet4-only               只會連接 IPv4 地址
  -6,  --inet6-only               只會連接 IPv6 地址
       --prefer-family=FAMILY     優先採用指定的位址格式,可以是 IPv6、IPv4 或者 none
       --user=用戶                指定 ftp 和 http 用戶名稱
       --password=PASS            指定 ftp 和 http 密碼
       --ask-password             prompt for passwords.
       --no-iri                   turn off IRI support.
       --local-encoding=ENC       use ENC as the local encoding for IRIs.
       --remote-encoding=ENC      use ENC as the default remote encoding.

目錄:
  -nd  --no-directories            不建立目錄
  -x,  --force-directories         強制建立目錄
  -nH, --no-host-directories       不建立含有遠端主機名稱的目錄
       --protocol-directories      在目錄中加上通訊協定名稱
  -P,  --directory-prefix=名稱     儲存檔案前先建立指定名稱的目錄
       --cut-dirs=數目             忽略遠端目錄中指定<數目>的目錄層

HTTP 選項:
       --http-user=用戶          指定 HTTP 用戶名稱
       --http-passwd=密碼        指定 HTTP 密碼
       --no-cache                不使用伺服器中的快取記憶資料
       --default-page=NAME       Change the default page name (normally
     this is `index.html'.).
  -E,  --adjust-extension        save HTML/CSS documents with proper extensions.
       --ignore-length           忽略 ‘Content-Length’ 標頭欄位
       --header=字串             在連線資料標頭中加入指定字串
       --max-redirect            maximum redirections allowed per page.
       --proxy-user=用戶         設定代理伺服器用戶名稱
       --proxy-password=密碼     設定代理伺服器密碼
       --referer=URL             在 HTTP 請求中包括 ‘Referer: URL’ 標頭
       --save-headers            將 HTTP 連線資料標頭存檔
  -U,  --user-agent=AGENT        宣稱為 AGENT 而不是 Wget/VERSION
       --no-http-keep-alive      不使用 HTTP keep-alive (持久性連線)
       --no-cookies              不使用 cookie
       --load-cookies=檔案       程式啟動時由指定檔案載入 cookie
       --save-cookies=檔案       程式結束後將 cookie 儲存至指定檔案
       --keep-session-cookies    會載入和儲存暫時性的 cookie
       --post-data=字串          使用 POST 方式送出字串
       --post-file=檔案          使用 POST 方式送出檔案內容
       --content-disposition     honor the Content-Disposition header when
     choosing local file names (EXPERIMENTAL).
       --auth-no-challenge       send Basic HTTP authentication information
     without first waiting for the server's challenge.

HTTPS (SSL/TLS) 選項:
       --secure-protocol=PR      選擇安全通訊協定,可以使用 auto, SSLv2, SSLv3 或 TLSv1
       --no-check-certificate    不檢驗伺服器的憑證
       --certificate=檔案                指定用戶端的憑證檔案名稱
       --certificate-type=類型           用戶端憑證的類型,可以是 PEM 或 DER
       --private-key=檔案                指定私鑰檔案
       --private-key-type=類型           私鑰的類型,可以是 PEM 或 DER
       --ca-certificate=檔案             載有憑證管理中心 (CA) 簽章的檔案
       --ca-directory=目錄               載有憑證管理中心 (CA) 簽章的目錄
       --random-file=檔案                作為 SSL 隨機數產生程序 (PRNG) 的來源數據檔案
       --egd-file=檔案                   產生隨機數據的 EGD socket 檔案名稱

FTP 選項:
       --ftp-user=用戶           指定 FTP 用戶名稱
       --ftp-password=密碼       設定 FTP 密碼
       --no-remove-listing       不刪除 ‘.listing’ 檔案
       --no-glob                 不展開有萬用字元的 FTP 檔名
       --no-passive-ftp          不使用「被動」傳輸模式
       --retr-symlinks           在遞迴模式中,下載鏈結指示的目標檔案 (不包括目錄)

遞迴下載:
  -r,  --recursive            遞迴下載
  -l,  --level=數字           最大搜尋深度 (inf 或 0 表示無限)
       --delete-after         刪除下載後的檔案
  -k,  --convert-links        make links in downloaded HTML or CSS point to
     local files.
  -K,  --backup-converted     將檔案 X 轉換前先備份為 X.orig
  -m,  --mirror               相等於 -N -r -l inf --no-remove-listing 選項
  -p,  --page-requisites      下載所有顯示網頁所需的檔案,例如圖片等
       --strict-comments      用嚴格方式 (SGML) 處理 HTML 注釋。

遞迴下載時有關接受/拒絕的選項:
  -A,  --accept=清單                接受的檔案樣式,以逗號分隔
  -R,  --reject=清單                排除的檔案樣式,以逗號分隔
  -D,  --domains=清單               接受的網域,以逗號分隔
       --exclude-domains=清單       排除的網域,以逗號分隔
       --follow-ftp                 跟隨 HTML 文件中的 FTP 鏈結
       --follow-tags=清單           會跟隨的 HTML 標籤,以逗號分隔
  -G,  --ignore-tags=清單           會忽略的 HTML 標籤,以逗號分隔
  -H,  --span-hosts                 遞迴模式中可進入其它主機
  -L,  --relative                   只跟隨相對鏈結
  -I,  --include-directories=清單   準備下載的目錄
  -X,  --exclude-directories=清單   準備排除的目錄
  -np, --no-parent                  不進入上層的目錄

請將錯誤報告或建議寄給 >bug-wget@gnu.org<。

Usage: wget [OPTION]... [URL]...

Mandatory arguments to long options are mandatory for short options too.

Startup:
  -V,  --version             display the version of Wget and exit.
  -h,  --help                print this help.
  -b,  --background          go to background after startup.
  -e,  --execute=COMMAND     execute a `.wgetrc'-style command.

Logging and input file:
  -o,  --output-file=FILE      log messages to FILE.
  -a,  --append-output=FILE    append messages to FILE.
  -d,  --debug                 print lots of debugging information.
  -q,  --quiet                 quiet (no output).
  -v,  --verbose               be verbose (this is the default).
  -nv, --no-verbose            turn off verboseness, without being quiet.
  -i,  --input-file=FILE       download URLs found in FILE.
  -F,  --force-html            treat input file as HTML.
  -B,  --base=URL              prepends URL to relative links in -F -i file.

Download:
  -t,  --tries=NUMBER             set number of retries to NUMBER (0 unlimits).
       --retry-connrefused        retry even if connection is refused.
  -O,  --output-document=FILE     write documents to FILE.
  -nc, --no-clobber               skip downloads that would download to existing files.
  -c,  --continue                 resume getting a partially-downloaded file.
       --progress=TYPE            select progress gauge type.
  -N,  --timestamping             don't re-retrieve files unless newer than local.
  -S,  --server-response          print server response.
       --spider                   don't download anything.
  -T,  --timeout=SECONDS          set all timeout values to SECONDS.
       --dns-timeout=SECS         set the DNS lookup timeout to SECS.
       --connect-timeout=SECS     set the connect timeout to SECS.
       --read-timeout=SECS        set the read timeout to SECS.
  -w,  --wait=SECONDS             wait SECONDS between retrievals.
       --waitretry=SECONDS        wait 1..SECONDS between retries of a retrieval.
       --random-wait              wait from 0...2*WAIT secs between retrievals.
       --no-proxy                 explicitly turn off proxy.
  -Q,  --quota=NUMBER             set retrieval quota to NUMBER.
       --bind-address=ADDRESS     bind to ADDRESS (hostname or IP) on local host.
       --limit-rate=RATE          limit download rate to RATE.
       --no-dns-cache             disable caching DNS lookups.
       --restrict-file-names=OS   restrict chars in file names to ones OS allows.
       --ignore-case              ignore case when matching files/directories.
  -4,  --inet4-only               connect only to IPv4 addresses.
  -6,  --inet6-only               connect only to IPv6 addresses.
       --prefer-family=FAMILY     connect first to addresses of specified family,
     one of IPv6, IPv4, or none.
       --user=USER                set both ftp and http user to USER.
       --password=PASS            set both ftp and http password to PASS.

Directories:
  -nd, --no-directories            don't create directories.
  -x,  --force-directories         force creation of directories.
  -nH, --no-host-directories       don't create host directories.
       --protocol-directories      use protocol name in directories.
  -P,  --directory-prefix=PREFIX   save files to PREFIX/...
       --cut-dirs=NUMBER           ignore NUMBER remote directory components.

HTTP options:
       --http-user=USER          set http user to USER.
       --http-password=PASS      set http password to PASS.
       --no-cache                disallow server-cached data.
  -E,  --html-extension          save HTML documents with `.html' extension.
       --ignore-length           ignore `Content-Length' header field.
       --header=STRING           insert STRING among the headers.
       --max-redirect            maximum redirections allowed per page.
       --proxy-user=USER         set USER as proxy username.
       --proxy-password=PASS     set PASS as proxy password.
       --referer=URL             include `Referer: URL' header in HTTP request.
       --save-headers            save the HTTP headers to file.
  -U,  --user-agent=AGENT        identify as AGENT instead of Wget/VERSION.
       --no-http-keep-alive      disable HTTP keep-alive (persistent connections).
       --no-cookies              don't use cookies.
       --load-cookies FILE       load cookies from FILE before session.
       --save-cookies FILE       save cookies to FILE after session.
       --keep-session-cookies    load and save session (non-permanent) cookies.
       --post-data=STRING        use the POST method; send STRING as the data.
       --post-file=FILE          use the POST method; send contents of FILE.
       --content-disposition     honor the Content-Disposition header when
     choosing local file names (EXPERIMENTAL).
       --auth-no-challenge       Send Basic HTTP authentication information
     without first waiting for the server's challenge.

HTTPS (SSL/TLS) options:
       --secure-protocol=PR      choose secure protocol, one of auto, SSLv2,
     SSLv3, and TLSv1.
       --no-check-certificate    don't validate the server's certificate.
       --certificate=FILE        client certificate file.
       --certificate-type=TYPE   client certificate type, PEM or DER.
       --private-key=FILE        private key file.
       --private-key-type=TYPE   private key type, PEM or DER.
       --ca-certificate=FILE     file with the bundle of CA's.
       --ca-directory=DIR        directory where hash list of CA's is stored.
       --random-file=FILE        file with random data for seeding the SSL PRNG.
       --egd-file=FILE           file naming the EGD socket with random data.

FTP options:
       --ftp-user=USER           set ftp user to USER.
       --ftp-password=PASS       set ftp password to PASS.
       --no-remove-listing       don't remove `.listing' files.
       --no-glob                 turn off FTP file name globbing.
       --no-passive-ftp          disable the "passive" transfer mode.
       --retr-symlinks           when recursing, get linked-to files (not dir).
       --preserve-permissions    preserve remote file permissions.

Recursive download:
  -r,  --recursive            specify recursive download.
  -l,  --level=NUMBER         maximum recursion depth (inf or 0 for infinite).
       --delete-after         delete files locally after downloading them.
  -k,  --convert-links        make links in downloaded HTML point to local files.
  -K,  --backup-converted     before converting file X, back up as X.orig.
  -m,  --mirror               shortcut for -N -r -l inf --no-remove-listing.
  -p,  --page-requisites      get all images, etc. needed to display HTML page.
       --strict-comments      turn on strict (SGML) handling of HTML comments.

Recursive accept/reject:
  -A,  --accept=LIST                comma-separated list of accepted extensions.
  -R,  --reject=LIST                comma-separated list of rejected extensions.
  -D,  --domains=LIST               comma-separated list of accepted domains.
       --exclude-domains=LIST       comma-separated list of rejected domains.
       --follow-ftp                 follow FTP links from HTML documents.
       --follow-tags=LIST           comma-separated list of followed HTML tags.
       --ignore-tags=LIST           comma-separated list of ignored HTML tags.
  -H,  --span-hosts                 go to foreign hosts when recursive.
  -L,  --relative                   follow relative links only.
  -I,  --include-directories=LIST   list of allowed directories.
  -X,  --exclude-directories=LIST   list of excluded directories.
  -np, --no-parent                  don't ascend to the parent directory.

總之 wget 真是博大精深,要用得好還是得再深入研究才行。
大概就這樣簡單介紹,其他的若之後有用到再補充。

關鍵字:wget, linux, wget指令, wget proxy, wget教學, wget砍站, wget參數
參考資料:

這個網誌中的熱門文章

【銀行代碼查詢】3碼銀行代碼列表、7碼分行代碼查詢

【博客來折價券】25/50/100/150現領現折+天天簽到換200+OP兩倍換!

【博客來折價券】博客來免費序號e-coupon分享(持續更新)

Gmail 無限分身術:產生無限 email 帳號

【新光卡攻略】台灣Pay單筆888送50+寰宇3%,LINE會員繳費10%!