Is there somehow I can check if a file or folder exists in Ubuntu Terminal?
Asked
Active
Viewed 4.5k times
21
-
How can you mark a question as duplicate and point to a question that has -3 raiting?? – Dr_Zaszuś Oct 30 '19 at 12:17
1 Answers
39
You can use the test
command of the shell bash
.
$ test -e ~/.bashrc && echo file exists || echo file not found
file exists
$ test -e ~/.bashrcx && echo file exists || echo file not found
file not found
The command
help test
prints a help text with the different options, that you can use with the test
command.
You may also find the following help texts useful, as well as the links in a comment by @dessert,
help [
and
help [[
You can use the find
command if you don't know where the file is (so that you must search for it in several directories) or you want to find different versions of the file.
$ sudo find / -name .bashrc
[sudo] password for sudodus:
/etc/skel/.bashrc
/root/.bashrc
find: ”/run/user/1002/gvfs”: Permission denied
/media/multimed-2/test/test/2015-04/colour-prompt/home/guru/.bashrc
/media/multimed-2/test/test/2015-04/colour-prompt/root/.bashrc
/media/multimed-2/test/test/2015-04/colour-prompt/etc/skel/.bashrc
/media/multimed-2/rsync-bup/nio/.bashrc
/home/lfs/.bashrc
/home/myself/.bashrc
/home/nio/.bashrc
/home/sudodus/.bashrc

sudodus
- 46,324
- 5
- 88
- 152
-
2I'd recommend using -e instead. See https://askubuntu.com/a/970306/295286 – Sergiy Kolodyazhnyy Nov 09 '17 at 07:12
-
1Another notation of the
test
command is e.g.[ -e ~/.bashrc ] && …
, a very good manual can be found on wiki.bash-hackers.org.test
should not be confused with its modern counterpart, the conditional expression, e.g.[[ -e ~/.bashrc ]] && …
. The last is much more powerful in many ways, allowing the use of extended regular expressions for instance. – dessert Nov 09 '17 at 08:50 -
@dessert, I find it easy to understand and remember
test
as described in my answer, when checking for the existence of files. In other cases, for example inif
statements I use the syntax that you describe in your comment. I guess it depends on how I learned using these things ... – sudodus Nov 09 '17 at 08:59 -
2
-e
tests for raw existence,-f
and-d
additionally test whether it's a file or directory respectively. – dessert Nov 09 '17 at 09:01 -
@sudodus I just wanted to add this as you often see it in scripts etc. It may confuse people that
[
actually is a command exactly like e.g.mv
. The conditional expression by the way is not a command, which is why you can use&&
inside it – I really prefer this notation over-a
. – dessert Nov 09 '17 at 09:03 -
-
1There is also
locate
, which locates all files that have searched string in the name.locate
uses its own database of filenames. Search is much faster than find, but it can miss newly created files ifupdatedb
did not yet reindex the files.updatedb
is a program that scans the disk and updates the database of files. You can start it by typingsudo updatedb
. It is also executed regularly on its own (/etc/cron.daily/mlocate
). – nobody Apr 19 '18 at 12:07 -
-
There is also
ls
:ls /var/www 2>/dev/null 1>/dev/null && echo 'file exists' || echo 'file not found'
– rosell-dk Oct 04 '19 at 14:27