How to get disk name that contains a specific partition

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












If I know that a partition is for example /dev/sda1 how can I get the disk name (/dev/sda in this case) that contains the partition ?



  • The output should be only a path to disk (like /dev/sda).

  • It shouldn't require string manipulation, because I need it to work for different disk types.









share|improve this question



























    up vote
    3
    down vote

    favorite
    1












    If I know that a partition is for example /dev/sda1 how can I get the disk name (/dev/sda in this case) that contains the partition ?



    • The output should be only a path to disk (like /dev/sda).

    • It shouldn't require string manipulation, because I need it to work for different disk types.









    share|improve this question

























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      If I know that a partition is for example /dev/sda1 how can I get the disk name (/dev/sda in this case) that contains the partition ?



      • The output should be only a path to disk (like /dev/sda).

      • It shouldn't require string manipulation, because I need it to work for different disk types.









      share|improve this question















      If I know that a partition is for example /dev/sda1 how can I get the disk name (/dev/sda in this case) that contains the partition ?



      • The output should be only a path to disk (like /dev/sda).

      • It shouldn't require string manipulation, because I need it to work for different disk types.






      linux partition disk block-device






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 14 '15 at 23:32









      Thomas Dickey

      51.1k593162




      51.1k593162










      asked Aug 30 '15 at 15:20









      Snoop05

      183




      183




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          You can observe in /sys the block device for a given partition name. For example, /dev/sda1:



          $ ls -l /sys/class/block/sda1
          lrwxrwxrwx 1 root root /sys/class/block/sda1 ->
          ../../devices/pci0000:00/.../ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1


          A script to take arg /dev/sda1 and print /dev/sda is:



          part=$1
          part=$part#/dev/
          disk=$(readlink /sys/class/block/$part)
          disk=$disk%/*
          disk=/dev/$disk##*/
          echo $disk


          I don't have lvm etc to try out, but there is probably some similar path.




          There is also lsblk:



          $ lsblk -as /dev/sde1
          NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
          sde1 8:65 1 7.4G 0 part
          `-sde 8:64 1 7.4G 0 disk


          and as @don_crissti said you can get the parent directly with:



          lsblk -no pkname /dev/sda1





          share|improve this answer


















          • 3




            No need for a script to do that, to print the just the parent device run: lsblk -no pkname /dev/sda1
            – don_crissti
            Aug 30 '15 at 16:39











          • @don_crissti Wow, even simpler. thanks.
            – meuh
            Aug 30 '15 at 16:42










          • @don_crissti Thanks! I was reading help and man pages of every single unix utility that can manage partitions or list them, i cant belive that i didnt saw "PKNAME internal parent kernel device name" in lsblk.
            – Snoop05
            Aug 30 '15 at 16:50










          • pkname is not in my man page, but in the output of lsblk -h, so it's easy to miss!
            – meuh
            Aug 30 '15 at 17:03










          • meuh, it is not present in any man page version, the man page clearly says (twice): Use lsblk --help to get a list of all available/supported columns.
            – don_crissti
            Aug 30 '15 at 17:19

















          up vote
          0
          down vote













          Perhaps not beautiful:



          for d in `fdisk -l 2>/dev/null | grep "^Disk " | cut -d":" -f1 | cut -f2`
          do
          if [ `fdisk -l $d 2>/dev/null | grep -c "/dev/sda1"` -gt 0 ]
          then
          echo On disk $d
          fi
          done


          It works only for 'real' disks not for LVM.






          share|improve this answer



























            up vote
            0
            down vote













            It's work only with UTF-8 locale. lvm, zfs tested ok.



            parent_tree_disk() awk '/^[A-Za-z]/d0=$1; print d0;/^[└─├─]/d1=$1; print d0, d1;/^ [└─├─]/d2=$1; print d0, d1, d2' 

            alias pd='parent_tree_disk'


            shell command:# pd
            NAME

            sda

            sda sda1

            sda sda2

            sda sda2 cl-root

            sda sda2 cl-swap



            shell command:# pd | awk '/sda2/print $1'

            sda



            And you can use other filter on pd list output, like sort, uniq...





            share








            New contributor




            Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f226420%2fhow-to-get-disk-name-that-contains-a-specific-partition%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote



              accepted










              You can observe in /sys the block device for a given partition name. For example, /dev/sda1:



              $ ls -l /sys/class/block/sda1
              lrwxrwxrwx 1 root root /sys/class/block/sda1 ->
              ../../devices/pci0000:00/.../ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1


              A script to take arg /dev/sda1 and print /dev/sda is:



              part=$1
              part=$part#/dev/
              disk=$(readlink /sys/class/block/$part)
              disk=$disk%/*
              disk=/dev/$disk##*/
              echo $disk


              I don't have lvm etc to try out, but there is probably some similar path.




              There is also lsblk:



              $ lsblk -as /dev/sde1
              NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
              sde1 8:65 1 7.4G 0 part
              `-sde 8:64 1 7.4G 0 disk


              and as @don_crissti said you can get the parent directly with:



              lsblk -no pkname /dev/sda1





              share|improve this answer


















              • 3




                No need for a script to do that, to print the just the parent device run: lsblk -no pkname /dev/sda1
                – don_crissti
                Aug 30 '15 at 16:39











              • @don_crissti Wow, even simpler. thanks.
                – meuh
                Aug 30 '15 at 16:42










              • @don_crissti Thanks! I was reading help and man pages of every single unix utility that can manage partitions or list them, i cant belive that i didnt saw "PKNAME internal parent kernel device name" in lsblk.
                – Snoop05
                Aug 30 '15 at 16:50










              • pkname is not in my man page, but in the output of lsblk -h, so it's easy to miss!
                – meuh
                Aug 30 '15 at 17:03










              • meuh, it is not present in any man page version, the man page clearly says (twice): Use lsblk --help to get a list of all available/supported columns.
                – don_crissti
                Aug 30 '15 at 17:19














              up vote
              5
              down vote



              accepted










              You can observe in /sys the block device for a given partition name. For example, /dev/sda1:



              $ ls -l /sys/class/block/sda1
              lrwxrwxrwx 1 root root /sys/class/block/sda1 ->
              ../../devices/pci0000:00/.../ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1


              A script to take arg /dev/sda1 and print /dev/sda is:



              part=$1
              part=$part#/dev/
              disk=$(readlink /sys/class/block/$part)
              disk=$disk%/*
              disk=/dev/$disk##*/
              echo $disk


              I don't have lvm etc to try out, but there is probably some similar path.




              There is also lsblk:



              $ lsblk -as /dev/sde1
              NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
              sde1 8:65 1 7.4G 0 part
              `-sde 8:64 1 7.4G 0 disk


              and as @don_crissti said you can get the parent directly with:



              lsblk -no pkname /dev/sda1





              share|improve this answer


















              • 3




                No need for a script to do that, to print the just the parent device run: lsblk -no pkname /dev/sda1
                – don_crissti
                Aug 30 '15 at 16:39











              • @don_crissti Wow, even simpler. thanks.
                – meuh
                Aug 30 '15 at 16:42










              • @don_crissti Thanks! I was reading help and man pages of every single unix utility that can manage partitions or list them, i cant belive that i didnt saw "PKNAME internal parent kernel device name" in lsblk.
                – Snoop05
                Aug 30 '15 at 16:50










              • pkname is not in my man page, but in the output of lsblk -h, so it's easy to miss!
                – meuh
                Aug 30 '15 at 17:03










              • meuh, it is not present in any man page version, the man page clearly says (twice): Use lsblk --help to get a list of all available/supported columns.
                – don_crissti
                Aug 30 '15 at 17:19












              up vote
              5
              down vote



              accepted







              up vote
              5
              down vote



              accepted






              You can observe in /sys the block device for a given partition name. For example, /dev/sda1:



              $ ls -l /sys/class/block/sda1
              lrwxrwxrwx 1 root root /sys/class/block/sda1 ->
              ../../devices/pci0000:00/.../ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1


              A script to take arg /dev/sda1 and print /dev/sda is:



              part=$1
              part=$part#/dev/
              disk=$(readlink /sys/class/block/$part)
              disk=$disk%/*
              disk=/dev/$disk##*/
              echo $disk


              I don't have lvm etc to try out, but there is probably some similar path.




              There is also lsblk:



              $ lsblk -as /dev/sde1
              NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
              sde1 8:65 1 7.4G 0 part
              `-sde 8:64 1 7.4G 0 disk


              and as @don_crissti said you can get the parent directly with:



              lsblk -no pkname /dev/sda1





              share|improve this answer














              You can observe in /sys the block device for a given partition name. For example, /dev/sda1:



              $ ls -l /sys/class/block/sda1
              lrwxrwxrwx 1 root root /sys/class/block/sda1 ->
              ../../devices/pci0000:00/.../ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1


              A script to take arg /dev/sda1 and print /dev/sda is:



              part=$1
              part=$part#/dev/
              disk=$(readlink /sys/class/block/$part)
              disk=$disk%/*
              disk=/dev/$disk##*/
              echo $disk


              I don't have lvm etc to try out, but there is probably some similar path.




              There is also lsblk:



              $ lsblk -as /dev/sde1
              NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
              sde1 8:65 1 7.4G 0 part
              `-sde 8:64 1 7.4G 0 disk


              and as @don_crissti said you can get the parent directly with:



              lsblk -no pkname /dev/sda1






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 27 at 10:44









              Jeff Schaller

              34.4k951114




              34.4k951114










              answered Aug 30 '15 at 16:18









              meuh

              30.6k11754




              30.6k11754







              • 3




                No need for a script to do that, to print the just the parent device run: lsblk -no pkname /dev/sda1
                – don_crissti
                Aug 30 '15 at 16:39











              • @don_crissti Wow, even simpler. thanks.
                – meuh
                Aug 30 '15 at 16:42










              • @don_crissti Thanks! I was reading help and man pages of every single unix utility that can manage partitions or list them, i cant belive that i didnt saw "PKNAME internal parent kernel device name" in lsblk.
                – Snoop05
                Aug 30 '15 at 16:50










              • pkname is not in my man page, but in the output of lsblk -h, so it's easy to miss!
                – meuh
                Aug 30 '15 at 17:03










              • meuh, it is not present in any man page version, the man page clearly says (twice): Use lsblk --help to get a list of all available/supported columns.
                – don_crissti
                Aug 30 '15 at 17:19












              • 3




                No need for a script to do that, to print the just the parent device run: lsblk -no pkname /dev/sda1
                – don_crissti
                Aug 30 '15 at 16:39











              • @don_crissti Wow, even simpler. thanks.
                – meuh
                Aug 30 '15 at 16:42










              • @don_crissti Thanks! I was reading help and man pages of every single unix utility that can manage partitions or list them, i cant belive that i didnt saw "PKNAME internal parent kernel device name" in lsblk.
                – Snoop05
                Aug 30 '15 at 16:50










              • pkname is not in my man page, but in the output of lsblk -h, so it's easy to miss!
                – meuh
                Aug 30 '15 at 17:03










              • meuh, it is not present in any man page version, the man page clearly says (twice): Use lsblk --help to get a list of all available/supported columns.
                – don_crissti
                Aug 30 '15 at 17:19







              3




              3




              No need for a script to do that, to print the just the parent device run: lsblk -no pkname /dev/sda1
              – don_crissti
              Aug 30 '15 at 16:39





              No need for a script to do that, to print the just the parent device run: lsblk -no pkname /dev/sda1
              – don_crissti
              Aug 30 '15 at 16:39













              @don_crissti Wow, even simpler. thanks.
              – meuh
              Aug 30 '15 at 16:42




              @don_crissti Wow, even simpler. thanks.
              – meuh
              Aug 30 '15 at 16:42












              @don_crissti Thanks! I was reading help and man pages of every single unix utility that can manage partitions or list them, i cant belive that i didnt saw "PKNAME internal parent kernel device name" in lsblk.
              – Snoop05
              Aug 30 '15 at 16:50




              @don_crissti Thanks! I was reading help and man pages of every single unix utility that can manage partitions or list them, i cant belive that i didnt saw "PKNAME internal parent kernel device name" in lsblk.
              – Snoop05
              Aug 30 '15 at 16:50












              pkname is not in my man page, but in the output of lsblk -h, so it's easy to miss!
              – meuh
              Aug 30 '15 at 17:03




              pkname is not in my man page, but in the output of lsblk -h, so it's easy to miss!
              – meuh
              Aug 30 '15 at 17:03












              meuh, it is not present in any man page version, the man page clearly says (twice): Use lsblk --help to get a list of all available/supported columns.
              – don_crissti
              Aug 30 '15 at 17:19




              meuh, it is not present in any man page version, the man page clearly says (twice): Use lsblk --help to get a list of all available/supported columns.
              – don_crissti
              Aug 30 '15 at 17:19












              up vote
              0
              down vote













              Perhaps not beautiful:



              for d in `fdisk -l 2>/dev/null | grep "^Disk " | cut -d":" -f1 | cut -f2`
              do
              if [ `fdisk -l $d 2>/dev/null | grep -c "/dev/sda1"` -gt 0 ]
              then
              echo On disk $d
              fi
              done


              It works only for 'real' disks not for LVM.






              share|improve this answer
























                up vote
                0
                down vote













                Perhaps not beautiful:



                for d in `fdisk -l 2>/dev/null | grep "^Disk " | cut -d":" -f1 | cut -f2`
                do
                if [ `fdisk -l $d 2>/dev/null | grep -c "/dev/sda1"` -gt 0 ]
                then
                echo On disk $d
                fi
                done


                It works only for 'real' disks not for LVM.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Perhaps not beautiful:



                  for d in `fdisk -l 2>/dev/null | grep "^Disk " | cut -d":" -f1 | cut -f2`
                  do
                  if [ `fdisk -l $d 2>/dev/null | grep -c "/dev/sda1"` -gt 0 ]
                  then
                  echo On disk $d
                  fi
                  done


                  It works only for 'real' disks not for LVM.






                  share|improve this answer












                  Perhaps not beautiful:



                  for d in `fdisk -l 2>/dev/null | grep "^Disk " | cut -d":" -f1 | cut -f2`
                  do
                  if [ `fdisk -l $d 2>/dev/null | grep -c "/dev/sda1"` -gt 0 ]
                  then
                  echo On disk $d
                  fi
                  done


                  It works only for 'real' disks not for LVM.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 30 '15 at 16:27









                  Marco

                  768616




                  768616




















                      up vote
                      0
                      down vote













                      It's work only with UTF-8 locale. lvm, zfs tested ok.



                      parent_tree_disk() awk '/^[A-Za-z]/d0=$1; print d0;/^[└─├─]/d1=$1; print d0, d1;/^ [└─├─]/d2=$1; print d0, d1, d2' 

                      alias pd='parent_tree_disk'


                      shell command:# pd
                      NAME

                      sda

                      sda sda1

                      sda sda2

                      sda sda2 cl-root

                      sda sda2 cl-swap



                      shell command:# pd | awk '/sda2/print $1'

                      sda



                      And you can use other filter on pd list output, like sort, uniq...





                      share








                      New contributor




                      Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                        up vote
                        0
                        down vote













                        It's work only with UTF-8 locale. lvm, zfs tested ok.



                        parent_tree_disk() awk '/^[A-Za-z]/d0=$1; print d0;/^[└─├─]/d1=$1; print d0, d1;/^ [└─├─]/d2=$1; print d0, d1, d2' 

                        alias pd='parent_tree_disk'


                        shell command:# pd
                        NAME

                        sda

                        sda sda1

                        sda sda2

                        sda sda2 cl-root

                        sda sda2 cl-swap



                        shell command:# pd | awk '/sda2/print $1'

                        sda



                        And you can use other filter on pd list output, like sort, uniq...





                        share








                        New contributor




                        Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.



















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          It's work only with UTF-8 locale. lvm, zfs tested ok.



                          parent_tree_disk() awk '/^[A-Za-z]/d0=$1; print d0;/^[└─├─]/d1=$1; print d0, d1;/^ [└─├─]/d2=$1; print d0, d1, d2' 

                          alias pd='parent_tree_disk'


                          shell command:# pd
                          NAME

                          sda

                          sda sda1

                          sda sda2

                          sda sda2 cl-root

                          sda sda2 cl-swap



                          shell command:# pd | awk '/sda2/print $1'

                          sda



                          And you can use other filter on pd list output, like sort, uniq...





                          share








                          New contributor




                          Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          It's work only with UTF-8 locale. lvm, zfs tested ok.



                          parent_tree_disk() awk '/^[A-Za-z]/d0=$1; print d0;/^[└─├─]/d1=$1; print d0, d1;/^ [└─├─]/d2=$1; print d0, d1, d2' 

                          alias pd='parent_tree_disk'


                          shell command:# pd
                          NAME

                          sda

                          sda sda1

                          sda sda2

                          sda sda2 cl-root

                          sda sda2 cl-swap



                          shell command:# pd | awk '/sda2/print $1'

                          sda



                          And you can use other filter on pd list output, like sort, uniq...






                          share








                          New contributor




                          Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.








                          share


                          share






                          New contributor




                          Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 2 mins ago









                          Anton Riab

                          11




                          11




                          New contributor




                          Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Anton Riab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f226420%2fhow-to-get-disk-name-that-contains-a-specific-partition%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?