4

df on linux lists the following stats on partitions.

-bash-4.1# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda3            1918217320 1473337340 347440136  81% /
tmpfs                 32970328         0  32970328   0% /dev/shm
/dev/sda1               482214    148534    308781  33% /boot
/dev/sdd1            1922860884 1638599284 186585876  90% /disk5
/dev/sdc1            1922858352 1474925416 350257336  81% /disk2
/dev/sdb1            1922858352 1028783752 796399000  57% /disk4

If I want to get a list of the partition names, for example: "/dev/sda3 /dev/sda1 /dev/sdc1..." how do I do it in bash?

user12145
  • 1,075
  • 6
  • 26
  • 47

8 Answers8

8

To list all partitions defined for a device as root run:

lsblk

OR

fdisk -l

OR

cat /proc/partitions

And also as mentioned by @Giedrius Rekasius

fdisk -l /dev/sda | grep '^/dev' | cut -d' ' -f1

TBI Infotech
  • 1,536
  • 9
  • 15
5

df will display only mounted partitions. If that's what you want then to extract the device nodes from df output you grep for the lines starting with "/dev" and cut the first column out of the remaining output:

df | grep '^/dev' | cut -d' ' -f1 

or to list them on a single line separated by spaces:

df | grep '^/dev' | cut -d' ' -f1 | tr '\n' ' '

If you want to get a list of partitions which are not necessarily mounted then as root you can run fdisk -l and optionally specify device(s) to scan for partitions:

fdisk -l [device...]

If you don't specify any device then fdisk will use devices mentioned in /proc/partitions if that file exists.

Fdisk will output information in a similar format as df so to extract device nodes you can do the same thing as described for df:

fdisk -l | grep '^/dev' | cut -d' ' -f1
grekasius
  • 2,046
  • 11
  • 15
2

Some ideas...

awk '{print $1}' /proc/mounts

df | awk '{print $1}'

df | cut -f1 -d " "

ewwhite
  • 194,921
  • 91
  • 434
  • 799
2

blkid displays just real storages attached to computer with or without mounted.

# sudo blkid | awk '{print substr($1, 0, length($1) - 1)}'

/dev/sda1
/dev/sda2
/dev/sdb1
/dev/sdb2
/dev/sr1
Sencer H.
  • 532
  • 1
  • 8
  • 17
1

What about :

df | cut -d" " -f1
krisFR
  • 12,830
  • 3
  • 31
  • 40
1

Another option:

df --type ext2 --type ext3 --type ext4 | tail -n +2 | awk '{ print $1 }' | tr '\n' ' '
  • --type <filesystem> allows you to specify which filesystems you are interested in
  • tail -n +2 starts printing from the second row of output (so that the heading line isn't printed)
  • awk '{ print $1 }' will print the leftmost column which contains the partition information
  • tr '\n' ' ' will translate newlines to spaces, so that the information is all on one line
mvermaes
  • 671
  • 5
  • 7
0
  [root@ct01 ~]# ls /dev/sd*[0-9]
  /dev/sda1  /dev/sda2
Kindule
  • 156
  • 8
0

This command will list all of the partitions on the system in a form that's easy to iterate on:

tail -n +3 /proc/partitions | awk '{ print $4 }'
Boscoe
  • 564
  • 3
  • 6