Q:

How do I resize a Linux filesystem data .bin file?

I have a 2 MB file: root.bin, that is a Linux rev 1.0 ext2 filesystem, and I would like to expand it so that I can put more files in it, but it does not just resize like I thought it would. How do I resize it, preferably keeping the files on it, to a specific size?

A:

We will try to make your two MB root.bin file to a 12 MB to demonstrate the steps.

Create a copy of your file (just in case for backup purpose):
cp root.bin 12mb.bin

Run this command to add 10M to the file size:
dd if=/dev/zero of=12mb.bin bs=1MiB count=10 conv=notrunc oflag=append

Now 12mb.bin is not 2M any more, actually its size is 12M.
Run:
e2fsck -f 12mb.bin

to check the filesystem on the file, then run:
resize2fs 12mb.bin

Done. mount it somewhere:
sudo mount 12mb.bin /mnt

check the size:
df -h --output=size /mnt/
Size
 12M

And the existence of files:
ls /mnt

We can also use losetup to act with the file like a block device:
sudo losetup -f 12mb.bin

then:
sudo losetup -l | grep -i "12mb.bin" | awk '{print $1}'
/dev/loop0

and we can resize /dev/loop0.

