Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hope it's OK to note this, now that I've encountered it:

I'm already aware that one can test for file existence using the test ([) command:

$ touch exists.file
$ if [ -f exists.file ] ; then echo "yes" ; else echo "no" ; fi
$ if [ -f noexists.file ] ; then echo "yes" ; else echo "no" ; fi

... but that's a bit of typing there :) So, I was wandering if there is a "default" single command, that will return the file existence result to stdout?

I can also use test with the exit status $?:

$ test -f exists.file ; echo $?
$ test -f noexists.file ; echo $?

... but this is still two commands - and the logic is also "inverse" (0 for success, and nonzero for failure).

The reason I'm asking is that I need to test for file existence in gnuplot, whose "system("command") returns the resulting character stream from stdout as a string."; in gnuplot I can do directly:

gnuplot> if (0) { print "yes" } else { print "no" }
gnuplot> if (1) { print "yes" } else { print "no" }

so I'd basically like to be able to write something like this (pseudo):

gnuplot> file_exists = system("check_file --stdout exists.file")
gnuplot> if (file_exists)  { print "yes" } else { print "no" }

... unfortunately, I cannot use that directly via test and $?:

gnuplot> file_exists = system("test -f exists.file; echo $?")
gnuplot> if (file_exists)  { print "yes" } else { print "no" }

... I'd have to invert the logic.

So, I'd basically have to come up with sort of a custom script solution (or writing a script inline as one-liner) ... and I thought, if there is already a default command that can print 0 or 1 (or a custom message) to stdout for file existence, then I don't have to do so :)

(note that ls may be a candidate, but it's stdout output is far too verbose; and if I want to avoid that, I'd have to suppress all output and again return exist status, as in

$ ls exists.file 1>/dev/null 2>/dev/null ; echo $? 
$ ls noexists.file 1>/dev/null 2>/dev/null ; echo $? 

... but that's again two commands (and more typing), and "inverted" logic...)

So, is there a single default command that can do this in Linux?

share|improve this question
how about ( [ -f py_concur.py ] && echo "file exists" ) || echo "file does not exist"? – iruvar Feb 1 '13 at 18:05

Sounds like you need the exit status reversed, so you could do:

system("[ ! -e file ]; echo $?")


system("[ -e file ]; echo $((!$?))")

(note that -f is for if file exists and is a regular file).

share|improve this answer

What about "inverting the logic" by:

file_exists = 1-system("test -f exists.file; echo $?")
share|improve this answer

Your Answer


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.