Question

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
Was it helpful?

Solution

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.

OTHER TIPS

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top