3

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!

2 Answers 2

3

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
0
0

test -S /var/run/some_socket_file

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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