
ファイルシステムと特殊ファイルfooがあります。スクリプトがこのファイルシステムのフォルダへの任意のパスを複数回(5、10、または100)選択し、fooファイルを各フォルダにコピーするようにしたいと思います。
アイデアはありましたが、実際のシナリオにするには不十分であり、そのアイデアがパフォーマンスの面で意味があるかどうかはわかりませんでした。
idea in pseudo-script:
read into variable n how many random paths should be found
file / -type d > file # put all existing directory paths into a file
repeat n-times{
choose_random_line<file | xargsintosecondargument cp foo
/* chose a random line from file and use it as second argument of copy
command, first is foo. */
}
答え1
GNUシステムでは:
find / -type d -print0 | shuf -zn5 | xargs -r0n1 cp foo
(ファイルを/sysや/procなどのディレクトリにコピーすることは意味がないか不可能です。-xdev
マウントされたファイルシステムの選択したディレクトリに追加するだけです/
。)
以下を使用して FreeBSD および GNU と互換性を持たせることができます。
find / -type d -print0 | sort -zR | tr '\0\n' '\n\0' | head -n5 |
tr '\0\n' '\n\0' | xargs -r0n1 cp foo