在bash中,文件操作符(-f)可以不区分大小写吗?
我正在执行以下操作:
if [ -f $FILE ] ; then
echo "File exists"
fi
但我希望 -f
不区分大小写。也就是说,如果 FILE 是 /etc/somefile
,我希望 -f 识别 /Etc/SomeFile
。
我可以使用 glob: 部分解决它,
shopt -s nocaseglob
TARG='/etc/somefile'
MATCH=$TARG* #assume it returns only one match
if [[ -f $MATCH ]] ; then
echo "File exists"
fi
但不区分大小写的 globbing 仅适用于文件名部分,而不适用于完整路径。所以如果 TARG 是 /Etc/somefile
,它将不起作用。
有什么办法可以做到这一点吗?
I'm doing the following:
if [ -f $FILE ] ; then
echo "File exists"
fi
But I want the -f
to be case-insensitive. That is, if FILE is /etc/somefile
, I want -f to recognize /Etc/SomeFile
.
I can partially work around it with glob:
shopt -s nocaseglob
TARG='/etc/somefile'
MATCH=$TARG* #assume it returns only one match
if [[ -f $MATCH ]] ; then
echo "File exists"
fi
but the case-insensitive globbing works only on the filename portion, not the full path. So it will not work if TARG is /Etc/somefile
.
Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
问题是您的文件系统区分大小写。文件系统仅提供两种相关的方式来获取文件:要么指定一个精确的、区分大小写的文件名并以这种方式检查其是否存在,要么读取目录中的所有文件,然后检查每个文件是否与某种模式匹配。
换句话说,检查区分大小写的文件系统上是否存在不区分大小写的文件版本是非常低效的。 shell 可能会为您做这件事,但在内部它会读取所有目录的内容并根据模式检查每个内容。
考虑到所有这些,这是可行的:
但是除非您想搜索从“/”开始的所有内容,否则您必须单独检查路径的组成部分。这是没有办法解决的;您无法神奇地检查区分大小写的文件系统上不区分大小写的匹配的整个路径!
The problem is that your filesystem is case-sensitive. The filesystem provides only two relevant ways to get a file: either you specify an exact, case-sensitive filename and check for its existence that way, or you read all the files in a directory and then check if each one matches a pattern.
In other words, it is very inefficient to check if a case-insensitive version of a file exists on a case-sensitive filesystem. The shell might do it for you, but internally it's reading all the directory's contents and checking each one against a pattern.
Given all that, this works:
BUTunless you want to search everything from '/' on down, you must check component of the path individually. There is no way around this; you can't magically check a whole path for case-insensitive matches on a case-sensitive filesystem!
您可以使用 nocasematch 选项
you can use the nocasematch option
不知道如何只使用 shell 评估。
但 grep 可以不区分大小写,因此调用 grep 、 find 和 wc 的脚本可能会满足您的需求。
not knowing howto only using shell evaluations.
but grep can be case-insensitive, therefore a script that invokes grep , find and wc may meet your demand.
一般来说,如果文件系统区分大小写,这是很难做到的。基本上,您已经分别迭代每个祖先目录。这是 Python 的起点:
This is very hard to do, in general, if the file system is case-sensitive. Basically, you have iterate over each of the ancestor directories separately. Here is a starting point in
Python
:因为这个问题被标记为 bash
简介
我在临时挂载点挂载 NTFS 分区后搜索 Windows 的注册表文件
mnt=$(mktemp -d)
然后,如果命令:
执行该工作,因为该命令基于 $mnt,
find
将愚蠢地扫描整个文件系统并花费大量时间。我的纯bash函数
为此,我编写了这个
ipath
函数:然后用于测试:
用作变量:
另一个具有多个答案的测试和空格路径:
注意,使用 unicode 重音字符进行测试:
résumé.pdf
变为[rR][éÉ][sS][uU][mM][éÉ].[pP][dD] [fF]
。As this question is tagged bash
Introduction
I was searching for window's registry file after mounted NTFS partition in a temporary mount point
mnt=$(mktemp -d)
Then, if the command:
do the job, as this command is based on $mnt,
find
will stupidely scan the whole filesystem and take a lot of time.My pure bash function
For this, I wrote this
ipath
function:Then for testing:
for use as a variable:
Another test with multiple answer and spaced path:
Note, tested with unicode accented characters:
résumé.pdf
become[rR][éÉ][sS][uU][mM][éÉ].[pP][dD][fF]
.这是使用 Bash 的起点:
And here's a starting point using Bash: