Use the "hidden" file flag for this:
chflags hidden file1.txt directory "something else hidden"
See the chflags
man page for more info. To see which files are hidden (and other file flags), use ls -lO
:
$ ls -lO
total 0
drwxr-xr-x@ 2 gordon staff hidden 68 Feb 27 00:52 directory
-rw-r--r--@ 1 gordon staff hidden 0 Feb 27 00:52 file1.txt
-rw-r--r-- 1 gordon staff uchg 0 Feb 27 00:54 lockedfile.txt
-rw-r--r--@ 1 gordon staff hidden 0 Feb 27 00:52 something else hidden
-rw-r--r-- 1 gordon staff - 0 Feb 27 00:53 visiblefile.txt
BTW the "@" is there to indicate files that have extended attributes -- the unix-style "hidden" flag also shows up as an old-MacOS-style Finder flag, which is now represented as a bit in the "com.apple.FinderInfo" extended attribute. You can see extended attributes as well with ls -lO@
, and their contents with xattr -l filename
:
$ ls -lO@ file1.txt
-rw-r--r--@ 1 gordon staff hidden 0 Feb 27 00:52 file1.txt
com.apple.FinderInfo 32
$ xattr -l file1.txt
com.apple.FinderInfo:
00000000 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 |........@.......|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000020
The "32" means there are 32 bytes of data in the "com.apple.FinderInfo" attribute, and in the full dump the hexadecimal "40" corresponds to the "Invisible" flag in the old-style FinderInfo (/FileInfo) data structure.