78

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.

1
  • 2
    To whoever will anser: make sure to explain the option -m on reserved space - it's relevant, because it's couterintuitive. Oct 3, 2014 at 13:52

2 Answers 2

88

There are a few options available:

  1. fdisk.
  2. parted (the CLI brother of GParted).
  3. The various mkfs programs, if you already have partitions and wish to format.

fdisk and parted are interactive, and have help commands, so you can always look for help within the program. Both are also scriptable. The mkfs commands are not interactive.


fdisk

fdisk expects a device (such as /dev/sda) as an argument. It has the following commands:

Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the DOS compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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:

  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  check NUMBER                             do a simple check on the file system
  cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER   copy file system to another partition
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkfs NUMBER FS-TYPE                      make a FS-TYPE file system on partition NUMBER
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  mkpartfs PART-TYPE FS-TYPE START END     make a partition with a file system
  resizepart NUMBER END                    resize partition NUMBER
  move NUMBER START END                    move partition NUMBER
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resize NUMBER START END                  resize partition NUMBER and its file system
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted

The commands can be contracted to a unique prefix (e.g., h is short for help).

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:

parted /tmp/part mklabel gpt

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 for mkpartfs, but we can specify the suffix:

parted /tmp/part mkpart primary ext4 3G 5G

And another, now an NTFS partition of 1GB:

parted /tmp/part mkpart primary ntfs 5G 6G

Result:

# parted /tmp/part print
Model:  (file)
Disk /tmp/blah: 10.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      3000MB  5000MB  2000MB               primary
 2      5000MB  6000MB  1000MB               primary  msftdata

Note how it uses SI prefixes, whereas GParted steadfastly uses binary prefixes (while dropping the silly i). I'll label the partitions:

# parted /tmp/part name 1 hello
# parted /tmp/part name 2 world
# parted /tmp/part print
Model:  (file)
Disk /tmp/blah: 10.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name   Flags
 1      3000MB  5000MB  2000MB               hello
 2      5000MB  6000MB  1000MB               world  msftdata

While parted can create partitions of filesystem ntfs just fine, it can't format an existing partition (!) to NTFS:

mkfs partition fs-type
         Make a filesystem fs-type on partition. fs-type can be one 
         of "fat16", "fat32", "ext2", "linux-swap", or "reiserfs".

Indeed, parted will tell you that you should use it for manipulating partitions, not filesystems, which brings me to:


mkfs

mkfs, like fsck, 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 for mkfs, 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 for mkfs is:

# mkfs --help
Usage: mkfs [options] [-t type fs-options] device [size]

Options:
 -t, --type=TYPE  file system type, when undefined ext2 is used
     fs-options   parameters to real file system builder
     device       path to a device
     size         number of blocks on the device
 -V, --verbose    explain what is done
                  defining -V more than once will cause a dry-run
 -V, --version    output version information and exit
                  -V as version must be only option
 -h, --help       display this help and exit

For more information, see mkfs(8).

As you can see, the -t flag lets us pass filesystem-specific flags. For example, NTFS flags:

# mkfs.ntfs --help 
Usage: mkntfs [options] device [number-of-sectors]

Basic options:
    -f, --fast                      Perform a quick format
    -Q, --quick                     Perform a quick format
    -L, --label STRING              Set the volume label
    -C, --enable-compression        Enable compression on the volume
    -I, --no-indexing               Disable indexing on the volume
    -n, --no-action                 Do not write to disk

Advanced options:
    -c, --cluster-size BYTES        Specify the cluster size for the volume
    -s, --sector-size BYTES         Specify the sector size for the device
    -p, --partition-start SECTOR    Specify the partition start sector
    -H, --heads NUM                 Specify the number of heads
    -S, --sectors-per-track NUM     Specify the number of sectors per track
    -z, --mft-zone-multiplier NUM   Set the MFT zone multiplier
    -T, --zero-time                 Fake the time to be 00:00 UTC, Jan 1, 1970
    -F, --force                     Force execution despite errors

Output options:
    -q, --quiet                     Quiet execution
    -v, --verbose                   Verbose execution
        --debug                     Very verbose execution

