おっきなファイルや多数のファイルを http、ftp で取ってくるのに便利な Wget をインストールします。
今回は ftp.gnu.org から wget-1.6.tar.gz をダウンロードしてインストールしてみます。
アーカイブを展開後にコンパイルしてインストールします。
↓こんな感じでさくさく。
% pwd |
実行可能なバイナリは <prefix_dir>/bin (上の例だと /opt/local/bin) にインストールされます。
せっかくなので path を通しておきましょ。
書式は以下になります。オプションと取ってくるファイルの url をダラーっと並べます。
Usage: wget [OPTION]... [URL]... |
設定ファイル (<prefix>/etc/wgetrc, $HOME/.wgetrc) を用意したり、 取得するファイルの url をファイルに記述して指定することで「ダラーっと」を緩和できます。
例えばおっきなファイルを一個だけ取ってくるような場合です。
% wget -b http://www.hogeratta.com/largefile.zip |
上の例では http でアクセスできる www.hogeratta.com/largefile.zip をバックグラウンド (-b オプション) で取得します。
たくさんのファイルの url を指定して取ってくるような場合です。
% cat url.txt http://www.hogeratta.com/file1 http://www.hogeratta.com/file2 http://www.hogeratta.com/file3 % wget -i ./url.txt |
上の例では www.hogeratta.com の file1, file2, file3 を取得します。
指定するファイル中のリンクをたどって取得します。
-r オプションが再帰的取得を意味しています。-l オプションで再帰の深度を指定します (デフォルトは 5 です)。
% wget -r -l 2 http://www.hogeratta.com/index.html |
上の例では、まず www.hogeratta.com/index.html を取得し、そのファイル中で参照、 あるいはリンクされるファイルを取得します (一段目の再帰的取得)。 次に、それら取得したファイル中で参照、あるいはリンクされるファイルを取得します (二段目の再帰的処理)。
デフォルトでは、起点の存在するホスト内だけで再帰取得が実行されますが、 -H オプションを指定すると他ホストに存在するファイルも取得の対象になります。
% wget -r -l 2 -H http://www.hogeratta.com/index.html |
-m オプションを指定することで -r -N -l inf -nr を指定するのと同等の意味になります。 -r で再帰取得、-N で新しいファイルのみの取得、-l inf で最大深度の再帰取得が指定されます。 -H は指定されないので単一ホスト内だけの再帰処理になります。 -nr は .listing ファイルを remove しないという指定です (.listing ファイルというのは ftp で retrieve する際、ディレクトリ構造を記録する一時ファイルのようです)。
% wget -m http://www.hogeratta.com/ |
cron で定期的に実行することでミラーサイトを構築できそうです。
特定のカテゴリのファイルだけを取得したいような場合です。
% wget -r -l 1 -nd -A gif,jpg http://www.hogeratta.com/index.html |
上の例では www.hogeratta.com/index.html 内でリンクされる gif, jpg を suffix に持つファイルだけを取得します (参照の必要がある index.html には当てはまりません)。 -l 1 を指定していますが、gif, jpg ファイルからは再帰取得されないので指定しなくても同じ結果になります。 また -nd を指定してますので、ディレクトリ構成は保持されず、ファイルはカレントディレクトリに取得されます。
デフォルトの動作を変更したり、コマンドラインでのオプション記述を省くために初期設定ファイルを利用できます。 <prefix>/etc/wgetrc は全てのユーザに対する設定ファイルで、 $HOME/.wgetrc は一人のユーザに対する設定ファイルです。 サンプルが <prefix>/etc/wgetrc としてインストールされますので利用できます。
wget --help で表示されるオプションの一覧です。
option | notes |
---|---|
Wget の version を表示します | |
help を表示します | |
バックグラウンドで実行します | |
wgetrc コマンドを指定します |
option | notes |
---|---|
ログを出力する FILE を指定します | |
ログを追加出力する FILE を指定します | |
デバッグ (用の) 出力をします | |
ログを出力しません | |
冗長なログを出力します (デフォルト) | |
簡潔なログを出力します | |
取得する url を記述した FILE を指定します | |
input file を HTML ファイルとして扱います (幾つかのオプションに影響します) | |
prepends URL to relative links in -F -i file. |
option | notes |
---|---|
bind to ADDRESS (hostname or IP) on local host. | |
set number of retries to NUMBER (0 unlimits). | |
write documents to FILE. | |
don't clobber existing files or use .# suffixes. | |
restart getting an existing file. | |
set retrieval display style. | |
ローカルに存在する file より新しくない file を取得しません | |
print server response. | |
don't download anything. | |
set the read timeout to SECONDS. | |
wait SECONDS between retrievals. | |
wait 1...SECONDS between retries of a retrieval. | |
turn proxy on or off. | |
set retrieval quota to NUMBER. |
option | notes |
---|---|
ローカルにディレクトリを作成しません (ファイルは全てカレントディレクトリに取得されます) | |
強制的にディレクトリを作成します (単一のファイルをディレクトリ構成を保持して取得したりとか) | |
ホスト名のディレクトリを作成しません (ホスト下のディレクトリは作成されます) | |
PREFIX で指定するディレクトリ下にファイルを取得します | |
ignore NUMBER remote directory components. |
option | notes |
---|---|
set http user to USER. | |
set http password to PASS. | |
(dis)allow server-cached data (normally allowed). | |
save all text/html documents with .html extension. | |
ignore `Content-Length' header field. | |
insert STRING among the headers. | |
set USER as proxy username. | |
set PASS as proxy password. | |
include `Referer: URL' header in HTTP request. | |
save the HTTP headers to file. | |
identify as AGENT instead of Wget/VERSION. |
option | notes |
---|---|
when recursing, retrieve linked-to files (not dirs). | |
turn file name globbing on or off. | |
use the "passive" transfer mode. |
option | notes |
---|---|
再帰的な取得を行います (通常 -l と同時に指定します) | |
再帰処理の深度を指定します (inf 或いは 0 で最大深度指定になります) | |
delete files locally after downloading them. | |
convert non-relative links to relative. | |
before converting file X, back up as X.orig. | |
-r -N -l inf -nr の指定と同等です (ミラーサイト作成用) | |
don't remove `.listing' files. | |
get all images, etc. needed to display HTML page. |
option | notes |
---|---|
取得を許可する suffix のリストをコンマで区切って指定 | |
取得を拒否する suffix のリストをコンマで区切って指定 | |
comma-separated list of accepted domains. | |
comma-separated list of rejected domains. | |
follow FTP links from HTML documents. | |
comma-separated list of followed HTML tags. | |
comma-separated list of ignored HTML tags. | |
他サイトからも取得します | |
相対指定のリンクだけを取得します | |
list of allowed directories. | |
list of excluded directories. | |
don't DNS-lookup hosts. | |
親ディレクトリを取得しません |
マニュアルに記述されている wgetrc コマンドを一覧します。
command | notes |
---|---|
Same as `-A'/`-R' (see section Types of Files). | |
Enable/disable host-prefixed file names. `-nH' disables it. | |
Enable/disable continuation of the retrieval--the same as `-c' (which enables it). | |
Enable/disable going to background--the same as `-b' (which enables it). | |
Enable/disable saving pre-converted files with the suffix `.orig'---the same as `-K' (which enables it). | |
Consider relative URLs in URL input files forced to be interpreted as HTML as being relative to string---the same as `-B'. | |
Bind to address, like the `--bind-address' option. | |
When set to off, disallow server-caching. See the `-C' option. | |
Convert non-relative links locally. The same as `-k'. | |
Ignore n remote directory components. | |
Debug mode, same as `-d'. | |
Delete after download--the same as `--delete-after'. | |
Top of directory tree--the same as `-P'. | |
Turning dirstruct on or off--the same as `-x' or `-nd', respectively. | |
Same as `-D' (see section Domain Acceptance). | |
Specify the number of bytes "contained" in a dot, as seen throughout the retrieval (1024 by default). You can postfix the value with `k' or `m', representing kilobytes and megabytes, respectively. With dot settings you can tailor the dot retrieval to suit your needs, or you can use the predefined styles (see section Download Options). | |
Specify the number of dots that will be printed in each line throughout the retrieval (50 by default). | |
Specify the number of dots in a single cluster (10 by default). | |
Specify the dot retrieval style, as with `--dot-style'. | |
Specify a comma-separated list of directories you wish to exclude from download--the same as `-X' (see section Directory-Based Limits). | |
Same as `--exclude-domains' (see section Domain Acceptance). | |
Follow FTP links from HTML documents--the same as `-f'. | |
Only follow certain HTML tags when doing a recursive retrieval, just like `--follow-tags'. | |
If set to on, force the input filename to be regarded as an HTML document--the same as `-F'. | |
Use string as FTP proxy, instead of the one specified in environment. | |
Turn globbing on/off--the same as `-g'. | |
Define an additional header, like `--header'. | |
Add a `.html' extension to `text/html' files without it, like `-E'. | |
Set HTTP password. | |
Use string as HTTP proxy, instead of the one specified in environment. | |
Set HTTP user to string. | |
When set to on, ignore Content-Length header; the same as `--ignore-length'. | |
Ignore certain HTML tags when doing a recursive retrieval, just like `-G' / `--ignore-tags'. | |
Specify a comma-separated list of directories you wish to follow when downloading--the same as `-I'. | |
Read the URLs from string, like `-i'. | |
Consider data longer than specified in content-length header as invalid (and retry getting it). The default behaviour is to save as much data as there is, provided there is more than or equal to the value in Content-Length. | |
Set logfile--the same as `-o'. | |
Your user name on the remote machine, for FTP. Defaults to `anonymous'. | |
Turn mirroring on/off. The same as `-m'. | |
Turn reading netrc on or off. | |
Same as `-nc'. | |
Disallow retrieving outside the directory hierarchy, like `--no-parent' (see section Directory-Based Limits). | |
Use string as the comma-separated list of domains to avoid in proxy loading, instead of the one specified in environment. | |
Set the output filename--the same as `-O'. | |
Download all ancillary documents necessary for a single HTML page to display properly--the same as `-p'. | |
Set passive FTP---the same as `--passive-ftp'. Some scripts and `.pm' (Perl module) files download files using `wget --passive-ftp'. If your firewall does not allow this, you can set `passive_ftp = never' to override the commandline. | |
Set your FTP password to password. Without this setting, the password defaults to `username@hostname.domainname'. | |
Set proxy authentication user name to string, like `--proxy-user'. | |
Set proxy authentication password to string, like `--proxy-passwd'. | |
Set HTTP `Referer:' header just like `--referer'. (Note it was the folks who wrote the HTTP spec who got the spelling of "referrer" wrong.) | |
Quiet mode--the same as `-q'. | |
Specify the download quota, which is useful to put in the global `wgetrc'. When download quota is specified, Wget will stop retrieving after the download sum has become greater than quota. The quota can be specified in bytes (default), kbytes `k' appended) or mbytes (`m' appended). Thus `quota = 5m' will set the quota to 5 mbytes. Note that the user's startup file overrides system settings. | |
Recursion level--the same as `-l'. | |
Recursive on/off--the same as `-r'. | |
Follow only relative links--the same as `-L' (see section Relative Links). | |
If set to on, remove FTP listings downloaded by Wget. Setting it to off is the same as `-nr'. | |
When set to on, retrieve symbolic links as if they were plain files; the same as `--retr-symlinks'. | |
Use (or not) `/robots.txt' file (see section Robots). Be sure to know what you are doing before changing the default (which is `on'). | |
Choose whether or not to print the HTTP and FTP server responses--the same as `-S'. | |
Same as `-nh' (see section Host Checking). | |
Same as `-H'. | |
Set timeout value--the same as `-T'. | |
Turn timestamping on/off. The same as `-N' (see section Time-Stamping). | |
Set number of retries per URL---the same as `-t'. | |
Turn proxy support on/off. The same as `-Y'. | |
Turn verbose on/off--the same as `-v'/`-nv'. | |
Wait n seconds between retrievals--the same as `-w'. | |
Wait up to n seconds between retries of failed retrievals only--the same as `--waitretry'. Note that this is turned on by default in the global `wgetrc'. |