Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

7 Answers 7

up vote 2 down vote accepted

Similar to the previous suggestions, piping commands to fidsk, I've found this approach useful to leave details for subsequent maintainers. The sed bits strip off all the comments before fdisk gets the input.

# to create the partitions programatically (rather than manually)
# we're going to simulate the manual input to fdisk
# The sed script strips off all the comments so that we can 
# document what we're doing in-line with the actual commands
# Note that a blank line (commented as "defualt" will send a empty
# line terminated with a newline to take the fdisk default.
sed -e 's/\t\([\+0-9a-zA-Z]*\)[ \t].*/\1/' << EOF | fdisk ${TGTDEV}
  o # clear the in memory partition table
  n # new partition
  p # primary partition
  1 # partition number 1
    # default - start at beginning of disk 
  +100M # 100 MB boot parttion
  n # new partition
  p # primary partition
  2 # partion number 2
    # default, start immediately after preceding partition
    # default, extend partition to end of disk
  a # make a partition bootable
  1 # bootable partition is partition 1 -- /dev/sda1
  p # print the in-memory partition table
  w # write the partition table
  q # and we're done
share|improve this answer
I completely agree. Document your work for others to read. – Eric Duncan Nov 26 at 13:55

You can do it with just a couple of commands, use intros \n instead of multiple echos.

echo -e "o\nn\np\n1\n\n\nw" | fdisk /dev/sda
share|improve this answer
This is more terse than the others, but unfortunately seems to only work with bash. – bk138 Feb 9 at 18:34
It's working with ash/busybox too. – kikeenrique Feb 10 at 8:59
Is another \n needed after the w? – Elgs Qian Chen Jul 17 at 13:48
This works, but is horrible :-) – Ciro Santilli 六四事件 法轮功 包卓轩 Sep 9 at 13:23
@ElgsQianChen: no, because echo automatically terminates the line with a newline (unless -n option is used) – Udo G Nov 9 at 8:14

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 ; echo; echo w) | sudo fdisk

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

share|improve this answer

You can script fdisk.

(echo n; echo p; echo 1; echo 1; echo 200; echo w) | fdisk /dev/sdc

That creates a 200 MB partition on /dev/sdc

share|improve this answer

Check out: Parition with sfdisk. All you need to do is make a file, in a certain format that sfdisk will read, then read it into sfdisk.

share|improve this answer
sfdisk is too hard :< – Felipe Alvarez Jun 17 at 23:37

Piping commands to fdisk works well as explained by other people, but this way a bit more elegant and readable:

fdisk /dev/sdc <<EOF


Piping from a (temporary) file also works:

fdisk /dev/sdc < /tmp/fdisk.cmds
share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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