# Extracting the Start Sector of a Disk with fdisk

fdisk is a wonderful little utility for managing partitions. I recently had to script a series of fdisk commands for resizing a partition and needed to extract the start sector from the existing disk to do so. I ended up using this combination of grep and awk to do the job.

 1  start_sector=$(sudo fdisk -l | grep ^/dev/sda1 | awk -F" " '{ print$3 }') 

This line executes fdisk with the -l option to list the current disks. Then runs grep to find the current boot disk. Finally, awk retrieves the third token of the string where the token seperator is empty space.

Having the start sector I was able to script the fdisk sequence by piping the necessary sequence of resize commands into fdisk.

 1 2 3 4 5 6 7 8   cat <