Categories
Linux Tutorial Series

Linux Tutorial Series – 72 – The find command

Here is the video version, if you prefer it:

The find command is used to find files. (Ward, 2014)⁠ There are a lot of options to find, but I remember it simply as this:

find dir -name file -print

where dir is a directory from where to start the search and file is the file name I’m looking for. To elaborate more on the dir part – find will find files in the directory dir or a subdirectory of dir, but it will not go upwards of dir. For example, if my find starts like this:

find /home/mislav …

it will not look outside the mislav folder, just “further down”. That means it is impossible for me to get a result such as /home/jellyfish/… .

-name is followed by the name of the file you are looking for. -print is used to instruct find to print the full file name on the standard output, followed by a newline. (“FIND(1),” n.d.)⁠

Some examples of find:

mislav@mislavovo-racunalo:~/Linux_folder$ find /home -name ab -print

/home/mislav/.cache/pip/wheels/ab

/home/mislav/.npm/_cacache/index-v5/ab

/home/mislav/.npm/_cacache/index-v5/fc/ab

/home/mislav/.npm/_cacache/index-v5/31/ab

/home/mislav/.npm/_cacache/content-v2/sha512/b9/ab

/home/mislav/.npm/_cacache/content-v2/sha512/ab

/home/mislav/.npm/_cacache/content-v2/sha512/b1/ab

mislav@mislavovo-racunalo:~/Linux_folder$ find /home -name ab.txt -print

/home/mislav/Linux_folder/ab.txt

mislav@mislavovo-racunalo:~/Linux_folder$ find /home -name 'ab*' -print | grep Linux_folder

/home/mislav/Linux_folder/aba.txt~

/home/mislav/Linux_folder/ab.txt~

/home/mislav/Linux_folder/aba.txt

/home/mislav/Linux_folder/ab.txt

/home/mislav/Linux_folder/ab2.txt

I always used find to find a particular file (such as some files I needed which relate to software development), so I never needed to use other find functionalities (and there are many). I found a good guide for functionalities of find I didn’t cover here: (“A find Tutorial and Primer,” n.d.)⁠, although again, other than memorizing this short line at the beginning of this post and using it, I never used anything else besides that.

“What’s the difference between locate and find?” you may ask. Well, the astute reader (and I hate when someone writes this as I always feel bad) will have noticed that locate operates on a database. So, if some file entry wasn’t added to the database locate operates on, locate won’t find it, but find will. So find is more powerful than locate in the sense that it doesn’t rely on a database which can be incomplete at a given moment. I have used find much more often than locate.

Hope you learned something useful!

References

A find Tutorial and Primer. (n.d.). Retrieved February 6, 2020, from https://danielmiessler.com/study/find/

FIND(1). (n.d.). Retrieved February 6, 2020, from http://man7.org/linux/man-pages/man1/find.1.html

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 20