Help options:
    -V, --version                   Display version
    -l, --license                   Display licensing information
    -h, --help                      Display this help

Developers' email address: [email protected]
News, support and information:  http://tuxera.com

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").

# mkfs -t ntfs -F -Q -L "hello world" /tmp/file
/tmp/file is not a block device.
mkntfs forced anyway.
The sector size was not specified for /tmp/file and it could not be obtained automatically.  It has been set to 512 bytes.
The partition start sector was not specified for /tmp/file and it could not be obtained automatically.  It has been set to 0.
The number of sectors per track was not specified for /tmp/file and it could not be obtained automatically.  It has been set to 0.
The number of heads was not specified for /tmp/file and it could not be obtained automatically.  It has been set to 0.
Cluster size has been automatically set to 4096 bytes.
To boot from a device, Windows needs the 'partition start sector', the 'sectors per track' and the 'number of heads' to be set.
Windows will not be able to boot from this device.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.

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:

# mount -t ntfs-3g /tmp/file /mnt
# touch "/mnt/a file in mnt"
# ls -l /mnt
total 0
-rwxrwxrwx 1 root root 0 Aug 29 06:43 a file in mnt
# umount /mnt
# ls -l /mnt
total 0

(See the weird permissions?)


Notes:

  1. I haven't used sudo anywhere in this answer yet. Since I was operating on files, and files owned by me, I didn't need sudo. parted will warn you about this. For block devices, which are usually always owned by root, you will need sudo (or you'll have to use a root shell via sudo -i or sudo su -).
  2. parted is a GNU program, and like many GNU programs, has extensive documentation in the info format. Install parted-doc (sudo apt-get install parted-doc) and then run info parted. You can also checkout the online user's manual.
  3. GParted is able to format a partition to NTFS as it calls the appropriate mkfs program directly (mkntfs, in this case - mkfs.ntfs is just a link to mkntfs). 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.
  4. I won't go into the merits of GPT vs MBR/MSDOS partition tables, but GPT is likely to be found on new devices with UEFI, especially if you got Windows 8 on them. The state of partitioning tools? discusses what tools are available if you're facing GPT.
  5. LVM, ZFS and btrfs are a whole another game. They all have their accompanying tools, and you should use them instead of parted or fdisk (except perhaps for an initial step of creating partitions for their use).

Note on parted usage:

The syntax of the parted program is:

parted [options] [device [command [options...]...]]

When you run parted without a command, like:

parted /tmp/parted

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:

# parted /tmp/parted
GNU Parted 2.3
Using /tmp/parted
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt

And

# parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /tmp/parted
Using /tmp/parted
(parted) mklabel gpt

And

parted /tmp/parted mklabel gpt

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, running

parted /dev/sda -- mkpart primary ext4 3G -1s

will 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, for 1s not to be interpreted as an invalid option.

8
  • IMHO, gdisk, aka GPT fdisk, is superior to parted Feb 1, 2015 at 6:48
  • 1
    @FireLizzard excellent! Please post an answer detailing how to use it.
    – muru
    Feb 1, 2015 at 6:50
  • while the question is clearly about how to format a disk, why are you even mentioning parted and fdisk? You even mention yourself, that you cannot format a disk with parted.
    – zunder
    Apr 22, 2016 at 12:05
  • @kreide read again: "Information on how to partition the drive via terminal is also wanted."
    – muru
    Apr 22, 2016 at 13:24
  • +1 for a detailed answer, I had a problem with an external hard drive without any partition table in it, so you're answer was very helpfull Jun 27, 2020 at 17:55
15

First you how to actually partition your hard drive with the fdisk utility.

Linux allows only 4 primary partitions. You can have a much larger number of logical partitions by sub-dividing one of the primary partitions.

Only one of the primary partitions can be sub-divided.

fdisk is started by typing as root fdisk device at the command prompt.

Device might be something like /dev/sda or /dev/sdb

The basic fdisk commands you need are:

p print the partition table

n create a new partition

d delete a partition

q quit without saving changes

w write the new partition table and exit

Changes you make to the partition table do not take effect until you issue the write (w) command.

Here is a sample partition table:

Disk /dev/sdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/sdb1   *         1       184    370912+  83  Linux
/dev/sdb2           185       368    370944   83  Linux
/dev/sdb3           369       552    370944   83  Linux
/dev/sdb4           553       621    139104   82  Linux swap

Example:

Start fdisk from the shell prompt:

sudo su
fdisk /dev/sdb 

Which indicates that You are using the second drive on your SATA controller.

Command (m for help): p

Disk /dev/hdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes

That makes for 384Mb per partition. 
Now You get to work.


Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-621, default 1):<RETURN>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-621, default 621): +384M

