Monday 27 January 2014

Hidden files

Tonight's post isn't strictly GIS, but then not much is. This is really just a post about a short but handy script I've written for showing and hiding invisible files.

In case you didn't know OSX has a large number of hidden files even in the folders that you use. You may have encountered them before in the terminal when using the -a flag on ls.

ls and ls -a 

As you can see, there are several dot-files on my desktop that don't show up with ls or in Finder (normally). The single dot is a stand-in for "here" and the double dot is a stand-in for "one up" or the directory containing this directory. These two are common to all (I think) Unix like systems. The ".DS_Store" file is OSX own file for keeping track of what is happening in that folder. These three will exist in all folders created under OSX and ".DS_Store" will be created by OSX when it opens a folder no matter who created it (this can be turned off for searching through servers).
So how do we get Finder to show these files? Well, we can type

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

into the terminal and that will do it. To hide them again do the following:

defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

That's a start but it would be nice to not have to remember all that. We could create aliases for both but another way, a way that introduces Bash scripting is the following

#!/bin/sh
echo "Show or hide dot files"
select yn in "show" "hide"; do
    case $yn in
        show ) defaults write com.apple.finder AppleShowAllFiles TRUE; break;;
        hide ) defaults write com.apple.finder AppleShowAllFiles FALSE; break;;
    esac
done
#
killall Finder

Enter the above text as is into a text file, save it as "hidden.sh" in a directory that is on your path e.g. /usr/local/bin or even better, create your own Bash script folder and put that on the path (my post on paths). You then need to change the permissions for the script so that it can be executed (run as a programme). In the terminal move to the folder where you saved the script, the folder that is now on your path and type

chmod 751 hidden.sh

This command changes the permission for you, the owner to 7, the user called "group" to 5 and others to 1. What does that mean, well in short, 7 means "read, write and run", 5 means "read and run" and 1 means "run". See here for more info.

The "hidden.sh" script in action









So that's it, it works.

No comments:

Post a Comment