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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 9 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/\s*\([\+0-9a-zA-Z]*\).*/\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 '15 at 13:55
Note you have to have a leading tab on each line. – Stu Dec 4 '15 at 19:06
Would you mind placing a big warning that a tab is required on each command character line as @Stu mentioned? I had to debug for a while just because copy-and-paste ignores tabs and replaces them with spaces, thus regex does not match with command lines. – ceremcem Mar 27 at 1:30
Instead of a warning about tabs, I've modified the sed to discard leading whitespace (not just tabs) and not expect a tab after the fdisk commands. This should cut and paste better. @ceremcem - good feedback and sorry to caused you any debug time. – user2070305 Apr 10 at 15:15
Works great, thank you. – ceremcem Apr 13 at 1:04

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 '15 at 18:34
It's working with ash/busybox too. – kikeenrique Feb 10 '15 at 8:59
Is another \n needed after the w? – Elgs Qian Chen Jul 17 '15 at 13:48
This works, but is horrible :-) – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Sep 9 '15 at 13:23
@ElgsQianChen: no, because echo automatically terminates the line with a newline (unless -n option is used) – Udo G Nov 9 '15 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 '15 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
There are half a dozen answers that prove otherwise. – spuder Dec 9 '15 at 20:08

printf 'o\nn\np\n1\n\n\nw' | fdisk /dev/sda

share|improve this answer
A common mistake new users make is to answer without details of how to actually resolves the issue. Answers should be detailed and include references, as needed. Please take a few minutes to edit your answer to include details of why your answer is valid. If you need some help, read How do I write a good answer?. Also, please realize you have posted an answer to a very old questions which has an accepted answer. Although there is noting wrong with doing so, you may not get a response from the OP. – CharlieRB Mar 23 at 19:49

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.