Making ls aware of "hidden" file flag
Mia Lopez
Is it possible to make ls aware of the hidden file flag on Mac OS X?
Currently, a simple ls -lO produces:
$ ls -lO
total 0
drwxr-xr-x@ 84 danielbeck staff - 2856 29 Mai 22:44 Applications
drwx------+ 158 danielbeck staff - 5372 29 Mai 15:27 Desktop
drwx------@ 250 danielbeck staff - 8500 30 Mai 20:40 Documents
drwx------+ 11 danielbeck staff - 374 29 Mai 22:21 Downloads
drwx------@ 84 danielbeck staff - 2856 29 Mai 22:48 Library
drwx------@ 3 danielbeck staff hidden 102 3 Apr 20:45 Movies
drwx------@ 5 danielbeck staff hidden 170 3 Apr 20:45 Music
drwx------+ 215 danielbeck staff - 7310 29 Mai 22:54 Pictures
drwxr-x---@ 7 danielbeck staff hidden 238 3 Apr 20:45 Public
drwxr-xr-x@ 4 danielbeck staff hidden 136 24 Apr 23:25 SitesSo we have three kinds of visibility: regular, visible files, files with the hidden file flag, that aren't actually hidden in Terminal, and really hidden files whose names start with a dot.
Is there a way, e.g. using an alias or a reliable shell function, to make ls treat hidden flagged and .dothidden files the same, i.e. hide by default and show with ls -A or ls -a, similar to how Finder behaves depending on the value of defaults read com.apple.Finder AppleShowAllFiles?
I am aware that man chflags specifically mentions hidden flag only hides from GUI, i.e. Finder.
1 Answer
These are the steps to get an OS X ls which hides files with the hidden flag unless the -a/-A option is specified, similar to dot files.
- Install Xcode. I am using Xcode 4.5.1 below.
- Download your OS release's
libutilandfile_cmdsfrom . There's a download button on the right side of each package list entry. This guide was written for OS X 10.8.2, YMMV with the exact steps below if you're on a different version. - Extract both archives.
Open the
file_cmds.xcodeprojin Xcode and select to build thelstarget.Select the
file_cmdsXcode project on the left, and select thelstarget in the main area. In the Build Settings tab, look for Header Search Paths in the Headers category and add the path to the folder to where you extractedlibutil.Open the file
print.cin thelsfolder on the left, and remove the line that says#include <membershipPriv.h>. Save the file afterwards. This should break something, because includes are there for a reason, but I haven't yet determined what it is.Open the file
ls.cin the same folder, and look for a comment saying/* Only display dot file if -a/-A set. */in thedisplayfunction. Replace it and the condition below it with the following:/* Only display dot file and file with hidden flag if -a/-A set. */ sp = cur->fts_statp; if (((sp != NULL && (sp->st_flags & 0x8000)) || cur->fts_name[0] == '.') && !f_listdot) { cur->fts_number = NO_PRINT; continue; }Press Cmd-B to build.
Select Products on the left (end of the folder list) and right-click
ls. Select Show in Finder.Move the
lsexecutable somewhere convenient, e.g. your home directory. Then open Terminal and runsudo mv $HOME/ls /bin/ls, or, even better, create a new folder namedbinin your home directory and move it there. Add that folder to yourPATHafterwards.
Testing the result:
$ mkdir test
$ cd test
$ touch foo bar
$ chflags hidden bar
$ /bin/ls -lO
total 0
-rw-r--r--@ 1 danielbeck staff hidden 0 25 Okt 22:25 bar
-rw-r--r-- 1 danielbeck staff - 0 25 Okt 22:25 foo
$ $HOME/bin/ls -lO
total 0
-rw-r--r-- 1 danielbeck staff - 0 25 Okt 22:25 foo
$ $HOME/bin/ls -A
bar foo 2