adrelanos
adrelanos

Reputation: 1593

How to get the offset of a partition with a bash script?

I can use parted to find out the offset of my image.

sudo parted -s image.img unit B print
Model:  (file)
Disk /home/user/image.img: 107374182400B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start     End            Size           Type     File system  Flags
 1      2097152B  107374182399B  107372085248B  primary  ext4

For example, partition starts at 2097152.

How can I get the 2097152 with a bash script?

I could probably parse the output, but perhaps there is a more suited method?

Upvotes: 4

Views: 5248

Answers (2)

antoine
antoine

Reputation: 1873

In case of multiple partitions in the image and to get only the first partition offset, you can use the following :

sudo parted -s image.img unit B print | sed 's/^ //g' | grep "^1 " | tr -s ' ' | cut -d ' ' -f2
  • sed will remove leading space
  • grep will extract line for partition 1 (change that number if you want another partition)
  • tr will remove extra space between words
  • final cut will extract the second number on the line which is the start offset

Upvotes: 0

iruvar
iruvar

Reputation: 23364

One option, feed the output to

sudo parted -s image.img unit B print | 
awk '/^Number/{p=1;next}; p{gsub(/[^[:digit:]]/, "", $2); print $2}' 

Upvotes: 6

Related Questions