Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know you can set or unset the hidden flag of a folder/file by doing chflags hidden foo.txt and chflags nohidden foo.txt.

But is there anyway of telling whether the folder/file is currently hidden or not?

I don't want to just determine if the folder/file is beginning with a dot.

share|improve this question

migrated from stackoverflow.com Aug 18 '11 at 19:09

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers 2

up vote 5 down vote accepted

According to the ls man page, you should be able -O option combined with the -l option to view flags with ls. For example:

ls -Ol foo.txt
-rw-r--r-- 1 harry staff - 0 18 Aug 19:11 foo.txt
chflags hidden foo.txt
ls -Ol foo.txt
-rw-r--r-- 1 harry staff hidden 0 18 Aug 19:11 foo.txt
chflags nohidden foo.txt
ls -Ol foo.txt
-rw-r--r-- 1 harry staff - 0 18 Aug 19:11 foo.txt

Edit: Just to give a more specific solution to what the OP wanted (see comments below): To see if a folder is hidden or not, we can pass the -a option to ls to view the folder itself. We can then pipe the output into sed -n 2p (thanks Stack Overflow) to get the required line of that output. An example:

mkdir foo
chflags hidden foo
ls -aOl foo | sed -n 2p
drwxr-xr-x@ 2 harry staff hidden 68 18 Aug 19:11 .

Edit 2: For a command that should work regardless of whether it's a file or a folder, we need to do something slightly more hacky.

The needed line of output from ls -al varies depending on whether the thing is a file or folder, as folders show a total count, whereas files do not. To get around this, we can grep for the character r. This should be in ~all of all files/folders (nearly all should have at least one read permission), but not in the totals line.

As the line we want to get then becomes the first line, we can use head -n 1 to get the first line (alternative, if you prefer sed, sed -n 1p could be used).

So, for example with a directory:

mkdir foo
chflags hidden foo
ls -aOl foo | grep r | head -n 1
drwxr-xr-x@ 2 harry staff hidden 68 18 Aug 19:11 .

and with a file:

touch foo.txt
chflags hidden foo.txt
ls -aOl foo.txt | grep r | head -n 1
-rw-r--r-- 1 harry staff hidden 0 18 Aug 19:11 foo.txt

Edit 3: See Tyilo's answer below for a nicer way than grepping for r :)

share|improve this answer
    
But doing this with a folder, will list the flags for the files/folders under it –  Tyilo Aug 18 '11 at 18:18
    
To just view hidden files, pipe it through grep (e.g ls -Ol fooDir/ | grep hidden) to view only hidden files/folders. This will still show all the files, but if you pipe it through some sed/awk magic (someone else will have to help here I'm afraid) you should be able to get just a list of files. –  hrickards Aug 18 '11 at 18:22
    
I don't want a list of the files I just want to know is folder "foo" is hidden or not –  Tyilo Aug 18 '11 at 18:27
    
Ok. So to view the directory itself add the -a option to ls. To just get the line you need from the output, you can use sed. For example: ls -aOl foo | sed -n 2p. This will display one line of output. If it contains the word "hidden", then foo is hidden. If it does not, foo is not hidden. :) –  hrickards Aug 18 '11 at 18:33
1  
Using ls -Old dirname/ will show you the properties of the directory itself, not its contents. –  bahamat Aug 18 '11 at 23:10
show 3 more comments

Found the solution here: ls - get information of the directory specified only, not info about the sub-files/folder

Which basically is ls -ldO foo and then you just append | awk '{ print $5 }' to make it display the information

share|improve this answer
    
Yep, or you could do that :) Much cleaner way than grepping for r –  hrickards Aug 18 '11 at 19:05
    
Should I mark your or mine question as the answer? Your question wasn't what I used / the best way, but my answer is not mine. –  Tyilo Aug 18 '11 at 19:06
    
I'm not entirely sure. I guess the expected way would be to mark mine as the answer, then accept the answer on your other question. I imagine anything's fine though. –  hrickards Aug 18 '11 at 19:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.