Mac Bash 脚本 - 判断隐藏文件夹是否隐藏
我正在使用 Mac OSX Lion,我想要一个脚本来告诉我目录是隐藏还是可见。该目录是 .whatyoulookingatfool。
这就是我现在所拥有的...
#!/bin/bash
#Check for dir
if test -d /Users/NSutton/Documents/.whatyoulookingatfool; then
echo "go go go"
else
echo "well shit"
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我这里没有任何 osx 机器,但我假设 mac 有一个基本名称命令和足够新的 bash。
请注意,这不适用于
.
和..
目录。I don't have any osx machine right here, but I assume that mac has a basename command and new enough bash.
Note that this does not work with
.
and..
directories.好的,您是在谈论由 Finder 隐藏的文件,还是以句点开头的文件,因此除非您指定参数,否则它们在
ls
等命令上隐藏 (如-a
)看到它们?如果您主要谈论的是点,则可以通过多种方式来实现。 Ahe 显示了其中之一。另一种是这样的:
if [[ ${fileName%%.*} = "" ]
然后
echo "文件名以句点开头"
别的
echo "文件名不以句点开头"
fi
顺便说一句,方括号相当于
test
命令,通常是首选。因此:
相同
与注意方括号周围的空格 。 Bash 程序中内置了更多使用双方括号进行模式匹配的测试。请参阅 BASH 手册页 了解更多信息。
现在,如果您想知道某个文件是否隐藏在 Finder 中。例如,
$HOME/Library
在命令行工具中可见,但通常在 Finder 中不可见,您必须使用 GetFileInfo 命令。 Mac OS X 中内置此功能,用于查看某个文件是否对 Finder 不可见。还有
/.hidden
目录列出了 Mac OS X 10.4 (Tiger) 之前使用的所有隐藏文件。不幸的是,我面前没有 Mac 来运行任何测试,所以我无法给你确切的命令,但请检查 GetFileInfo 手册页,然后播放稍微看看它是如何工作的。
顺便说一句,您可以通过以下命令打开和关闭文件的隐藏:
您可能需要重新启动 Finder:
Okay, are you talking about files that are hidden by the Finder, or files that start with a period, so they are hidden on commands such as
ls
unless you specify a parameter (like-a
) to see them?If you're mainly talking about the dot, you can do this various ways. One is shown by Ahe. Another is this way:
if [[ ${fileName%%.*} = "" ]
then
echo "File name starts with period"
else
echo "File name does not start with period"
fi
BTW, the square braces are the equivalent of the
test
command and is usually preferred.Thus:
is the same as
Note the spaces around the square braces. There are even more tests that are built into the Bash program using double square braces that can do pattern matching. See the BASH manpage for more information.
Now, if you want to know if a file is hidden in the Finder. For example,
$HOME/Library
is visible in the command line tool, but is normally invisible in the Finder, you'll have to use the GetFileInfo command. This is built into Mac OS X to see if a file is suppose to be invisible to the Finder.There's also the
/.hidden
directory that lists all files that are hidden which was used before Mac OS X 10.4 (Tiger).Unfortunately, I don't have a Mac in front of me to run any tests, so I can't give you the exact command, but check the GetFileInfo manpage, and play around a bit and see how it works.
BTW, you can turn on and off the hiding of files via the following command:
You may have to restart the Finder:
那么如果名称存在重命名为.name,反之亦然?
So if name exists rename to .name, and vice versa?