LoginSignup
20
9

More than 5 years have passed since last update.

LinuxやMacで任意のサイズの「テキスト」ファイルを作る方法

Last updated at Posted at 2016-12-07

検証などの用途で、大容量のダミーファイルを作りたいことがあります。ddfallocateでは、バイナリファイルになってしまって、テキストエディタで開けません。次のコマンドで、素早く作成できます。

base64 /dev/urandom | head -c アスキー文字数(バイト数) > 作成するファイル

これを応用すると、決まった長さの行を繰り返すファイルや、一部の内容を固定したファイルも、簡単に作れます。

以下の例では、THIS IS HEADの行で始まり、100文字の行が100個続き、THIS IS TAILの行で終わる、XXX.logファイルを作成しています。

createRandomFile.sh
echo 'THIS IS HEAD' > XXX.log
base64 /dev/urandom | fold -w 100 | head -100 >> XXX.log
echo 'THIS IS TAIL' >> XXX.log

参考:
How to create a random .txt(Human readable text like ascii) file in linux
コマンドラインでランダムな10文字を得る方法

20
9
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
20
9