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

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?





Is your specific confusion that you have increased the size of the file, but the filesystem it contains doesn't seem to have changed to match?
– Carcer
Sep 2 at 14:22




1 Answer
1



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


root.bin



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:


losetup


sudo losetup -f 12mb.bin



then:


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



and we can resize /dev/loop0.


/dev/loop0





The dd command doesn't overwrite the original contents
– George Udosen
Sep 2 at 8:57


dd





@GeorgeUdosen No it does not. we are using notrunc and append. and it shouldn't right?
– Ravexina
Sep 2 at 8:57



notrunc


append





Your right sorry I am accessing from mobile didn't see those options almost thought you were using some esoteric code. Always nice to follow your answer always a teaching moment for me
– George Udosen
Sep 2 at 9:01





Rather than using dd to expand the file, why not just use truncate?
– Carcer
Sep 2 at 14:19



dd


truncate





@Carcer That's also a good option, I just used to use dd ...
– Ravexina
Sep 2 at 14:25


dd



Thanks for contributing an answer to Ask Ubuntu!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)