23

On a Linux box I want to check if a specific socket file exists. I know the socket files exists, but my checks in bash don't show that to me:

$ ls -l /var/run/supervisor.sock
srwxrw-rw- 1 root root 0 Jun  3 13:30 /var/run/supervisor.sock  # <== THE FILE EXISTS!!
$ if [ ! -f /var/run/supervisor.sock ]; then echo 'file does not exist!'; fi
file does not exist!

Why oh why can't bash see that the file exists?

kramer65
  • 50,427
  • 120
  • 308
  • 488

1 Answers1

56

http://www.tldp.org/LDP/abs/html/fto.html

Use -S to test if its a socket. -f is for regular files.

See man 1 test:

   -e FILE
          FILE exists
   -f FILE
          FILE exists and is a regular file
   ...
   ...
   -S FILE
          FILE exists and is a socket
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
bodangly
  • 2,473
  • 17
  • 28
  • 1
    Dang. You beat me to it. Same link and everything. That said, lots of things in Unix are "files". However, if there's a separate test for that specific kind of file, you should use that instead. – Mr. Llama Jun 03 '16 at 15:44
  • @Mr.Llama :) This is true. However, -f is specifically for regular files, and so in certain cases cannot be used, such as here. – bodangly Jun 03 '16 at 15:46