Mac Bash 脚本 - 判断隐藏文件夹是否隐藏

发布于 2024-12-02 07:25:14 字数 263 浏览 0 评论 0 原文

我正在使用 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

I'm using Mac OSX Lion and I would like to have a script that tells me if a directory is hidden or visible. the directory is .whatyoulookingatfool.

This is what I have now...

#!/bin/bash

#Check for dir
if test -d /Users/NSutton/Documents/.whatyoulookingatfool; then
    echo "go go go"
else
    echo "well shit"
fi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕梦 2024-12-09 07:25:14

我这里没有任何 osx 机器,但我假设 mac 有一个基本名称命令和足够新的 bash。

#!/bin/bash

dir=$1
bn=$(basename $dir)

if [[ -d $dir && $bn == .* ]]
then
   echo yep
else
   echo nay
fi

请注意,这不适用于 ... 目录。

I don't have any osx machine right here, but I assume that mac has a basename command and new enough bash.

#!/bin/bash

dir=$1
bn=$(basename $dir)

if [[ -d $dir && $bn == .* ]]
then
   echo yep
else
   echo nay
fi

Note that this does not work with . and .. directories.

朦胧时间 2024-12-09 07:25:14

好的,您是在谈论由 Finder 隐藏的文件,还是以句点开头的文件,因此除非您指定参数,否则它们在 ls 等命令上隐藏 (如 -a)看到它们?

如果您主要谈论的是点,则可以通过多种方式来实现。 Ahe 显示了其中之一。另一种是这样的:

if [[ ${fileName%%.*} = "" ]
然后
echo "文件名以句点开头"
别的
echo "文件名不以句点开头"
fi

顺便说一句,方括号相当于 test 命令,通常是首选。

因此:

if test -d /Users/NSutton/Documents/.whatyoulookingatfool; then

相同

if [ -d /Users/NSutton/Documents/.whatyoulookingatfool ]; then

与注意方括号周围的空格 。 Bash 程序中内置了更多使用双方括号进行模式匹配的测试。请参阅 BASH 手册页 了解更多信息。

现在,如果您想知道某个文件是否隐藏在 Finder 中。例如,$HOME/Library 在命令行工具中可见,但通常在 Finder 中不可见,您必须使用 GetFileInfo 命令。 Mac OS X 中内置此功能,用于查看某个文件是否对 Finder 不可见。

还有 /.hidden 目录列出了 Mac OS X 10.4 (Tiger) 之前使用的所有隐藏文件。

不幸的是,我面前没有 Mac 来运行任何测试,所以我无法给你确切的命令,但请检查 GetFileInfo 手册页,然后播放稍微看看它是如何工作的。

顺便说一句,您可以通过以下命令打开和关闭文件的隐藏:

defaults write com.apple.finder AppleShowAllFiles TRUE  #Shows hidden files
defaults write com.apple.finder AppleShowAllFiles FALSE #Hides hidden files

您可能需要重新启动 Finder:

killAll 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:

if test -d /Users/NSutton/Documents/.whatyoulookingatfool; then

is the same as

if [ -d /Users/NSutton/Documents/.whatyoulookingatfool ]; then

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:

defaults write com.apple.finder AppleShowAllFiles TRUE  #Shows hidden files
defaults write com.apple.finder AppleShowAllFiles FALSE #Hides hidden files

You may have to restart the Finder:

killAll Finder
━╋う一瞬間旳綻放 2024-12-09 07:25:14

那么如果名称存在重命名为.name,反之亦然?

#!/bin/sh

name=whatyoulookingatfool

for f in . ''; do
    test -d "$f$name" || continue
    mv "$f$name" "${f:-.}$name"
    break
done

So if name exists rename to .name, and vice versa?

#!/bin/sh

name=whatyoulookingatfool

for f in . ''; do
    test -d "$f$name" || continue
    mv "$f$name" "${f:-.}$name"
    break
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文