Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

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

8 Answers 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

share|improve this answer

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
share|improve this answer

Some ideas...

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

df | awk '{print $1}'

df | cut -f1 -d " "

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

What about :

df | cut -d" " -f1
share|improve this answer

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
share|improve this answer

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
share|improve this answer
  [root@ct01 ~]# ls /dev/sd*[0-9]
  /dev/sda1  /dev/sda2
share|improve this answer

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 }'
share|improve this answer
    
Should be tail -n+3 and it will list not only partitions but also devices, e.g. sda. –  girek Jul 21 at 14:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.