LoginSignup
8

More than 5 years have passed since last update.

posted at

updated at

フォルダ内のファイルをランダムに10個コピーする

動機

ファイル数が膨大なフォルダ内から10個ランダムにサンプリングして取得しようと思った.

環境

cygwin

コマンド

find ./target/ | grep txt | sort -R | tail -n 10 | xargs -n 1 sh -c 'cp -v $0 ./temp/'

解説

  • find ./target/
    • ./target/フォルダ以下にあるファイル・フォルダをすべて列挙する
  • grep txt
    • 文字列の列からtxtが入った文字列のみ表示
  • sort -R
    • 文字列の列をランダムに並び替え表示する
  • tail -n 10
    • 文字列の列から先頭10個を取得
  • xargs -n 1 sh -c 'cp -v $0 ./temp/'
    • 文字列の列から文字列一つずつ取ってきて\$0に置き換えて,'cp -v $0 ./temp/'を実行する.

感想

できました.

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
What you can do with signing up
8