0

I need to find the best approach that search the unused disk in my linux OS

for example from the following ouput we can see that sde is not mounted and seems to be free disk ( we need free disk in order to create on him FS and then mount to some folder )

please advice what is the best approach to find the free disk ? with command line or command line with awk / sed / perl etc , in order to capture the unused disk

sda is for the OS

lsblk | grep disk | grep -v fd0 
sda                8:0    0   150G  0 disk
sdb                8:16   0    20G  0 disk /jeded
sdc                8:32   0    20G  0 disk /var/mmn
sdd                8:48   0    20G  0 disk /var/nrdd
sde                8:64   0    20G  0 disk

expected output should be sde

  • there are some other command to view the disk as sfdisk -s , or fdisk -l , but what we want to find is which disk is a free disk ( without FS / mounted )
9
  • 1
    if you can use presence of / to indicate not-free disk, try lsblk | awk '/disk/ && !/fd0|^sda |\//{print $1}' Commented Jul 25, 2017 at 15:31
  • lsblk | awk '/disk/ && !/fd0|^sda |\//{print $1}' this command print --> sde Commented Jul 25, 2017 at 15:33
  • but we need to check also if this disk is mount according to the last field Commented Jul 25, 2017 at 15:34
  • from the example - expected output should be sde Commented Jul 25, 2017 at 15:37
  • 1
    lsblk -f will list the file systems output as well. You can then filter it to display only the ones that doesn't have specific FS type. Commented Jul 25, 2017 at 19:01

2 Answers 2

1

On Ubuntu 18.04, one option could be to use the command

lsblk | grep -v '^loop'

The output in my case is

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0   1.8T  0 disk
└─sda1        8:1    0   1.8T  0 part /raid
nvme1n1     259:0    0   477G  0 disk
nvme0n1     259:1    0   477G  0 disk
├─nvme0n1p1 259:2    0   800M  0 part /boot/efi
├─nvme0n1p2 259:3    0     5G  0 part
└─nvme0n1p3 259:4    0 471.2G  0 part /

The output shows that disk nvme1n1 is not in use and not mounted anywhere.




Another command to identify the disk with no partition

fdisk -l | grep -v 'Disk /dev/loop' | grep -v '^Units' | grep -v '^Sector' | grep -v 'I/O'
0

Try this:

lsblk  --noheadings --raw | awk '$1~/s.*[[:digit:]]/ && $7==""'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.