How to test if file flag hidden is set
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
How can I test to see if the file flag 'hidden' is set? (ls -lO | grep hidden can filter files with the hidden flag set)
If I stat -f %f I get 32768 for a hidden file and 0 for a file that is not hidden, but is this a reliable method?
10 replies
Sign in or join Beachball Help to reply.
- 0
red_menace ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
There is also GetFileInfo - it can return just a 0 or 1.
- 0
BobHarris ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
The following is from the /usr/include/sys/stat.h, where UF seems to relate to User Flags:
/* #define UF_NOUNLINK 0x00000010 */ /* file may not be removed or renamed */
#define UF_COMPRESSED 0x00000020 /* file is hfs-compressed */
/* Bits 0x0040 through 0x4000 are currently undefined. */
*/fine UF_HIDDEN 0x00008000 /* hint that this item should not be */
* Definitions of flags stored in file flags word.
*
* Super-user and owner changeable flags.
*/
#define UF_SETTABLE 0x0000ffff /* mask of owner changeable flags */
#define UF_NODUMP 0x00000001 /* do not dump file */
#define UF_IMMUTABLE 0x00000002 /* file may not be changed */
#define UF_APPEND 0x00000004 /* writes to file may only append */
#define UF_OPAQUE 0x00000008 /* directory is opaque wrt. union */
/*
* The following bit is reserved for FreeBSD. It is not implemented
* in Mac OS X.
/* #define UF_NOUNLINK 0x00000010 */ /* file may not be removed or renamed */
#define UF_COMPRESSED 0x00000020 /* file is hfs-compressed */
/* Bits 0x0040 through 0x4000 are currently undefined. */
#define UF_HIDDEN 0x00008000 /* hint that this item should not be */
/* displayed in a GUI */ngeable flags */
/*efine SF_ARCHIVED 0x00010000 /* file is archived */
* Super-user changeable flags.000 /* file may not be changed */
*/fine SF_APPEND 0x00040000 /* writes to file may only append */
#define SF_SETTABLE 0xffff0000 /* mask of superuser changeable flags */
#define SF_ARCHIVED 0x00010000 /* file is archived */
#define SF_IMMUTABLE 0x00020000 /* file may not be changed */
#define SF_APPEND 0x00040000 /* writes to file may only append */
*/
/* #define SF_NOUNLINK 0x00100000 */ /* file may not be removed or renamed */
* The following two bits are reserved for FreeBSD. They are not
* implemented in Mac OS X.DEN bit. */
*/
/* #define SF_NOUNLINK 0x00100000 */ /* file may not be removed or renamed */
/* #define SF_SNAPSHOT 0x00200000 */ /* snapshot inode */
/* NOTE: There is no SF_HIDDEN bit. */Note: 32768 will ONLY be the value if ONLY hidden is set. If any of the other flags are set the value will be something different. You need to figure out how to test for a single bit being set.
Message was edited by: BobHarris
- 0
red_menace ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
There is also GetFileInfo - it can return just a 0 or 1.
- 0
BobHarris ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
The following is from the /usr/include/sys/stat.h, where UF seems to relate to User Flags:
/* #define UF_NOUNLINK 0x00000010 */ /* file may not be removed or renamed */
#define UF_COMPRESSED 0x00000020 /* file is hfs-compressed */
/* Bits 0x0040 through 0x4000 are currently undefined. */
*/fine UF_HIDDEN 0x00008000 /* hint that this item should not be */
* Definitions of flags stored in file flags word.
*
* Super-user and owner changeable flags.
*/
#define UF_SETTABLE 0x0000ffff /* mask of owner changeable flags */
#define UF_NODUMP 0x00000001 /* do not dump file */
#define UF_IMMUTABLE 0x00000002 /* file may not be changed */
#define UF_APPEND 0x00000004 /* writes to file may only append */
#define UF_OPAQUE 0x00000008 /* directory is opaque wrt. union */
/*
* The following bit is reserved for FreeBSD. It is not implemented
* in Mac OS X.
/* #define UF_NOUNLINK 0x00000010 */ /* file may not be removed or renamed */
#define UF_COMPRESSED 0x00000020 /* file is hfs-compressed */
/* Bits 0x0040 through 0x4000 are currently undefined. */
#define UF_HIDDEN 0x00008000 /* hint that this item should not be */
/* displayed in a GUI */ngeable flags */
/*efine SF_ARCHIVED 0x00010000 /* file is archived */
* Super-user changeable flags.000 /* file may not be changed */
*/fine SF_APPEND 0x00040000 /* writes to file may only append */
#define SF_SETTABLE 0xffff0000 /* mask of superuser changeable flags */
#define SF_ARCHIVED 0x00010000 /* file is archived */
#define SF_IMMUTABLE 0x00020000 /* file may not be changed */
#define SF_APPEND 0x00040000 /* writes to file may only append */
*/
/* #define SF_NOUNLINK 0x00100000 */ /* file may not be removed or renamed */
* The following two bits are reserved for FreeBSD. They are not
* implemented in Mac OS X.DEN bit. */
*/
/* #define SF_NOUNLINK 0x00100000 */ /* file may not be removed or renamed */
/* #define SF_SNAPSHOT 0x00200000 */ /* snapshot inode */
/* NOTE: There is no SF_HIDDEN bit. */Note: 32768 will ONLY be the value if ONLY hidden is set. If any of the other flags are set the value will be something different. You need to figure out how to test for a single bit being set.
Message was edited by: BobHarris
- 0
Tony T1 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Note: 32768 will ONLY be the value if ONLY hidden is set. If any of the other flags are set the value will be something different.
That's what I thought (...but I didn't think to look at stat.h). GetFileInfo will have to do.
- 0
BobHarris ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Helpful? I'm shooting for "Solved!" 🙂
You could use something like (( $(stat -f %f filename) & 32768 )) to mask the hidden bit. For example:
#!/usr/bin/env bash
UF_HIDDEN=32768
filename=tmp.hidden
touch $filename
chflags hidden $filename
if [[ $(( $(stat -f %f $filename) & ${UF_HIDDEN} )) != 0 ]]; then
echo $filename: this file is marked hidden
else
echo $filename: visiable file
fi
filename=tmp.visible
touch $filename
chflags nohidden $filename
if [[ $(( $(stat -f %f $filename) & ${UF_HIDDEN} )) != 0 ]]; then
echo $filename: this file is marked hidden
else
echo $filename: visiable file
fiRunning this little script generates:
tmp.hidden: this file is marked hidden
tmp.visible: visiable fileMessage was edited by: BobHarris
- 0
William-Boyd-Jr ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
BobHarris wrote:
UF_HIDDEN=32768I like to avoid hard-coding things like that. It's possible to extract the value of "UF_HIDDEN" from stat.h, although the sample code I developed to do that is (1) a little messy, (2) overwrites the values of parameters received by the script, (3) is dependent on the value of UF_HIDDEN being in the first occurrence of that string in stat.h, (4) requires a few intermediate variables, and (5) results in some strange garbage in one of those values, although that wouldn't be obvious during normal use.
There's probably a cleaner way to accomplish the same thing.
- 0
BobHarris ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Well, if you want robust, professional code, then you are absolutely correct 🙂
I will note that GetFileInfo only exists if you have installed the Xcode package. If this is your personal environment, that is easy to insure. If this is for a wider distribution, that might be a problem.
You could write a C or other language program that can include stat.h and query the file for you.
Maybe there is a perl, python, ruby Mac OS X standard module that knows about these extended Mac OS X st_flags that would keep your code robust.
In a past life, I spend about 6 months of my life living inside the stat() and fstat() operating system calls for a now defuncted major Unix operating system vendor. Changing the meaning of any value in the stat.h header file is a very Very VERY painful thing to do. Too many of your own programs and many customer programs depend on the stat.h information remaining unchanged.
At best you can start using existing unused fields, but changing the meaning of an exsiting field, then size of the stat structure, the position in the structure for any of the fields, is will shutdown your own product development, and cause your customers to scream so loud that even the CEO will notice.
This is because any change that upsets the stat structure requries just about every program in the operating system and at the customer site to be recompiled. And most customers are not interested in figuring out how to rebuild a program, or to get new versions from a 3rd party.
At best you can make changes when switching to a different platform (68000 to PowerPC to intel) maybe.
So the question is; do you need this to work forever, or just until Apple leaps from intel to yet another CPU architecture? 🙂
- 0
William-Boyd-Jr ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Your point about OS structure stability is well taken.
Another justification of my approach is good documentation. Forcing a successor code maintainer to figure out the meaning of the value 32768 is cruel. Your sample code uses a variable name that matches the stat.h value, which helps a lot. Another thing that might add value is a comment naming the .h file where that value is defined.
- 0
Tony T1 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
You could use something like (( $(stat -f %f filename) & 32768 )) to mask the hidden bit.
I was just looking into bit masking. It's been a while since I twiddled-bits in my C programing days (...before C++ became popular), but I gave up as I couldn't find anything. I didn't realize that it was as easy as it was in C with '&'.
I will note that GetFileInfo only exists if you have installed the Xcode package.
Agreed that's why I was looking for a stat solution. (you'll notice I had said that "GetFileInfo will have to do.").
HiddenFlag=$((( $(stat -f %f filename) & 0x00008000 ))) is what I was looking for, thanks.