The following, very non-robust shell code will give the mount point of $path
:
以下非常非健壮的shell代码将给出$ path的挂载点:
(for i in $(df|cut -c 63-99); do case $path in $i*) echo $i;; esac; done) | tail -n 1
Is there a better way to do this in shell?
在shell中有更好的方法吗?
Postscript
后记
This script is really awful, but has the redeeming quality that it Works On My Systems. Note that several mount points may be prefixes of $path
.
这个脚本非常糟糕,但具有可在我的系统上运行的兑换质量。请注意,几个挂载点可能是$ path的前缀。
Examples On a Linux system:
示例在Linux系统上:
cas@txtproof:~$ path=/sys/block/hda1 cas@txtproof:~$ for i in $(df -a|cut -c 57-99); do case $path in $i*) echo $i;; esac; done| tail -1 /sys
On a Mac OSX system
在Mac OSX系统上
cas local$ path=/dev/fd/0 cas local$ for i in $(df -a|cut -c 63-99); do case $path in $i*) echo $i;; esac; done| tail -1 /dev
Note the need to vary cut's parameters, because of the way df's output differs; using awk solves this, but even awk is non-portable, given the range of result formatting various implementations of df return.
注意需要改变切割参数,因为df的输出方式不同;使用awk解决了这个问题,但即使是awk也是不可移植的,给定了结果格式化df返回的各种实现。
Answer It looks like munging tabular output is the only way within the shell, but
回答看起来像是表格输出是shell中唯一的方法,但是
df -P "$path" | tail -1 | awk '{ print $NF}'
based on ghostdog74's answer, is a big improvement on what I had. Note two new issues: firstly, df $path
insists that $path
names an existing file, the script I had above doesn't care; secondly, there are no worries about dereferencing symlinks. This doesn't work if you have mount points with spaces in them, which occurs if one has removable media with spaces in their volume names.
基于ghostdog74的答案,对我所拥有的是一个很大的改进。注意两个新问题:首先,df $ path坚持$ path命名一个现有文件,我上面的脚本并不关心;其次,不用担心解除引用符号链接的问题。如果您的挂载点中包含空格,则不起作用,如果其中包含可移动媒体,其卷名称中包含空格,则会出现这种情况。
It's not difficult to write Python code to do the job properly.
编写Python代码来正确完成工作并不困难。
11 个解决方案
#1
17
df
takes the path as parameter, so something like this should be fairly robust;
df将路径作为参数,所以这样的东西应该相当健壮;
df "$path" | tail -1 | awk '{ print $6 }'
#2
13
In theory stat
will tell you the device the file is on, and there should be some way of mapping the device to a mount point.
理论上,stat会告诉你文件所在的设备,并且应该有一些方法将设备映射到挂载点。
For example, on linux, this should work:
例如,在linux上,这应该工作:
stat -c '%m' $path
#3
4
Always been a fan of using formatting options of a program, as it can be more robust than manipulating output (eg if the mount point has spaces). GNU df
allows the following:
一直是使用程序格式化选项的粉丝,因为它比操作输出更强大(例如,如果挂载点有空格)。 GNU df允许以下内容:
df --output=target "$path" | tail -1
Unfortunately there is no option I can see to prevent the printing of a header, so the tail is still required.
不幸的是,我无法看到阻止打印标题的选项,因此仍需要尾部。
#4
1
i don't know what your desired output is, therefore this is a guess
我不知道你想要的输出是什么,因此这是一个猜测
#!/bin/bash
path=/home
df | awk -v path="$path" 'NR>1 && $NF~path{
print $NF
}'
Using cut with -c is not really reliable, since the output of df will be different , say a 5% can change to 10% and you will miss some characters. Since the mount point is always at the back, you can use fields and field delimiters. In the above, $NF is the last column which is the mount point.
使用带-c的cut是不可靠的,因为df的输出会有所不同,比如5%可以改为10%而你会错过一些字符。由于挂载点始终位于后面,因此您可以使用字段和字段分隔符。在上面,$ NF是最后一列,也就是挂载点。
#5
1
I would take the source code to df and find out what it does besides calling stat
as Douglas Leeder suggests.
我会把源代码发送到df并找出它除了调用stat之外的作用,正如Douglas Leeder建议的那样。
Line-by-line parsing of the df
output will cause problems as those lines often look like
df输出的逐行解析将导致问题,因为这些线通常看起来像
/dev/mapper/VOLGROUP00-logical--volume
1234567 1000000 200000 90% /path/to/mountpoint
With the added complexity of parsing those kinds of lines as well, probably calling stat
and finding the mountpoint is less complex.
由于增加了解析这些行的复杂性,可能调用stat并找到mountpoint也不那么复杂。
#6
0
mount | grep "^$path" | awk '{print $3}'
#7
0
I missed this when I looked over prior questions: Python: Get Mount Point on Windows or Linux, which says that os.path.ismount(path)
tells if path is a mount point.
当我查看之前的问题时,我错过了这个:Python:在Windows或Linux上获取Mount Point,它表示os.path.ismount(path)告诉路径是否是挂载点。
My preference is for a shell solution, but this looks pretty simple.
我更喜欢shell解决方案,但这看起来很简单。
#8
0
I use this:
我用这个:
df -h $path | cut -f 1 -d " " | tail -1
#9
0
Linux has this, which will avoid problem with spaces:
Linux有这个,这将避免空格问题:
lsblk -no MOUNTPOINT ${device}
Not sure about BSD land.
不确定BSD土地。
#10
0
f () { echo $6; }; f $(df -P "$path" | tail -n 1)
#11
0
If you want to use only df and awk to find the filesystem device/remote share or a mount point and they include spaces you can cheat by defining the field separator of awk to be a regular expression that matches the format of the numeric sizes used to display total size, used space, available space and capacity percentage. By defining those columns as the field separator you are then left with $1
representing the filesystem device/remote share and $NF
representing the mount path.
如果你只想使用df和awk来查找文件系统设备/远程共享或挂载点,并且它们包含空格,你可以通过将awk的字段分隔符定义为与所用数字大小的格式匹配的正则表达式来作弊。显示总大小,已用空间,可用空间和容量百分比。通过将这些列定义为字段分隔符,您将获得表示文件系统设备/远程共享的$ 1和表示装载路径的$ NF。
Take this for example:
以此为例:
[root@testsystem ~] df -P
Filesystem 1024-blocks Used Available Capacity Mounted on
192.168.0.200:/NFS WITH SPACES 11695881728 11186577920 509303808 96% /mnt/MOUNT WITH SPACES
If you attempt to parse this with the quick and dirty awk '{print $1}'
or awk '{print $NF}'
you'll only get a portion of the filesystem/remote share path and mount path and that's no good. Now make awk use the four numeric data columns as the field separator.
如果你试图用快速和脏的awk'{print $ 1}'或awk'{print $ NF}'来解析它,你将只获得文件系统/远程共享路径和挂载路径的一部分,这是不好的。现在让awk使用四个数值数据列作为字段分隔符。
[root@testsystem ~] df -P "/mnt/MOUNT WITH SPACES/path/to/file/filename.txt" | \
awk 'BEGIN {FS="[ ]*[0-9]+%?[ ]+"}; NR==2 {print $1}'
192.168.0.200:/NFS WITH SPACES
[root@testsystem ~] df -P "/mnt/MOUNT WITH SPACES/path/to/file/filename.txt" | \
awk 'BEGIN {FS="[ ]*[0-9]+%?[ ]+"}; NR==2 {print $NF}'
/mnt/MOUNT WITH SPACES
Enjoy :-)
请享用 :-)
Edit: These commands are based on RHEL/CentOS/Fedora but should work on just about any distribution.
编辑:这些命令基于RHEL / CentOS / Fedora,但应该适用于任何分发。
#1
17
df
takes the path as parameter, so something like this should be fairly robust;
df将路径作为参数,所以这样的东西应该相当健壮;
df "$path" | tail -1 | awk '{ print $6 }'
#2
13
In theory stat
will tell you the device the file is on, and there should be some way of mapping the device to a mount point.
理论上,stat会告诉你文件所在的设备,并且应该有一些方法将设备映射到挂载点。
For example, on linux, this should work:
例如,在linux上,这应该工作:
stat -c '%m' $path
#3
4
Always been a fan of using formatting options of a program, as it can be more robust than manipulating output (eg if the mount point has spaces). GNU df
allows the following:
一直是使用程序格式化选项的粉丝,因为它比操作输出更强大(例如,如果挂载点有空格)。 GNU df允许以下内容:
df --output=target "$path" | tail -1
Unfortunately there is no option I can see to prevent the printing of a header, so the tail is still required.
不幸的是,我无法看到阻止打印标题的选项,因此仍需要尾部。
#4
1
i don't know what your desired output is, therefore this is a guess
我不知道你想要的输出是什么,因此这是一个猜测
#!/bin/bash
path=/home
df | awk -v path="$path" 'NR>1 && $NF~path{
print $NF
}'
Using cut with -c is not really reliable, since the output of df will be different , say a 5% can change to 10% and you will miss some characters. Since the mount point is always at the back, you can use fields and field delimiters. In the above, $NF is the last column which is the mount point.
使用带-c的cut是不可靠的,因为df的输出会有所不同,比如5%可以改为10%而你会错过一些字符。由于挂载点始终位于后面,因此您可以使用字段和字段分隔符。在上面,$ NF是最后一列,也就是挂载点。
#5
1
I would take the source code to df and find out what it does besides calling stat
as Douglas Leeder suggests.
我会把源代码发送到df并找出它除了调用stat之外的作用,正如Douglas Leeder建议的那样。
Line-by-line parsing of the df
output will cause problems as those lines often look like
df输出的逐行解析将导致问题,因为这些线通常看起来像
/dev/mapper/VOLGROUP00-logical--volume
1234567 1000000 200000 90% /path/to/mountpoint
With the added complexity of parsing those kinds of lines as well, probably calling stat
and finding the mountpoint is less complex.
由于增加了解析这些行的复杂性,可能调用stat并找到mountpoint也不那么复杂。
#6
0
mount | grep "^$path" | awk '{print $3}'
#7
0
I missed this when I looked over prior questions: Python: Get Mount Point on Windows or Linux, which says that os.path.ismount(path)
tells if path is a mount point.
当我查看之前的问题时,我错过了这个:Python:在Windows或Linux上获取Mount Point,它表示os.path.ismount(path)告诉路径是否是挂载点。
My preference is for a shell solution, but this looks pretty simple.
我更喜欢shell解决方案,但这看起来很简单。
#8
0
I use this:
我用这个:
df -h $path | cut -f 1 -d " " | tail -1
#9
0
Linux has this, which will avoid problem with spaces:
Linux有这个,这将避免空格问题:
lsblk -no MOUNTPOINT ${device}
Not sure about BSD land.
不确定BSD土地。
#10
0
f () { echo $6; }; f $(df -P "$path" | tail -n 1)
#11
0
If you want to use only df and awk to find the filesystem device/remote share or a mount point and they include spaces you can cheat by defining the field separator of awk to be a regular expression that matches the format of the numeric sizes used to display total size, used space, available space and capacity percentage. By defining those columns as the field separator you are then left with $1
representing the filesystem device/remote share and $NF
representing the mount path.
如果你只想使用df和awk来查找文件系统设备/远程共享或挂载点,并且它们包含空格,你可以通过将awk的字段分隔符定义为与所用数字大小的格式匹配的正则表达式来作弊。显示总大小,已用空间,可用空间和容量百分比。通过将这些列定义为字段分隔符,您将获得表示文件系统设备/远程共享的$ 1和表示装载路径的$ NF。
Take this for example:
以此为例:
[root@testsystem ~] df -P
Filesystem 1024-blocks Used Available Capacity Mounted on
192.168.0.200:/NFS WITH SPACES 11695881728 11186577920 509303808 96% /mnt/MOUNT WITH SPACES
If you attempt to parse this with the quick and dirty awk '{print $1}'
or awk '{print $NF}'
you'll only get a portion of the filesystem/remote share path and mount path and that's no good. Now make awk use the four numeric data columns as the field separator.
如果你试图用快速和脏的awk'{print $ 1}'或awk'{print $ NF}'来解析它,你将只获得文件系统/远程共享路径和挂载路径的一部分,这是不好的。现在让awk使用四个数值数据列作为字段分隔符。
[root@testsystem ~] df -P "/mnt/MOUNT WITH SPACES/path/to/file/filename.txt" | \
awk 'BEGIN {FS="[ ]*[0-9]+%?[ ]+"}; NR==2 {print $1}'
192.168.0.200:/NFS WITH SPACES
[root@testsystem ~] df -P "/mnt/MOUNT WITH SPACES/path/to/file/filename.txt" | \
awk 'BEGIN {FS="[ ]*[0-9]+%?[ ]+"}; NR==2 {print $NF}'
/mnt/MOUNT WITH SPACES
Enjoy :-)
请享用 :-)
Edit: These commands are based on RHEL/CentOS/Fedora but should work on just about any distribution.
编辑:这些命令基于RHEL / CentOS / Fedora,但应该适用于任何分发。