Linux: get absolute path of running shell script / executable

Q. In Linux from a running shell script or a C/C++ executable how to get the absolute path of itself?

Ans:

For shell script:
Below line will give you the absolute path of shell script that is running.

ABSOLUTE_DIR_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

For a c/c++ executable:

void
getExecutableAbsolutePath(
    IN std::string &path
    )
{
    char linkname[1024];
    ssize_t r;

    r = readlink("/proc/self/exe", linkname, 1024);
    assert(-1 != r);
    linkname[r] = '\0';
    path = linkname;
    std::size_t found = path.find_last_of("/");
    path = path.substr(0, found+1);
}

 

 

Advertisements

How to list the physical disk on Linux machine?

Q. How to list the physical disks present in a linux machine? List the file names like /dev/sdX of all the physical disk.

Ans:
There are many commands to do that.

A. lsblk:
 $ sudo lsblk -d | grep disk
 fd0 2:0 1 4K 0 disk
 sda 8:0 0 128G 0 disk
 sdb 8:16 0 5G 0 disk
 sdc 8:32 0 50G 0 disk

This will list all the physical disk. It also includes the md / lvm devices you need to filter them out. Change the command to get the physical disk list like below:

$ sudo lsblk -d | grep disk | grep “^sd” | tr -s ‘ ‘ | cut -d ‘ ‘ -f 1
sda
sdb
sdc

B. blkid:

$ sudo blkid
 /dev/sr0: LABEL="VMGUEST" TYPE="iso9660"
 /dev/sda1: UUID="202b983a-02c4-4ee0-9a18-3d3b38f7bc7a" TYPE="swap"
 /dev/sda2: UUID="d7441b33-1aa1-459c-af17-6459b0d18a19" TYPE="ext4"
 /dev/sda3: UUID="611a5df3-16bc-4240-97bd-c8bbdd8ebf2c" TYPE="ext4"
 /dev/sdb1: UUID="6ed027ff-04ff-42a7-afad-08726ada10f9" TYPE="ext4"
 /dev/sdc1: UUID="9a1c60cf-7790-4ed9-9da3-d7c1a9471d78" TYPE="ext4"
 /dev/sdc2: UUID="z32bc1-xzAH-yUvM-xVJp-3hLe-QAj2-Zxp6uN" TYPE="LVM2_member"
 /dev/sdc3: UUID="NWBkAy-Nxtw-OKkq-wdxM-N6db-VzOP-t76PLT" TYPE="LVM2_member"
 /dev/sdc5: UUID="aC1WB9-5p0Y-EsPD-yPBD-m1x0-Q0qW-kuZxXw" TYPE="LVM2_member"
 /dev/sdc6: UUID="bO8SNW-YOWd-6qke-yZIf-5ekT-FbQJ-W2OFnC" TYPE="LVM2_member"

We can modify this command to list physical disk like below:

 $ sudo blkid | grep "^/dev/sd" | cut -d ':' -f 1 | sed s'/[0-9]//g' | sort | uniq
 /dev/sda
 /dev/sdb
 /dev/sdc

C. fdisk

 $ sudo fdisk -l
Disk /dev/sda: 137.4 GB, 137438953472 bytes
255 heads, 63 sectors/track, 16709 cylinders, total 268435456 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003a379
Device Boot Start End Blocks Id System
/dev/sda1 2048 62500863 31249408 82 Linux swap / Solaris
/dev/sda2 * 62500864 64454655 976896 83 Linux
/dev/sda3 64454656 268433407 101989376 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
181 heads, 40 sectors/track, 1448 cylinders, total 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000497c4
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 10485759 5241856 83 Linux
Disk /dev/sdc: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000dc579
Device Boot Start End Blocks Id System
/dev/sdc1 * 2048 12290047 6144000 83 Linux
/dev/sdc2 12290048 32770047 10240000 8e Linux LVM
/dev/sdc3 32770048 53250047 10240000 8e Linux LVM
/dev/sdc4 53250048 104857599 25803776 5 Extended
/dev/sdc5 53254144 73734143 10240000 8e Linux LVM
/dev/sdc6 73736192 94216191 10240000 8e Linux LVM
/dev/sdc7 94218240 104458239 5120000 83 Linux

You can change this command to get the physical disk like below:

 $ sudo fdisk -l | grep "^Disk /dev/sd" | cut -d ' ' -f 2 | sed 's/://'
 /dev/sda
 /dev/sdb
 /dev/sdc