Sample Header Ad - 728x90

How to list in Terminal only files visible in Finder?

0 votes
1 answer
157 views
I know how to view hidden files in the Finder. Instead, what I'd like to do is list files in the shell/Terminal by the same criteria that the Finder uses for files that are hidden. (My objective is to collect metrics using a cronjob/launchd script that are consistent with what appears in the UI.) This is proving to be more difficult than it sounds. Here is an example case of the ways I am aware of to hide a file from the Finder (Big Sur 11.6, Apple Silicon, APFS):
mkdir test
cd test
touch regular hidden invisible .dotfile
SetFile -a V invisible
chflags nohidden invisible
chflags hidden hidden
This requres that SetFile be present, which is available with Developer Tools. The expected outcome of the command or script is one file, regular. All three of the other files are not visible in Finder under usual circumstances. I would like the solution to only require native tools (no Developer Tools or gnu coreutils/homebrew and the like). So far, my approach would be to loop through all files output by ls -lO | awk 'NR!=1 && $5!~/hidden/ { print $0 }' (this filters out dotfiles and the hidden file flag), and use GetFileInfo (another Developer Tool installed alongside SetFile) to filter out the invisible attribute. However, this requires that Developer Tools be installed, which I would prefer to avoid the dependency on, and also seems awkward and excessively complicated--and likely also inefficient if there are thousands of files in a directory. I suspect that the other options would be to manually parse the output of xattr -px com.apple.FinderInfo invisible (more complicated but avoids the GetFileInfo dependency) or to use osascript to somehow make use of the Finder's existing logic instead of attempting to reimplement it using BSD tools. **Edit:** Since apparently some users' setfile commands set the hidden flag, I added chflags nohidden invisible to the test case since it is entirely likely for the filesystem to contain files (created via other means) which only have the extended attribute FinderInfo flag and do not have the BSD file flag.
Asked by NReilingh (4509 rep)
Sep 19, 2021, 03:33 PM
Last activity: Sep 30, 2021, 08:08 PM