I want to know how to format a storage drive from the terminal. Helpful things to provide in the answer would be often used options for commands and base knowledge that one can use to extrapolate future uses. Specifically I would like to know how to format in the different file systems such as NTFS, FAT32, EXT4, etc. Information on how to partition the drive via terminal is also wanted.
I am trying to format a high capacity external hard drive (EHDD) into NTFS – from the terminal.
I know I can use gparted for this as well as other GUI programs, but I still want to now how to do it from the terminal.
Best Answer
There are a few options available:
fdisk
(older, doesn't support GPT4).parted
(the CLI brother of GParted).mkfs
programs, if you already have partitions and wish to format.fdisk
andparted
are interactive, and have help commands, so you can always look for help within the program. Both are also scriptable. Themkfs
commands are not interactive.fdisk
fdisk
expects a device (such as/dev/sda
) as an argument. It has the following commands:I don't use
fdisk
that much. I'll just focus on:parted
parted
doesn't need an argument (it tries to "guess"), but you should always specify the disk. Given the choice,parted
is the program you should prefer. It has the following commands:The commands can be contracted to a unique prefix (e.g.,
h
is short forhelp
).I'm going to use a temporary file (
/tmp/part
) I created to show you the commands, so the sizes will be somewhat small. You should replace that with the device you need (/dev/sda
, for example).First, if your disk doesn't have a partition table, we must create one:
or
mklabel msdos
, if you want the old-school 4-primary-partition thing (called MBR or MSDOS partition table). Then we make, say, an ext4 partition starting starting at 3GB (i.e., leaving the initial 3G free) and of size 2GB (i.e., ending at 5GB).parted
expects locations in MB formkpartfs
, but we can specify the suffix:And another, now an NTFS partition of 1GB:
Result:
Note how it uses SI prefixes, whereas GParted steadfastly uses binary prefixes (while dropping the silly
i
). I'll label the partitions:While
parted
can create partitions of filesystemntfs
just fine, it can't format an existing partition (!) to NTFS:Indeed, parted will tell you that you should use it for manipulating partitions, not filesystems, which brings me to:
mkfs
mkfs
, likefsck
, is essentially a frontend to various filesystem-specific commands. On my system for example,mkfs.bfs
,mkfs.cramfs
,mkfs.ext2
,mkfs.ext3
,mkfs.ext4
,mkfs.ext4dev
,mkfs.fat
,mkfs.minix
,mkfs.msdos
,mkfs.ntfs
,mkfs.vfat
are available.Now, unfortunately, while
parted
operates just fine on a file, like the one I used above,mkfs
can't go hunting for partitions in such files. In fact, it expects block devices, so if I'm going to use a new file/tmp/file
formkfs
, I have to force it do so. You'll use the block device corresponding to the partition you want to format, such as/dev/sda2
. The general syntax formkfs
is:As you can see, the
-t
flag lets us pass filesystem-specific flags. For example, NTFS flags:So let's make an NTFS partition, with quick formatting (
-Q
), forcing it to operate on a non-block-device file (-F
), and setting a label (-L "hello world"
).Clearly it didn't enjoy working on a file. :) Don't worry, it should automatically detect most values when working on an actual disk. Even this "file" works fine as a filesystem:
(See the weird permissions?)
Notes:
sudo
anywhere in this answer yet. Since I was operating on files, and files owned by me, I didn't needsudo
.parted
will warn you about this. For block devices, which are usually always owned byroot
, you will needsudo
(or you'll have to use a root shell viasudo -i
orsudo su -
).parted
is a GNU program, and like many GNU programs, has extensive documentation in theinfo
format. Installparted-doc
(sudo apt-get install parted-doc
) and then runinfo parted
. You can also checkout the online user's manual.mkfs
program directly (mkntfs
, in this case -mkfs.ntfs
is just a link tomkntfs
). It also sets a number of parameters. In fact, for most operations, you can examine the details of the GParted messages to see which commands were run.parted
orfdisk
(except perhaps for an initial step of creating partitions for their use).Note on
parted
usage:The syntax of the
parted
program is:When you run
parted
without a command, like:You'll be presented a simple shell, where you can run the above commands. However, these commands can also be run directly using the
parted
program. So these three are equivalent:And
And
Note also that, when creating partitions with
parted
, a useful indicator of end of partitions is-1s
(this is "1" between the hyphen and the "s"). This is useful if you want your partition to span from a specified start to the rest of the disk. To be more specific, runningwill create a partition of
/dev/sda
that starts at 3G and ends at the last sector of the/dev/sda
disk (i.e. it spans from 3G to the whole remainder of the disk). Note that the--
is necessary, for1s
not to be interpreted as an invalid option.