January 19th, 2021
Hello everyone, today I'll try to explain the basics of find command and the bare minimum that you should know to use find command properly. I hope you'll like it, now let's dive right into tutorial.
As I explained in the former posts the most insightful resource that you can find about a Unix command probably its manual, let's take a look at its content and what we can say from that.
Now here we can see that defined as " walk a file hierarchy" and the description says "The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the ``primaries'' and ``operands'' listed below) in terms of each file in the tree." Now that basically means with find command we can search in all the subdirectories and files inside of a directory.
You can always look more from the official manual here, or you can basically run the man command yourself for the find and see what it offers more.
We have an example directory with some subdirectories and files for this post now let's take a look at it with tree command. If you are using MacOS it doesn't come built-in yet you can easily install it via "brew install tree". I'll not provide a GitHub repo for that because you can create some similar structure within minutes if not seconds yourself.
We can see that we have 4 directories and inside each of them we have different types of files to work with.
We can also achieve some similar result via our lovely find command :). Let's take a look.
As we can see now by calling the find with the directory choice of ours we can display everything inside and print them to standard output.
Let's take a closer look at the command we ran "find ." that dot stands for our current directory in Unix systems and since we didn't provide any other flags find automatically used -print argument and printed everything inside of the current directory, directories itself included with dot on the top. So we can understand that find also has some other arguments and -print is the default one.
Let's take a look at -name argument for example.
Now as we can see here above we basically found all the .txt files under our current directory, as you can see we can also use regex with -name argument we can run regexes like we did in this one. But maybe I'm a little bit ahead of myself, let's take a look at the most basic use of -name argument.
As we can see above we can just check for the full name of a file and get the full directory.
Now let's take a look at this one, we are now just checking if a folder name contains the string "text" and as a whole and ignoring all the other characters that came before or after it.
Another argument we can use is -type I'll quickly walk you through with -type because in my opinion it's quite straightforward and easier to pick up.
As we can see now "find ." command with displays only files for us and not the folders. Similarly we can do something like that for folders.
Now we only have folders nothing else.
Now let's move to a trickier one, -exec argument. With find we can execute other commands on the things we found with find let's have a quick look by combining some other flags with exec.
As we can see we have 2 lines of text here probably from other types of files and directories, why we have only 2 lines of text here, because I got lazy :).
Here what type does is it finds all files with -type f argument and executes cat command for each of them "{}" stands for all the things we found, we need to close -exec argument with ";" and we are using "\" for escape.
Now with adding one more filter we eliminated all the not ".js" files, speaking of not let's do something different.
With -not argument instead of looking for ".js" files we can look for the files that are anything but a ".js" file as we can see the results are supporting that.
The last but not least -delete argument, it's a useful argument too and makes find quite powerful, let's see what can we do with that now.
Now I left a ".txt" file with "txt" string inside the file name side and I just want to have "text" on that side let's say. What we can do is to check if that's the case by using regexes and then overwriting the -print argument with -delete.
Alright everyone, that was it for today. I hope you enjoyed it, I'm planning to make more Unix posts in the future, until then see you and stay safe.