Check if file exists for hidden (.file) files

3

Using if [ -f "file" ] works on non-hidden files.
I'm trying to use it for a hidden file: if [ -f ".file" ] and of course it returns false (even that .file exists).

If I use if [ -f -a "file" ] it returns true for all hidden files (that is, if 'any' file is hidden).

How can I check if a .someFile exists (where 'someFile' is a hidden file)?

bauerMusic

Posted 2018-04-21T08:57:23.030

Reputation: 151

1I suggest: help test – Cyrus – 2018-04-21T12:25:10.500

@Cyrus Ok, now I feel dumb. It's all there, listed green on black. Thanks! – bauerMusic – 2018-04-21T13:31:51.203

if [ -f "file" ] will work fine for detecting hidden (but otherwise normal) files. If it's failing, you probably have something like a symbolic link to a file elsewhere; that's not a regular file, so the -f test will return false. It has nothing to do with whether it's hidden or not. – Gordon Davisson – 2018-04-21T17:22:22.177

Answers

2

Ok, found it (by just trying really). Need to use -a instead of -f:
if [ -a ".file" ]

-Edit-

Following the great advice by Cyrus, $ help test will list all the test options. Specifically:
-a FILE True if file exists.
-f FILE True if file exists and is a regular file.

bauerMusic

Posted 2018-04-21T08:57:23.030

Reputation: 151