【コマンド(wget)】でダウンロードする方法


はじめに

環境

  • Windows11
  • WSL2
  • Ubuntu

ここで説明していること

  • Linuxなどの CLI 環境での実行方法
  • wgetコマンドを使ってファイルを取得、保存する
    • Windowsなら「右クリック」→「対象ファイルの保存」のようなこと

手順

ファイルのURLを取得

  • クリップボードに保存する
    • ブラウザで「右クリック」→「リンクのコピー」

ダウンロード実行

wget -O data.zip "https://・・・"
$ wget -O data.zip "https://・・・"
--2022-11-15 12:22:16--  https://・・・
Resolving 接続先ホスト (ドメイン)... xxx.xxx.xxx.xxx, ...
Connecting to 接続先ホスト (ドメイン)|xxx.xxx.xxx.xxx|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 620597 (606K) [application/zip]
Saving to: ‘data.zip’

data.zip                            100%[===================================================================>] 606.05K  --.-KB/s    in 0.1s

2022-11-15 12:22:17 (5.84 MB/s) - ‘data.zip’ saved [620597/620597]
  • 「data.zip」をダウンロード
    • ファイル名を「-O」で指定
    • 指定しないと変な文字列のファイル名になる(後でmvでも可)
  • URLは「"(ダブルクォーテーション)」で囲む
  • 「200 OK」
    • ダウンロード元のサーバとの通信が正常
  • 「data.zip saved」
    • ダウンロードが実行された
    • ファイルも保存できている

ファイルを確認

ls -l
$ ls -l
total 608
-rw-rw-r-- 1 futa futa 620597 Sep 29 10:33 data.zip
  • data.zip
    • ファイルが作成されている

うまくいかない場合

エラーの例

  • ERROR 400 Bad Request.
  • ERROR 403: Forbidden.

参考記事

  • 上記エラーの対処方法を説明した記事です


Posted by futa