21

Is there somehow I can check if a file or folder exists in Ubuntu Terminal?

muru
  • 197,895
  • 55
  • 485
  • 740
Solo
  • 883

1 Answers1

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
  • 2
    I'd recommend using -e instead. See https://askubuntu.com/a/970306/295286 – Sergiy Kolodyazhnyy Nov 09 '17 at 07:12
  • 1
    Another 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 in if 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
  • @dessert, thank you for adding these helpful comments :-) – sudodus Nov 09 '17 at 09:04
  • 1
    There 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 if updatedb 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 typing sudo updatedb. It is also executed regularly on its own (/etc/cron.daily/mlocate). – nobody Apr 19 '18 at 12:07
  • @nobody, Thank you for adding this useful comment :-) – sudodus Apr 19 '18 at 12:29
  • 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