How to search for a file using Bash Scripting!

We all love bash scripting, and every now and then we have to search for some files or use it as a condition in scripting. So we are here to teach you how to do so with the help of a short tutorial!

First of all the basics, open a file, and add the first line as:

#!/bin/bash

This tell the Linux system that this is a bash script, and now we can move on with out code.

function filexists {
   test -e "$1" -o -L "$1"
}

This function is used to determine the presence of a file. For the uninitiated, the way you declare functions in bash is just type: “function” and name it whatever you want! Then we test if the filename we specify when we call the function actually exists.

if ! filexists The_Witcher_3* ; then

Now, we call the function, specify a not statement and call the function, pass the filename to it and there we go! The “then” statement is there so that you can specify what you want to do after this condition is satisfied.

This is all for this tutorial! Let us know if you want any other short tutorials like this in the comments below! ( Or our Telegram group 😉 )

Leave a Reply