How to test if file is a socket in command line?

Posted on

Problem :

As titled. Is there a way to test in command line whether a file is a socket? By test I mean whether I can tell from the stdout, and not programmatically as in a bash script, although that’s fine too. Btw, I am on a Mac (once in a while the commands are different). Thanks!

Solution :

A socket has “s” in the first permission flag:

ls -la X0 
srwxrwxrwx 1 paul hum 0 Jul 14 14:48 X0

You can test in bash with the -S switch:

if  [ -S /my/file.socket ] ; then
   # stuff
fi

Or the find command could be used to find any sockets in a directory:

find  . -type s

test -S /var/run/some_socket_file

if $? == 0, this file is a socket file.

Leave a Reply

Your email address will not be published. Required fields are marked *