A custom Terminal command to show/hide hidden files in the Mavericks Finder

In my previous blog post I showed the Terminal commands that allow you to cause Finder to display hidden files and directories.

finder-show-all-files

By default Finder does not show a file or directory if its name begins with a period, such as the .gitignore file commonly edited by Git users. In this post I show how to simplify this task by adding a custom command that Terminal will recognize and execute, so that you don’t need to copy and paste the command every time you need to use it (or, dare I say, memorize it!).

Please note, the Terminal command seen in this post works starting in OS X Mavericks. Previous versions of OS X used a slightly different command to show/hide hidden items (Google it if you care).

First, fire up Terminal. If this is your first time using the Mac’s command line, it is available via Applications > Utilities > Terminal. ¬†Once the command line window is up and running, open up one of the configuration files used by Terminal (which is actually a Bash command line interface inherited from Unix) by typing the bold text here:

user$ open ~/.bashrc

After pressing the Return key you should see a TextEdit window showing the contents of that file. If the file does not exist for some reason, you can create it with this command touch ~/.bashrc and then open it.

Now we will add the code needed to introduce a custom command in Terminal, named showallfiles. Copy and paste this code into the .bashrc file and then save it and close the editor window.

showallfiles() {
 defaults write com.apple.finder AppleShowAllFiles -boolean $1
 killall Finder
}

In order to load this new command into Terminal, either quit the Terminal session and reopen it, or type this command that causes the edited file to be reprocessed by the active Terminal session:

user$ source ~/.bashrc

Now you can easily toggle between showing all hidden files and directories…

user$ showallfiles true

…and hiding them when you no longer want to see them in Finder…

user$ showallfiles false

Happy programming!

About Josh Smith

Josh Smith is a musician at heart, and a software developer by obsession. He lives in Arizona, plays classical music on the piano, and enjoys a variety of tasty beverages. He has a wonderful wife and thinks that, in general, life is good.
This entry was posted in Tips and Tricks. Bookmark the permalink.

One Response to A custom Terminal command to show/hide hidden files in the Mavericks Finder

  1. Pingback: View hidden files and directories with Finder in OS X Mavericks | iJoshSmith

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s