1. 概要

サーバーにダミーのリクエストを送信したいが、リソースをダウンロードしたくない状況を考えてみます。 これは、サーバーが稼働しているかどうかを確認したり、キャッシュをウォームアップしたりするため、またはその他の理由で行う場合があります。

wgetおよびcurlコマンドを使用して、ファイルを要求し、それに応じてデータを保存しないようにすることができます。

2. wgetコマンドの使用

GNU wget は、Webからファイルをダウンロードするためのオープンソースの非対話型コマンドラインユーティリティです。 wget を使用すると、HTTP、HTTPS、およびFTPプロトコルを使用してファイルをダウンロードできます。

wget は、複数のファイルのダウンロード、ダウンロードの再開、帯域幅調整、再帰的ダウンロード、バックグラウンドダウンロード、ミラーサイトなどを可能にするいくつかのオプションを提供します。

次に、wgetを使用してファイルの保存を回避する方法を見てみましょう。

2.1. 出力リダイレクト

Linuxでは、コマンドは端末から入力を受け取り、出力を端末に返します。 出力リダイレクトは、デフォルトの端末以外の場所に出力を転送するのに役立ちます。 出力をファイル、ディレクトリにリダイレクトしたり、別のコマンドへの入力としてリダイレクトしたりできます。

wget は、出力リダイレクトの概念を使用して、出力をディレクトリにリダイレクトし、ファイルを別の名前で保存します。 一般に、ファイルをマシンに保存しないように、出力を / dev /nullにリダイレクトできます。

$ wget -O /dev/null https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip 
--2022-02-09 17:49:12--  https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip
Resolving github.com (github.com)... 140.82.113.3
Connecting to github.com (github.com)|140.82.113.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/Baeldung/spring-security-oauth/zip/refs/heads/master [following]
--2022-02-09 17:49:13--  https://codeload.github.com/Baeldung/spring-security-oauth/zip/refs/heads/master
Resolving codeload.github.com (codeload.github.com)... 140.82.114.9
Connecting to codeload.github.com (codeload.github.com)|140.82.114.9|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: '/dev/null'

/dev/null             [ <=>                        ]   1.49M  --.-KB/s    in 0.1s    

2022-02-09 17:49:13 (10.1 MB/s) - '/dev/null' saved [1564839]

上記のコマンドで、-Oオプションは出力をファイルにリダイレクトします。 出力を特別なファイルにリダイレクトしています。 / dev/null。 これは、書き込まれたすべてのデータを破棄すると同時に、実行された書き込み操作の成功を返す標準ファイルです。

wgetコマンドがmaster.zipファイルをローカルに保存しなかったことを確認しましょう。

$ ls -lah
total 0
drwxr-xr-x. 2 root root  6 Feb  9 17:36 .
drwxr-xr-x. 7 root root 97 Feb  9 17:36 ..

デフォルトでは、wgetコマンドは出力を現在のシェルにリダイレクトします。

$ wget -O- https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip

今回は、 master.zip ファイルの内容が端末に表示され、保存されません。

上記の出力では、wgetコマンドの出力が端末に出力されています。 役立つネットワーク情報がたくさん含まれています。 しかし、その出力も破棄したい場合はどうでしょうか。 この目的のために-qオプションを使用できます。

$ wget -qO /dev/null https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip

-q フラグを使用して、wgetコマンドの出力を停止します。

2.2. –delete-afterフラグの使用

保存せずにファイルを要求する単純なアプローチは、最初にそのファイルをダウンロードし、ダウンロードが完了した直後にそれを削除することです。 wgetコマンドの–delete-after フラグは、これをそのまま実行します。

$ wget --delete-after https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip
--2022-02-09 17:54:49--  https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip
Resolving github.com (github.com)... 140.82.113.3
Connecting to github.com (github.com)|140.82.113.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/Baeldung/spring-security-oauth/zip/refs/heads/master [following]
--2022-02-09 17:54:49--  https://codeload.github.com/Baeldung/spring-security-oauth/zip/refs/heads/master
Resolving codeload.github.com (codeload.github.com)... 140.82.114.10
Connecting to codeload.github.com (codeload.github.com)|140.82.114.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: 'master.zip.tmp'

master.zip.tmp       [ <=>                        ]   1.49M  --.-KB/s    in 0.1s    

2022-02-09 17:54:50 (10.0 MB/s) - 'master.zip.tmp' saved [1564839]

Removing master.zip.tmp.

ここでは、要求されたリソース master.zip、が最初に一時ファイルとしてダウンロードされました。 次に、ダウンロードしたファイルを削除します。 –delete-afterフラグはこれを暗黙的に実行します。

2.3. –spiderフラグの使用

wgetコマンドの–spider オプションは、リソースをダウンロードせずに、リソースが使用可能かどうかを確認するだけです。

$ wget --spider https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip
Spider mode enabled. Check if remote file exists.
--2022-02-10 16:48:21--  https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip
Resolving github.com (github.com)... 140.82.112.4
Connecting to github.com (github.com)|140.82.112.4|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/Baeldung/spring-security-oauth/zip/refs/heads/master [following]
Spider mode enabled. Check if remote file exists.
--2022-02-10 16:48:21--  https://codeload.github.com/Baeldung/spring-security-oauth/zip/refs/heads/master
Resolving codeload.github.com (codeload.github.com)... 140.82.112.10
Connecting to codeload.github.com (codeload.github.com)|140.82.112.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Remote file exists.

これまでに説明した他の2つのアプローチとは異なり、このアプローチではリソースをマシンにダウンロードしません。 したがって、これはすべての中で最も最適化されたソリューションです。

3. curlコマンドの使用

curl は、libcurlライブラリのコマンドラインユーティリティです。 クライアントとサーバー間のデータ転送に役立ちます。

curl コマンドを使用して、リソースが使用可能かどうかを確認してみましょう。

$ curl https://github.com/Baeldung/spring-security-oauth/archive/refs/heads/master.zip > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   146  100   146    0     0   1327      0 --:--:-- --:--:-- --:--:--  1315

と同じように wget コマンドでは、リダイレクト演算子を使用して、ダウンロードしたコンテンツをにリダイレクトしました / dev/null。 最後に、を使用してファイルがダウンロードされていないことを確認できます。 ls 指図。

4. 結論

このチュートリアルでは、ファイルをローカルに保存せずにファイルを要求するためのさまざまなアプローチについて説明しました。 curlおよびwgetコマンドで出力リダイレクトメカニズムを使用することにより、出力を破棄できます。 さらに、 wget コマンドでサポートされているいくつかのオプションを使用して、作業を完了しました。