Find if a file exists or not in our system

Tux teacher

Well as we know the shell allows us to have extreme control of our entire system, despite its roughness and how primitive it is for many compared to modern graphical interfaces. But it must be taken into account that it is a much more optimal way of working, although it requires greater technical knowledge. The problem with the GUI is not only that it has some limitations compared to the terminal, but you are also running an extra layer for the graphics that consume a lot of resources that will not be destined for the final task.

We have been launching some Mini tutorials of some of the possibilities that can be obtained from some simple commands. This is another one, and as you can see, they are simple practices that can be of great help in many cases. Well, in this case specifically we are going to check how with a few simple commands we can see if a file or several of them exist in our system or not. The steps for this are as follows:

[ -f /etc/httpd ] && echo "Existe" || echo "No existe"

As you can see, we have used the evaluation expressions for it. Basically what we have done in the example, although you can replace it with many other options keeping the syntax, is to evaluate if / etc / httpd exists in our system or not. In the case of existing, since the -f option returns a true value if it exists, an "Exists" message will be displayed thanks to the use of the echo command. While otherwise it will show "Does not exist". As simple as that…

You can also substitute -f for Other options, like -e, which will return true if it exists, but does not evaluate if it is a regular file. With -r something similar will also happen, but in this case it evaluates if it is a readable file. To see if it is writable use -w, -x to see if it is also executable, and -d to see if it is a directory ... The possibilities are many. You can even use the character! to deny the actions. For example, to check that there is no / etc / test file:

[ ! -f /etc/prueba ] && echo "No existe"


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

2 comments, leave yours

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   fprietog said

    Ubuntu maintains a database by default to speed up file searches. That allows you to use the locate command.

    This database is updated automatically, although it can be forced to update with the sudo updatedb command.

  2.   asdf said

    By using this code, are we using bash programming?