Melanchole
Melanchole

Reputation: 35

Bash script that creates files of a set size

I'm trying to set up a script that will create empty .txt files with the size of 24MB in the /tmp/ directory. The idea behind this script is that Zabbix, a monitoring service, will notice that the directory is full and wipe it completely with the usage of a recovery expression.

However, I'm new to Linux and seem to be stuck on the script that generates the files. This is what I've currently written out.

today="$( date +¨%Y%m%d" )"
number=0

while test -e ¨$today$suffix.txt¨; do
    (( ++number ))
    suffix=¨$( printf -- %02d ¨$number¨ )
done

fname=¨$today$suffix.txt¨

printf ´Will use ¨%s¨ as filename\n´ ¨$fname¨
printf -c  24m /tmp/testf > ¨$fname¨

I'm thinking what I'm doing wrong has to do with the printf command. But some input, advice and/or directions to a guide to scripting are very welcome.

Many thanks, Melanchole

Upvotes: 0

Views: 93

Answers (1)

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17415

I guess that it doesn't matter what bytes are actually in that file, as long as it fills up the temp dir. For that reason, the right tool to create the file is dd, which is available in every Linux distribution, often installed by default.

Check the manpage for different options, but the most important ones are

  • if: the input file, /dev/zero probably which is just an endless stream of bytes with value zero
  • of: the output file, you can keep the code you have to generate it
  • count: number of blocks to copy, just use 24 here
  • bs: size of each block, use 1MB for that

Upvotes: 2

Related Questions