動かざることバグの如し

近づきたいよ 君の理想に

Linuxでファイル名をランダムに一括変更するコマンド

環境

やりたいこと

例えば以下のようなファイルがたくさんあったとする。これをランダムなファイル名に一括変更したい

./my1.txt
./my10.txt
./my11.txt
./my12.txt
./my13.txt
./my14.txt
./my2.txt
./my3.txt
./my4.txt
./my5.txt
./my6.txt
./my7.txt
./my8.txt
./my9.txt

コマンド

find . -type f -iname "*.txt" -exec bash -c 'mv "$0" "${0%/*}/$(head /dev/urandom | tr -dc "0-9" | head -c 6).txt"' {} \;

実行すると以下のようになる

./200500.txt
./217328.txt
./227527.txt
./228890.txt
./391966.txt
./461458.txt
./540946.txt
./595365.txt
./600386.txt
./780341.txt
./838847.txt
./866032.txt
./899626.txt
./960757.txt

Macで実行するとエラーになってファイル消えるので注意