3

If i have 8 directories under / , with 5 directories on different mountpoints then /

/dev/md/dsk/d0          49G    32G    17G    66%    /
/dev/md/dsk/d65         76G    77M    75G     1%    /u03
/dev/md/dsk/d64        345G    76G   266G    23%    /u02
/dev/md/dsk/d5          76G    77M    75G     1%    /u01
/dev/dsk/emcpower0g    591G   288G   297G    50%    /db
/dev/dsk/emcpower1g    591G   116G   469G    20%    /db2

Doing a ls -l

bash-3.2# pwd
/
bash-3.2# ls -l

drwxr-xr-x   3 root     sys          512 Jun 24  2014 boot
**drwxr-xr-x   5 root     root         512 Sep 16  2014 db
drwxr-xr-x   6 root     root         512 Sep 16  2014 db2
drwxr-xr-x  19 root     sys         5120 Jul 11 22:57 dev
drwxr-xr-x   2 root     sys          512 Jul 11 16:17 devices
drwxr-xr-x   3 root     root         512 Jun 25  2014 u01
drwxr-xr-x   4 root     root         512 Jul 11 17:08 u02
drwxr-xr-x   3 root     root         512 Sep  3  2014 u03

How do i filter/list to show

a) only those files and the other 3 directories (/boot, /dev, /devices..) that is created under / mountpoint

b) the 5 directories (e.g. /db, /db2, /u01..) that is under / but is mounted on different mointpoints

1

On a linux system you can use findmnt:

set ''
for r in /*
do  findmnt "$r"   || 
  ! set '' "$r$@"  &&
    ls "$r"
done

That command will do the ls for the other mounts, and all of the / root-mount filenames are available in $@ when you're ready. So to list those you'd do:

ls "$@"
0

For question a:

{
  stat --printf='%d\n' /
  find / -mount -maxdepth 1 -type d -printf '%D/%p\n'
} | awk -F/ 'NR==1{root=$1}; $1!=root{print "/"$2}'

stat spits out the decimal device number of the root device. Then the find command lists the directories directly under /, together with the decimal device number. The output of both these commands is sent to awk. The awk prints out the directories if the device number does not equal the root device.

Change != to == in the awk program to answer your second question.

  • 1
    what about just adding -xdev to find? – Jeff Schaller Jul 13 '15 at 19:48
  • -xdev does not descend into directories mounted on another device. It will, however, list the directories themselves, even if the contents of those directories is on another device. – joepd Jul 13 '15 at 20:34
  • Ahh, sorry. I misunderstood the original question. – Jeff Schaller Jul 13 '15 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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