rad blog programowanie, majsterkowanie, życie

How to generate sample files in Linux


Empty file

touch empty.txt

Binary files

The simplest binary safe way to generate file with given size is to use dd:

dd if=/dev/zero of=1MiB_00.bin bs=1MiB count=1

This will create 1 MiB file full of zeros. The zeros would compress nicely when sending over the network and the integrity of the file can be easily verified. The file size can be adjusted, it is bs * count. For very big files it would be wise to adjust both, otherwise, just edit the bs value. In this example I have used mebibyte as a unit for clarity, it is 1024 * 1024 bytes. Other suffixes like KiB, GiB, TiB and so on can also be used.

If a file with a little bit higher entropy is needed we can populate it with pseudo-random data:

dd if=/dev/urandom of=1MiB_random.bin bs=1MiB count=1


Using ImageMagick it’s easy to generate random images in different sizes and formats. To generate images in different formats like BMP, JPG, PNG, GIF or even WebP we only need to change the extension.
To get a list of supported formats type identify -list format.

# colorful noise
magick -size 320x320 xc: +noise Random noise_random_color_320x320.png
# grayscale noise
magick -size 320x320 xc:gray +noise Random noise_random_gray_320x320.png
# fractal plasma
magick -size 320x320 plasma:fractal plasma_fractal_320x320.png
# solid background in your favorite RGB color
magick -size 320x320 xc:'#f0f' fuchsia_320x320.png
# chessboard
magick -size 120x120 pattern:checkerboard -auto-level chessboard.png

Every format has its distinctive feature, here is a quick walkthrough.
You can incorporate these parameters into the above command if ImageMagick is used or use them onto other files e. g. found on the internet.


# progressive JPEG with 92% quality
magick lenna.tif -interlace Line -quality 92 lenna_progressive_q92.jpg
# best quality JPEG without exceeding given size
magick lenna_full.jpg -define jpeg:extent=100kb lenna_full_100kb.jpg
# convert JPEG into progressive JPEG (lossless)
jpegtran -progressive -copy all lenna_full.jpg > lenna_full_progressive.jpg
# add comment into JPEG
wrjpgcom -c "Lorem ipsum" image.jpg > comment.jpg
# add Exif metadata tag
exiftool -artist="rad" exif.jpg


# make white transparent
magick chessboard.png -transparent white chessboard_transparent.png

Add comment

rad blog programowanie, majsterkowanie, życie