Saturday, April 2, 2022

[SOLVED] short way to check (one liner) if a file exists or not

Issue

I'm looking for a very short way to check if a file exists or not. If the file exists 1 should be printed and if not 0 should be printed.

The below syntax doesn't print anything? Just an empty output

if [ -f /proc/$pid/stat ]; then echo '1' else echo '0' fi

Solution

Try this:

[ -f file ] && echo 1 || echo 0


Answered By - gabor.zed
Answer Checked By - Robin (WPSolving Admin)