Generating files of specific size

I've been working on getting a script to take size, dir name and file name variables from an input file and creating the same dir structure along with the file of specific size.

An example of the input file:

size/dirname/filename

2100/JAN_06/12345ABC.TCC
2354/FEB_06/24564XYZ.NOS
11240/MAR_06/1212ABAB.NCC

I am able to get results with the following code:

#!/bin/sh

set filesize=$1
set dirname=$2
set filename=$3

awk -F/ '{print $1,$2,$3}' os_listing.out | \
while read filesize dirname filename
do
        mkdir $dirname
        cd $dirname
        dd if=test/inputfile of=$filename bs=1 count=$filesize
        cd ..
done

The input file is just a file with random text that equals the size of the largest file this script will create.

What I'm trying to figure out is how to add text of a fixed character length, specifically the file name inside each file when it's created, then have the dd command create the file of specific size.

If I use any of the seek options with the dd command, it will just add onto the count size, which results in larger files.

Please let me know if additional information is needed.

Thank you in advance for any help and support.

It depends on where you want to add the filename in the output file, but look at this:

#!/bin/ksh

awk -F/ '{print $1,$2,$3}' os_listing.out | while read filesize dirname filename; do
        mkdir $dirname
        cd $dirname
        echo $filename $filesize > $filename
        count=$(($filesize-${#filename}))
        dd if=/tmp/inputfile of=$filename bs=1 count=$count seek=${#filename}
        cd ..
done

This code adds the filename to the beginning of the file and then appends (count-length_of_filename) characters from the input file.

That works beautifully. Thank you very much for the quick reply. I truly appreciate the great help. :slight_smile: