linux disk partition filesystem command - ghdrako/doc_snipets GitHub Wiki

fdisk

command-line utility that’s used to partition disks. It allows us to create and modify partition tables, enabling administrators to organize storage space as required. fdisk can create different types of partitions, including primary, logical, and extended partitions. Additionally, it can be used to change the partition size, delete partitions, and modify partition types.

List partition

fdisk -l 

lsblk

list all the available block devices, including hard drives, USB devices, and CD-ROM drives. This utility is useful in identifying the storage devices attached to the system, and the details of each block device. The output of lsblk provides critical information about the block devices, such as the device name, the size, the filesystem type, and the mount point.

df

df(Disk Free) utility is a command-line tool that displays the disk space used and available in filesystems on Linux systems. This tool is important in monitoring filesystem usage, identifying storage space consumption patterns, and making decisions on how to allocate storage space efficiently.

-h option to display the output in a human-readable format, making it easier to understand the disk space usage. The output shows the total size of the filesystem, the amount of space used, the available space, the percentage of space used, and the mount point.

The df utility provides a detailed report on the filesystem usage, including the filesystem type, the total size, the used space, the available space, and the percentage of the disk space used.

du

determine the amount of space used by specific directories and files. With du, administrators can identify files or directories that are consuming too much space and decide whether to delete, archive, or move them to another filesystem. The du utility also displays the amount of space used by each subdirectory, enabling administrators to pinpoint storage hogs quickly

Finding large files in a directory

du -a | sort -rn | head –n 10  # identify the 10 largest files in the directory

mkfs, mke2fs, fdformat

  • mkfs is a command that’s used to create a filesystem on a storage device. This command can be used to create a variety of filesystems, including ext2, ext3, ext4, XFS, btrfs, and more.

  • mke2fs is a variant of the mkfs command that is specifically designed to create ext2, ext3, and ext4 filesystems.With this command, administrators can specify the size of the filesystem, the block size, and other parameters to optimize the filesystem for specific use cases.

mke2fs -t ext3 /dev/sdb1 # creates an ext3 filesystem on the /dev/sdb1 partition
  • fdformat command is primarily used for low-level formatting of floppy disks, which are legacy storage media. It writes a new disk geometry and sector layout to the floppy disk, preparing it for use. However, this command is not intended for formatting modern storage devices such as pen drives or USB flash drives.

For formatting pen drives or USB flash drives in Linux, mkfs (make filesystem) commands are typically used, such as mkfs.fat for creating a FAT filesystem or mkfs.ext4 for creating an ext4 filesystem, as shown in the previous examples. These commands are specifically designed for formatting different types of storage devices, including pen drives, USB flash drives, and hard drives.

mkswap

  • The mkswap command is used to create a swap area on a Linux system. A swap area is a dedicated space on a hard drive that the system can use as virtual memory when it runs out of physical memory. The mkswap command initializes a disk partition or a file as a swap area and assigns it a unique identifier.
sudo mkswap /dev/sdb1

gdisk

  • The gdisk command is a variant of the fdisk command and is used to partition hard drives on Linux systems. gdisk is primarily designed for GPT, which is a newer partitioning scheme that has replaced the older Master Boot Record (MBR) partitioning scheme on many modern systems. The gdisk command is a powerful tool that allows you to create, modify, and delete partitions on GPT disks.

parted

  • The parted command is a partition editor that allows you to create, delete, resize, and move partitions on a hard drive. Parted supports both MBR and GPT partitioning schemes and can work with multiple filesystem types. It is a powerful tool for managing disk partitions and is commonly used in server environments. Usage
  1. create a new partition on the /dev/sdb disk.
$> parted /dev/sdb
  1. mklabel gpt command to create a new GPT partition table on the disk. This ensures compatibility with modern systems and larger disk sizes.
(parted)mklabel gpt
  1. create a new primary partition that spans the entire disk. The partition is formatted with the ext4 filesystem.
(parted)mkpart primary ext4 0% 100%
  1. verify the partition layout and details. This helps ensure that the partition was created correctly.
(parted) print
  1. Exit (parted) quit

dd

  • dd command is a low-level tool that’s used for copying and converting data between files, disks, and partitions. The dd command is commonly used for creating bootable USB drives, backing up and restoring disk images, and cloning disks. It can also be used to write zeros to a hard drive, which is useful for securely wiping sensitive data

using the dd command to copy the contents of a source file to a USB device represented by /dev/sdb.

dd if=/home/user1/backup.tar.gz of=/dev/sdb bs=4M

bs option is used to specify the block size for data transfer. In this example, bs=4M indicates a block size of 4 megabytes.