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.
-
18
-
@ZebMcCorkle Correct, that works too. You might post that as separate answer.– Byte Commander ♦Jun 14, 2016 at 18:28
-
3If
FILENAME
is a variable expansion, or contains any special characters, then don't forget to quote it. e.g. in a script,[ -e "$1" ] && echo Yes
. Jun 15, 2016 at 11:12 -
@PeterCordes if you're using bash you can use
[[
and]]
instead of quoting the variable.– HollowayJun 15, 2016 at 14:47 -
1@Holloway: yup, but sometimes you need to write portable scripts that only require POSIX sh. That's why I gave a safe version of this instead of downvoting it and upvoting the bash answer. Also note that it's not a problem to quote expansions inside
[[ ]]
, always quoting is a good habit for beginners that don't remember the special rules for inside the[[ ]]
operator, or for arithmetic contexts. Jun 15, 2016 at 14:53
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
-
2Good, but Thou shalt quote variable references like
"$1"
. If it has white space in it, it will break without quotes and other bad things can happen.– JoeJun 15, 2016 at 22:33
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
.
-
1
-
1I'm not a fan of
test
in it's original format, I know it's overkill, but it's habit of mine. Jun 14, 2016 at 17:07 -
7@ByteCommander: double braces are just generally better than single braces. They're part of the shell syntax (instead of invoking the
[
command, which usually calls a builtin.) They correct inconsistencies and dumb behaviors of the single brace version. The [ builtin remains for compatibility, but I don't see any good reason to use it in new bash code. (If you need to write POSIX-compliant scripts that can run on ancient shells or something, that's different.) Jun 14, 2016 at 19:13 -
@kundor: Mostly agreed, though, to be fair, "
[
usually calls a builtin" is dubious.type -a [
for me lists "shell builtin", then "/usr/bin/[
."– wcharginJun 16, 2016 at 0:37 -
1@wchargin: I'm afraid I don't understand what you're getting at. Your
type
results seem to corroborate what I said, no? Jun 16, 2016 at 4:27
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 ]
.
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 '' "