10S of projects, hundreds of folders and thousands of files. Does this scenario sound familiar? If the answer is yes, then you probably found yourself more than once in a situation where you couldn't find a specific file. The find command will help us locate any file in our project, and much more. But first, for creating a quick playground. Let's download the electron open source project.
For this we use git clone. And now we CD into it change directory. We see here lots of different files and folders, just like in any normal sized software project. In order to find a particular file, let's say packaged package dot JSON. We will use this command the first argument Where should find search, in this case in the current folder, and the second argument is the file name to search for. And of course, the file name.
If we were to look for all read README files in the current project, the previous command format is not helpful. We need to issue a case insensitive find for demonstration purposes, we will also create a readme.md file with our cheat in our created and use this argument for case insensitive search. Okay, you see here that both readme.md and big uppercase readme.md have been found. Now, if we were to search for all JavaScript files, we would use find with these arguments, and as you can see, there are quite a few results. For narrowing down our results. Let's limit the fight to the Default app folder.
Okay, as you can see, there are only two GS files in this folder. And if we were to find all files that are not JavaScript, just add an exclamation mark before the name argument like this. You can see here all files that don't end their name with JavaScript. If we were to look for all the nodes in the directory, which are of type file, we would use this arc. And in the same way, with this one, we find all directories in a specific location. Find can also locate files based on time identifiers.
For example, in order to find all files in the you user shared directory that were modified in the last 24 hours, issue the following command and on My computer at the time of this recording I have quite a big list. Using m time modified time broadens the list even more. If we were to find for example all the files modified in the last hour, we can use modifying minimum 60 minutes. This good folder to search is slash local slash share. If we use mean, modify minimum, 90 minutes the least broadens again. Fine can also show us the list of files accessed in the less than four hours by using the access time Minus one argument like so.
While working with lots of project files, it is sometimes the case that some project remains empty and we forget to delete it. In order to locate all empty files, just do a find. Minus empty. And as we can see electron has a few empty files. Find will also show us empty directories or links. Removing empty files will keep our project clean.
But when it comes to reducing size, we sometimes want to know which files are taking up most of the space. As we said in the beginning, find can do much more than locating files in our project. Using the exec argument it can be combined with almost any other command, which gives it almost infinite capabilities. For example, if we want to find all JavaScript files that can be In the text manager, we can combine find with grep like so. This will execute the grep command on all files returned by find. Let's also search inside the file using vim so that we verified the result is correct.
As you can see, the text manager appears in this file. Moving on with the practical examples, let's say you have a folder where you want to remove all the files modified in the last 100 days. We can see our default app folder contains such files. So if we combine fine with remove like so we can do a quick cleanup. Find the can be used for Smart backups. For example, if we were to backup all JSON files in the project, we would combine find with the CPO backup utility using a pipe and a standard output redirection.
Sound sounds complex, but it isn't just check this command CPU sounds like Star Wars snake. We can see that in this command, we created a backup dot CPU o file of type CPU and archive. Now this could probably have been written with exec also but it's critical you understand that pipes can be can also be used in this type of scenarios to gather with redirects. When you're doing reports, you may have to count the numbers of lines written. In order to do this, we combine fun With a word count, like this, this will give us all JavaScript files and the number of designs. We can pipe this to cat.
Here we go to only output the number of lines and then pipe to the paste command, we do this. This will merge all our lines with a plus sign as a delimiter. This of course can be translated to an arithmetic operation, which we can calculate using the binary calculator. Here we go, long line. And this last command will tell us how many lines our JavaScript files contain. Of course, these are not actual lines of code as they can be empty lines, or comments.
For precise calculation of lines of code. You can use this lock utility In order to mass rename files like changing the file extension name to note for all JavaScript files, we can use this command. You can see the Rename syntax is quite similar to set. And there are no more JavaScript files left because they have all been renamed to the suffix note. Some software projects require all source code files to have a copyright header. As this is not required in the beginning, oftentimes we can find ourselves in the situation that we have to add copyright information in the beginning of all our files.
In order to do this we can combine find with said like this. Why this is basically doing is telling the computer to find all node files and add the copyright notice in the beginning of each file, followed by a new line. We can check one random file. And yes, the copyright notice is there. As you can imagine, find has lots of use cases. The examples I've shown you are only the first piece of the pie.
Learning find along with set and the Jeet Creek can set you free from your EDA, when it comes to finding refactoring or working with git, which means you can more easily switch from one either to the other because you don't have to learn all the features. You just use your friendly CLI tools