M HYPE SPLASH
// updates

Command to display an arbitrary message if a particular file exists

By Emma Valentine

Is there a command to display message “Yes” if a particular file exists? No need to give functionality if the file does not exist.

5 Answers

Use this simple Bash one-liner:

if [ -e FILENAME ] ; then echo Yes ; fi

The -e check evaluates to true if FILENAME exists, no matter what it is (file, directory, link, device, ...).

If you only want to check regular files, use -f instead, as @Arronical said.

5

You can use this simple script:

#!/bin/bash
if [[ -f $1 ]]; then echo "Yes" exit 0
else exit 1
fi

Save it as file-exists.sh. Then, in the Terminal, type chmod +x file-exists.sh.

Use it like: ./file-exists.sh FILE where you replace FILE with the file you want to check, for example:

./file-exists.sh file.txt

If file.txt exists, Yes will be printed to the Terminal, and the program will exit with status 0 (success). If the file does not exist, nothing will be printed and the program will exit with status 1 (failure).

If you're curious why I included the exit command, read on...


What's up with the exit command?

exit causes normal process termination. What this means is, basically: it stops the script. It accepts an optional (numerical) parameter that will be the exit status of the script that called it.

This exit status enables your other scripts to use your file-exists script and is their way of knowing the file exists or not.

A simple example that puts this to use is this script (save it as file-exists-cli.sh):

#!/bin/bash
echo "Enter a filename and I will tell you if it exists or not: "
read FILE
# Run `file-exists.sh` but discard any output because we don't need it in this example
./file-exists.sh $FILE &>> /dev/null
# #? is a special variable that holds the exit status of the previous command
if [[ $? == 0 ]]; then echo "$FILE exists"
else echo "$FILE does not exist"
fi

Do the usual chmod +x file-exists-cli.sh and then run it: ./file-exists-cli.sh. You'll see something like this:

File exists (exit 0):

➜ ~ ./file-exists-cli.sh
Enter a filename and I will tell you if it exists or not:
booleans.py
booleans.py exists

File does not exist (exit 1):

➜ ~ ./file-exists-cli.sh
Enter a filename and I will tell you if it exists or not:
asdf
asdf does not exist
1

In the bash shell on the command line.

if [[ -f /path/to/file ]]; then echo "Yes"; fi

This uses the bash conditional operator -f, and is checking whether the file exists and is a regular file. If you want to test for any files including directories and links then use -e.

This is a great resource for bash conditionals.

5

The shortest command for doing what you want is:

test -e FILENAME && echo Yes

test -e will test whether the name given exists in the file system. (You can use test -f to restrict to regular files only. See man test for more.)

If the condition given evaluates to true, then test returns a successful exit status (otherwise it returns a failure status). We combine the two commands using &&, which means "execute the next command if the previous command exited with a success status". The next command simply prints Yes on standard output; in the case of an interactive shell, on the terminal.

This avoids the additional textual material of an if statement, yet gives the same result. Using && (or its opposite, ||) to tie commands together works well when only a single command is involved. If you want to do more than execute a single command in response to a single command's exit status, then using the if syntax quickly becomes much more readable.

As already pointed out in other answers, the equivalent if style construct would be:

if test -e FILENAME; then echo Yes; fi

or alternatively:

if test -e FILENAME
then echo Yes
fi

For these purposes, [ and test are equivalent, except that [ demands a terminating ].

0

To skin this type of question there's multiple ways, and here's another one : use find command with -exec flag . The path to file can be split into two parts find /etc which sets directory and -name FILENAME which specifies filename (duh!) . -maxdepth will keep find working with /etc directory only and won't descend into subdirectories

adminx@L455D:~$ find /etc -maxdepth 1 -name passwd -exec printf "YES\n" \;
YES
adminx@L455D:~$ find /etc -maxdepth 1 -name passwd1 -exec printf "YES\n" \;
adminx@L455D:~$ 

Another way , via stat :

adminx@L455D:~$ stat /etc/passwd1 &>/dev/null && echo YES
adminx@L455D:~$ stat /etc/passwd &>/dev/null && echo YES
YES

And alternatively via python :

>>> import os
>>> if os.stat('/etc/passwd'):
... print 'YES'
...
YES
>>> if os.stat('/etc/passwd1'):
... print 'YES'
...
Traceback (most recent call last): File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/etc/passwd1'

Or a short command line alternative :

 python -c "from os.path import exists; print 'Yes' if exists('/etc/fstab') else '' "
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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