The best thing about Linux is that it provides you with the ability to code and see the changes happening at the OS level. So, there could be a code that helps you create folders and files, the same way it will be done in the GUI. This time we are coding or for that way making small snippets of code for fun!
/* Code that checks if there is a file named “avantika.txt” anywhere in my system and gives me the location of the file*/
echo “Hey there Type in the file name or the approximate file name that you would like to search for”
read filename
echo $filename
find ~ -iname *”$filename”* | cat > pathfile
var=`cat pathfile | wc -l`
if [ $var = “0” ]; then
echo “Sorry there is no such file present in your system”
else
echo “Do you wish to see the content of the paths or save it in a file?”
echo “type 1 for storing the content in a file, type 2 to get them printed”
read choice
if [ $choice = “1” ]; then
echo “A file name pathfile has been created containing the path of the file. Cat the file and check the result”
else
if [ $choice = “2” ]; then
cat pathfile
else
echo “You have entered a wrong choice. Type either a 1 or 2.”
fi
fi
fi
This code allows you enter any of the characters that may be present in the file. So, for example, if you have a wild guess that the file name is something like “imp”, it searches your computer and lists down all the files along with their path that have a “imp” in their name.
UNDERSTANDING THE CODE
[1]echo “Hey there Type in the file name or the approximate file name that you would like to search for”
This is a basic introductory line. Something like, Hey 🙂 It is nice that you are using my code. Now, it also asks the user to enter the filename that he or she is interested in searching for.
[2]read filename
The line uses the command read. What this command does is, it allows the user to enter a file name and stores it in the variable filename.
[3]find ~ -iname *”$filename”* | cat > pathfile
This line is actually the basic of the entire code. It searches for the file named filename and stores it in a file named pathfile. Now, in case the file pathfile is not present, there is no need to worry. Because, cat > pathfile creates a file if it does not exists. In case it does exist, it simply overrides the content. So, everytime you have got the file with new content.
find command searches for the name of the file or for that matter the regex expression, like in our case. *”$filename”*. In case you put the * inside the parameters, it will take * as a string and not as the expression.
iname command searches for the filename irrespective of the case of the file. Simple terms, it ignores the case of the file name you entered.
~ symbol searches the entire system i.e. all the directories and sub directories present.
| symbol called the pipe symbol shifts the result of the find command to the command next to it.
cat > pathfile command stores the result returned by the file command in the file pathfile.
[4] var=`cat pathfile | wc -l`
Now, we wish to check if there is even a file present in the system. From now on, the commands written make sure that the user interactivity is maintained. Here, we are storing the number of lines that are present in the pathfile file.
[5]if [ $var = “0” ]; then
echo “Sorry there is no such file present in your system”
In case, the file does not have values, it simply means that find did not return anything and so there are no files present in the system by the name specified by you.
[6]else
echo “Do you wish to see the content of the paths or save it in a file?”
echo “type 1 for storing the content in a file, type 2 to get them printed”
read choice
if [ $choice = “1” ]; then
echo “A file name pathfile has been created containing the path of the file. Cat the file and check the result”
else
if [ $choice = “2” ]; then
cat pathfile
else
echo “You have entered a wrong choice. Type either a 1 or 2.”
fi
fi
fi
Now, if the file does have some content in it, you enter in this condition. We are trying to make it more user interactive. So, we ask the user if they wish to see the content right now or do they wish to just get it stored in a file for further processing.
read choice – This command basically stores the choice entered by the user in the choice variable. Now, if the choice entered is 1 we are asking the user to cat the file to check the contents. In case the choice is 2, we did the cat work for them.
Why the extra else?
Suppose, the user does not enter either 1 or 2, you don’t want the user dangling deciding what actually happen, right? Therefore, in case either of 1 or 2 is not entered, we tell the user to type the correct choice.
In the next post, we shall be refining this code more using the case function and some looping. Stay tuned for more!

Leave a comment