How can I resize partitions from the command line? I've heard of GParted, but I don't want to use a GUI program.
parted instead, possibly coupled with your filesystem's resizing command.
parted is the engine underneath the GParted GUI. You can use it in either interactive command mode or directly from the command line.
parted 3.0, the following command does what you are probably expecting, having learned about GParted:
$ sudo parted /dev/sdb resize 1 1 200M
That will resize the first partition on the second hard disk to 200 MiB, and make sure it starts 1 MiB into the disk so as to avoid alignment problems with Advanced Format drives.
v3.2 is where things really got interesting, from a Unix/Linux file system perspective. It replaces the
resize command with
resizepart. The new name is due to the fact that it can only change the partition size; it doesn't even attempt to rearrange its contents first.
In the case of growing an existing filesystem, this is a low-risk operation as long as you're using a filesystem that can be grown on the fly, like ext4 or XFS. For example, if we start with a 200 MiB ext4 partition on
/dev/sdb1, we can double its size:
$ sudo parted /dev/sdb resizepart 1 400M $ sudo resize2fs /dev/sdb1 400M
The same command pair also works for shrinking
ext filesystems, except that you give them in reverse: shrink the actual filesystem, then chop the empty space off the end of the partition.
Some filesystems (e.g. XFS) can be grown only; they can't be shrunk. This is why XFS's equivalent to
resize2fs is called
RAID and LVM systems complicate all of this. They have their own restrictions and capabilities.
A common situation is to have an XFS filesystem on top of a multi-drive LVM-managed disk array, to which you add some drives, then expand the LVM, and finally expand the XFS filesystem into the new space.