Next, You set up the partition You want to use for swap:


Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (197-621, default 197):<RETURN>
Using default value 197
Last cylinder or +size or +sizeM or +sizeK (197-621, default 621): +128M

Now the partition table looks like this:

 Device Boot      Start       End    Blocks   Id  System
/dev/sdb1             1       196    395104   83  Linux
/dev/sdb2           197       262    133056   83  Linux

Finally, You make the first partition bootable:

Command (m for help): a
Partition number (1-4): 1

And You make the second partition of type swap:


Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap)      
Command (m for help): p

The end result:

Disk /dev/sdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/sdb1   *         1       196    395104+  83  Linux
/dev/sdb2           197       262    133056   82  Linux swap

Finally, You issue the write command (w) to write the table on the disk.


mkfs utility is used to create filesystem (ext2, ext3, ext4, etc) on your Linux system.

You should specify the device name to mkfs on which the filesystem to be created.

View the Available Filesystem Builder Commands

The filesystem builders (mkfs* commands) are usually searched in directories like /sbin/, /sbin/fs, /sbin/fs.d, /etc/fs and /etc.

If not found, finally it searches the directories found in the PATH variable.

The following list shows the available mkfs* commands in a system.

sudo su
cd /sbin
ls mkfs*

mkfs  mkfs.bfs  mkfs.cramfs  mkfs.ext2  mkfs.ext3  mkfs.ext4  mkfs.ext4dev  
mkfs.minix  mkfs.msdos  mkfs.ntfs  mkfs.vfat

Build a Filesystem on a Specific Device

In order to build the filesystem using mkfs command, the required arguments are device-filename and filesystem-type as shown below.

The following example creates ext4 filesystem on /dev/sdb1 partition.

sudo su
mkfs -t ext4 /dev/sdb1 

mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1120112 inodes, 4476416 blocks
223820 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
137 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

Please note that the default filesystem type for mkfs command is ext2.

If you don’t specify “-t” option, it will create ext2 filesystem.

Also, you can use the method we discussed earlier to identify whether you have ext2 or ext3 or ext4 file system.


Format An NTFS Drive

First, you’re going to need the ability to create NTFS file systems, so install ntfsprogs:

sudo su 
apt-get install ntfs-3g

Second, You blow away the partition and re-create it as NTFS.

sudo su 
umount /dev/sdb1
fdisk /dev/sdb

Options to select:

    ‘d’ to delete the partition
    ‘n’ to create a new partition
    ‘p’ for primary
    ‘1’ for partition number
    ‘Enter’ for first cylinder (default 1)
    ‘Enter’ for last cylinder (default of max size)
    ‘t’ for type
    ‘L’ to list codes, and enter code for HPFS/NTFS. In my case, it’s ‘7’
    ‘w’ to write changes to disk, and exit

umount /dev/sdb1

In the last step, You unmount the partition, because Ubuntu auto-mounted it again for You.

Now, You need to create the file system. There are two ways to go about it: the impatient way (Quick Format), or the better but much longer way (Full Format).

Quick Format

This just allocates the disk space, but doesn’t zero out the drive or check for bad sectors. This means it’ll take a few seconds.

sudo su 
mkfs.ntfs -f /dev/sdb1

Full Format

If you’re much more concerned about data integrity and don’t mind waiting, do a full format.

This may take a few hours to zero out a large drive!

sudo su 
mkfs.ntfs /dev/sdb1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .