Advertisement

The seen and the unseen; invisible files and the command line

visible music folder

Since C.K.'s post on invisible iPod files got so much response over the weekend, I though I'd do a quick rundown this morning on the different ways to handle invisible files in OS X.

The first thing to understand is that there are three ways to make a file invisible in OS X. The first, and least used, is the .hidden file. This is a file that sits at the root of the filesystem and keeps track of which files should be hidden. It's a holdover from Classic and the original HFS filesystem, and isn't included by default on volumes formatted by Tiger's Disk Utility. The OS will, however, honor it if it's present. The second way to hide a file in OS X is the traditional Unix method: start the filename with a '.'. If you run ls -a at the command line in your home directory, you can see this in action. You'll notice a number of files and directories with names like .bashrc that aren't visible in the finder or via the normal ls command. The final way to make a file invisible under OS X is to modify it's HFS+ extended attributes.

Making the first two kinds of files visible is easy:

To unhide a .hidden file, just edit .hidden and reboot. To work with Unix invisible files, just use ls -a instead of ls or rename the files. To rename files from the command line, use mv:

mv [.hidden-file] [filname-without-leading-dot]

If you always want to see Unix hidden files in the Finder, you can also change your finder preferences with the following command (warning: some folders, like your Home folder, will become very cluttered):

defaults write com.apple.finder AppleShowAllFiles '1'

Files with the invisibility bit set in their metadata are a little bit trickier. For one or two files, most people will find it easiest to use a graphical tool like XRay to manually change the file attributes. The defaults write hack will show these invisible files, too, and tools like InVisibles and TinkerTool can automate the process of changing Finder's plist to control the visible clutter at any given time. If you're working with a lot of files or working from the command line, though, you're going to want to check out the SetFile command included with the Developer Tools. SetFile can be used to toggle any of the HFS+ attributes, including the invisibility bit. The command to unset the invisibility bit for a file is:

/Developer/Tools/SetFile -a v [filename]

And finally, we'll talk a little bit about copying. If you're going to move an invisible file before you work with it, the simplest thing to do is just not copy the HFS+ attributes when you copy the file, and the easiest way to do this is to use a tool that doesn't recognize them. In recent releaes, the Unix cp and mv commands have gotten smater, but most of the traditional archiving utilities haven't. tar, in particular, has been a favorite tool for many years for moving files and directories aroun unix filesystems. Its name stands for 'Tape Archival Retreival' and it's original function was to make backups to tape and restore files from tape when needed. It is included with OS X and will happlity ignore file metadata, leaving a visible copy of any file in a new location. By default it creates a .tar archive of a directory on a disk, but it can be used in what it known as a 'tar pipe' to simply copy directories from one location to another. For instance, the command to copy your iPod_Control directory to the Desktop would be:

cd /Volumes/[your_ipod]
tar -cf - ./iPod_Control | tar -xf - -C ~/Desktop

As always, replace anything in square brackets ([]) with the actual path on your system.