30

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
yes
$ if [ -f noexists.file ] ; then echo "yes" ; else echo "no" ; fi
no

... 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 $?
0
$ test -f noexists.file ; echo $?
1

... 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" }
no
gnuplot> if (1) { print "yes" } else { print "no" }
yes

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" }
yes

... 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" }
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 $? 
0
$ ls noexists.file 1>/dev/null 2>/dev/null ; echo $? 
2

... 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?

1
  • 4
    how about ( [ -f py_concur.py ] && echo "file exists" ) || echo "file does not exist"?
    – iruvar
    Commented Feb 1, 2013 at 18:05

5 Answers 5

11

Use this single bash command:

 [ -f /home/user/file_name ]

The [] perform the test and returns 0 on success

5
  • 4
    doesn't return anything if either case -- CentOS
    – amphibient
    Commented Sep 17, 2018 at 16:53
  • 1
    Works great on macOS. Commented Jun 25, 2019 at 5:50
  • 3
    You can use this to run commands conditionally with the && and || operators: [ -f /home/user/file_name ] || echo "File not present" and also [ -f /home/user/file_name ] && echo "File is present" Commented Oct 20, 2019 at 17:41
  • 1
    @charlesreid1 's one does the job, thanks! Commented Nov 1, 2019 at 18:57
  • @charlesreid1 's one can be combined (if done in the correct order) to return the desired "yes"/"no" output: [ -f /home/user/file_name ] && echo yes || echo no
    – M. Justin
    Commented Feb 10, 2021 at 23:45
11

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

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

or:

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

(note that -f is for if file exists and is a regular file. See also this answer of mine to a related stackoverflow Q&A for more digressions about the meaning of exist here).

0
1

What about "inverting the logic" by:

file_exists = 1-system("test -f exists.file; echo $?")
1

Given the path-to-file do an:

ls path-to-file

If the file exists it will output the path to the file. If the file does not exist it will return nothing. If the path to file is a directory, it will return the contents of that directory.

1
  • Welcome to the site, and thank you for your contribution. Please note however that the question explicitly asks to print a custom message to stdout, as this is going to be used in a gnuplot program. Also be careful that depending on how the output is captured, the error message that ls prints if path-to-file does not exist may be captured, so it is not unconditionally true that nothing will be printed if the file does not exist.
    – AdminBee
    Commented Mar 15, 2023 at 16:28
0

This works:

pwsh -c Test-Path /etc/passwd

...if you have PowerShell installed. It returns either the string "True", if the path exists, or "False" if it doesn't. (Do I get "sarcastically unhelpful" points for this answer?)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .