Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

Can you create a partition in a usb disk using fdisk command in a single line.

fdisk command is interactive in nature, But I want to automate partition creation in a single line using fdisk command.

share|improve this question
Do you want to erase everything and create a single partition, or create a new partition with given start / end? – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Sep 11 '15 at 20:21
up vote 4 down vote accepted

Trying to automate fdisk is possible, but it is not easy to maintain. As other answers note, either parted or sfdisk are designed to do what you want and are easier to automate.


To create a partition in one line with parted:

parted -a optimal /dev/usb mkpart primary 0% 4096MB

as seen in this UNIX SE post. Each of the parts is pretty self-explanatory, but just in case here is how mkpart is defined:

mkpart [part-type fs-type name] start end

where things in square brackets are optional, but you probably want primary for your part-type, start at 0% and end at 4096MB or however large your USB stick is.


The input to sfdisk is arguably not much easier to understand than the input to fdisk but it is intended for automation.

# Example Usage: create_sd /dev/sdb


dd if=/dev/zero of=$DRIVE bs=1024 count=1024

SIZE=`fdisk -l $DRIVE | grep Disk | awk '{print $5}'`

echo DISK SIZE - $SIZE bytes

CYLINDERS=`echo $SIZE/255/63/512 | bc`


echo ,9,0x0C,*
echo ,,,-
} | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE

mkfs.vfat -F 32 -n "boot" ${DRIVE}1
mke2fs -j -L "rootfs" ${DRIVE}2

comes from The Linux Daily and despite not fitting the one line request it does show how it could be done.

share|improve this answer

You probably need to use the parted command instead of fdisk.

share|improve this answer

sfdisk also has a non-interactive mode that reads in partition information from stdin. parted is more flexible, though.

share|improve this answer

Use sfdisk instead.

The sfdisk man page is a little confusing, here's some specific examples of how to automate partition setup with sfdisk. One example is you can save the partition info from one drive via sfdisk -l and then dump that directly on to a new drive.

share|improve this answer

Erase everything, and create a single partition:

sudo umount "$dev"
printf "o\nn\np\n1\n\n\nw\n" | sudo fdisk "$dev"
sudo mkfs.ext4 "${dev}1"

See also:

share|improve this answer
this works but using sfdisk or parted will be easier to get working and maintain – chicks Sep 11 '15 at 20:36
@chicks cool, I will have a look at it. Someone should have posted an example :-) – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Sep 11 '15 at 20:40
good point @Ciro, I will take care of that... – chicks Sep 12 '15 at 0:10
@chicks cool, thanks! – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Sep 12 '15 at 7:52

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.