Is there any way to create and format a partition using a bash script?

I think it can be done with fdisk but I don't know how to feed commands from the bash script into the fdisk shell then exit the fdisk shell.

I'm wanting to create a partition then format it to ntfs from within bash.

link|improve this question
feedback

2 Answers

I don't think you can create a partition with fdisk without interacting. You'll want to use parted or sfdisk

link|improve this answer
feedback

fdisk reads from stdin so you just need to feed it the appropriate commands. For example, the following clears the partition table, if there is one, and makes a new one that has a single parititon that is the; entire disk:

(echo o; echo n; echo p; echo 1; echo 1; echo; echo w) | fdisk

I recommend you do the task you want, recording what you type so you can reproduce it.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.