5

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?

3
  • What are you trying to do with the information you receive from df? Can you describe the end goal?
    – ewwhite
    Jul 20, 2014 at 20:26
  • Amongst other utilities awk and grep are your friends.
    – user9517
    Jul 20, 2014 at 20:27
  • df | awk '{print $1}'
    – Alex Berry
    Jul 21, 2014 at 15:08

8 Answers 8

9

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

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
3

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
2

Some ideas...

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

df | awk '{print $1}'

df | cut -f1 -d " "

1
  • fdisk -l | awk '/\/dev\/s[a-z]/ {print "Partition" " " $1}'
    – c4f4t0r
    Jul 20, 2014 at 21:53
1

What about :

df | cut -d" " -f1
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
1
  [root@ct01 ~]# ls /dev/sd*[0-9]
  /dev/sda1  /dev/sda2
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 }'
1
  • Should be tail -n+3 and it will list not only partitions but also devices, e.g. sda.
    – grekasius
    Jul 21, 2014 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .