3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

wgetで特定のディレクトリだけダウンロードしたい場合

Last updated at Posted at 2021-03-21

はじめに

書籍をもとにコードを書いている際、GitHub等にあるデータを読み込みたい場合がある.
例えば、"Deep Learning with PyTorch"の"2.2.2 CycleGAN"において馬の画像をシマウマの画像に変換するために、"https://github.com/deep-learning-with-pytorch/dlwpt-code/tree/master/data/p1ch2/horse2zebra_0.4.0.pth" をダウンロードする必要がある.

この際、Google Colabなどで作業している場合、愚直にやれば上記ファイルをローカルでダウンロードし、それをGoogle Drive等にアップロードするという手順を踏むことになると思うが、面倒&時間がかかるためGoogle Colab内で完結させたい.

先に結論:

> wget -nH --cut-dirs=4 -r --no-parent <URL>

wget

当然、wgetで以下のようにダウンロードすることができる.

> wget https://github.com/deep-learning-with-pytorch/dlwpt-code/tree/master/data/p1ch2/horse2zebra_0.4.0.pth

だが、毎回これをするのは面倒なので、当該リポジトリのdataフォルダごとGoogle Driveにダウンロードし、以下のようにデータへのアクセスを省力化することを考える.

# Google Colabでの処理
from google.colab import drive
drive.mount('/content/gdrive')

data_path = './gdrive/My Drive/Colab Notebooks/<dir_name>/data/'

問題

  1. 普通にwget -r すると、親ディレクトリのファイル等も含む関係ないものまでダウンロードしてしまう
    • -> --no-parentオプションを用いる
  2. 最後のdata/だけダウンロードしたいのに、github.comフォルダからの階層を維持してダウンロードされる(ディレクトリ構成の例: github/deep-learning-with-pytorch/dlwpt-code/tree/master/data/
    • -> -nHオプションと--cut-dirsオプションの組み合わせ
      • -nHオプション: ホストを無視(github.com)
      • --cut-dirsオプション: --cut-dirs=nの形式でcutする階層数をnで指定する(上の例で-nHオプションと組み合わせた場合、n=2とすると、github/deep-learning-with-pytorch/dlwpt-code/までcutできる)

よって、以下のコマンドをたたけばよい.

> wget -nH --cut-dirs=4 -r --no-parent \
https://github.com/deep-learning-with-pytorch/dlwpt-code/tree/master/data/ (-P ./<dir_name>/)

必要であれば、-Pでダウンロード先のディレクトリを指定する.

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?