Linux/Mac Terminal Tutorial: How To Use The find Command

Linux/Mac Terminal Tutorial: How To Use The find Command

Views:50650|Rating:4.97|View Time:17:45Minutes|Likes:1547|Dislikes:8



In this Linux/Mac terminal tutorial, we will be learning how to use the find command. The find command allows us to scan through our file system in order to find files and directories that meet a certain criteria. We will also learn how to perform actions on the results that are Let’s get started.

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Linux #Mac

hey there how's it going everybody in this video we'll be learning how to use the fine command from within a Linux or Mac terminal now the fine command is a powerful tool that allows us to quickly and easily scan through our file system to find files and directories that meet a certain criteria and we can even perform actions on those results that we get back now the reason I wanted to walk through how to use this tool is because sometimes I see people who write scripts that are more complex than they need to be whether that's in Python or another language when really what they are trying to do can sometimes be done a lot more easily using these command line tools so let's go ahead and look at some examples so just like any of these commands if you look at the manual page by typing in man and then the name of the command in this case it's fine now this will show us all of the options and different ways that we can use this command but we're going to go over a lot of these here in this video so right now I'm currently in a test directory where I've created some sample files and directories now if I wanted to find all the files and directories starting in this current directory then I can just say fine and then a dot and that dot signifies the current directory now if I run this then you can see that we get all of the files and directories below the current directory that we searched and you can see that it even searched down through some of the subdirectories here now if I wanted to find all of the files and directories within a specific directory then I can just replace that dot with a directory name so if I wanted to find all the files and folders within my website demo directory then I could just say find website demo and if I run that then you can see that it returned all of the directories and files within that website demo directory okay so now let's start filtering out some of the results so let's say that I wanted to find only the directories and exclude the files now to do that I'm going to do this in my current directory so I'm going to say find within my current directory and I'm just going to say type D and this will find all the directories and no files so there you can see that I got a result of all of my directories below my current directory now if I wanted to find all of the files and no directories then I can just replace that D with an F now if I run this then you'll see that it returns all of the files and no directories okay so now let's say that I wanted to find a file with a specific name now I know that there's a file below my current directory somewhere called test underscore one dot txt but I can't remember exactly where that is so let's go ahead and search within the current directory for this file and it will search in all the sub directories as well so we want to do a find within the current directory which is that dot a type with an F which is going to find all the files and then I'm going to do an option of name and that name was test underscore one dot txt so you can see when I search for that exact filename that it returned this one result within this testdir directory okay but sometimes you don't know the entire file name exactly what if I knew the file started with the word test but I couldn't remember the rest of it so if I wanted to find all the files that started with the word test and you can use an asterisk as a wild-card so instead of searching for the specific file name here I'm just going to say test and then that asterisk there to do a wild-card now if I run that then you can see that it returns all of these files that start with the word test now there are some files that start with the word test that it didn't return and that's because some of them have capital letters and this name option here is case sensitive now if I want it to be case insensitive then instead of typing name I can use I name so I'll do an i name search instead and if I run that now you can see that it gets files that start with test and it's case insensitive so it returns the ones that have capital letters in them also now that wildcard that I showed you sometimes that is useful in order to find files with certain extensions so let's say that I wanted to find all Python files below my current directory then I could just say star dot PI so you can see when I ran that that it did return some Python files within some of these subdirectories here and this star just means anything and then this dot py it's saying return anything that ends in this dot py now we can also filter files and directories based on their metadata and that can be extremely useful so let's say that we wanted to find all the files modified in the last ten minutes so I can do the find type file and now I want to find all the ones modified in the last ten minutes so I'll say M min – 10 so when you will use this minus sign right here it's saying to find files that were modified less than 10 minutes ago and we could use the plus sign to find files that were modified more than 10 minutes ago and you can see that this didn't return any results because none of these files have been modified in the last 10 minutes but if I create a new file here so I'm just going to say new one dot txt and then I rerun this command to find files that were modified in the last 10 minutes then you can see that it found that new file that we just created now all of the other files were modified more than 10 minutes ago but just to show you what that looks like I can do find files that were modified more than 10 minutes ago so if I run that you can see that it returns a lot more here because all those were modified more than 10 minutes ago now you can actually combine multiple of those time searches together so let's say that I wanted to find a file that was modified more than one minute ago but less than five minutes ago so I can just put in this same option here and this says more than one minute ago and less than five minutes ago so if I run that then you can see that it returned that new file that we just created because it was more than a minute since we created that file now sometimes working with minutes isn't exactly convenient because if you have something where it's been days that you want to search then you don't want to calculate up all the minutes for that now if you wanted to see files that were last modified a certain number of days ago then instead of that m minute option we can use this m time option so I'll do em time and I'll say less than 20 days ago and just like with the minute searches if we wanted to then we could make this more than 20 days ago also but you can see that that returns no results because none of these were modified more than 20 days ago now I've been using M min and M time for modified minutes and modified days but you can also use a men and a time for access minutes and access days and you can also use C min and see time for changed minutes and change days ok so another useful option is to be able to search by file size so let's say that we had some files eating up our disk space and we didn't know exactly where those were well we could run a search and try to find all the files over a certain size so let's say that I wanted to find all the files that are over 5 megabytes so in order to do that I'm going to find everything I'm like under my current directory and then that's going to be the size option and we want to find over 5 megabytes and that's how we would find those so if I run that you can see that that returns one result now you can see that this is similar to the same syntax that we use to search by time except now we have this uppercase M here to signify megabytes now we could also use a lowercase K 4 kilobytes and we could use an uppercase G 4 gigabytes and just to prove that that file is over 5 megabytes and I can actually go ahead and search all of the files in this directory and if I run that you can see that our search returned this wallpaper dot jpg and if we look at the size of that it's actually 7 megabytes and we have two more pictures in here that are under that 5 megabytes so those didn't get returned by that fine command ok so another common search that I like to perform is finding any files that I have created that are currently empty now listen come in handy if you've created a bunch of test files that are just lingering around that don't actually have any data so to do this we can just find all of the files and we can just tack on this in the option here and if I run that then you can see that a lot of these files are empty because I just created these to do this walkthrough okay so one of the last filters that we're going to look at is how to search based on permissions and I do this a lot especially when working with websites and checking to make sure that certain permissions are what they should be and we could do that using the – PE RM option so I'm going to do a find within the current directory – PE RM for permissions now I'm going to do a search on all files and directories that have permissions of 777 and you can see that within this website demo directory that a lot of our directories and files have that permission of 777 ok so now that we've looked at several different ways that we can filter our results let's look at how we can actually perform some actions on our results so this is where this becomes extremely useful so let's say that we found a lot of permissions here within our website demo directory that we're in correct and that we wanted to change these so currently everything is set to 777 which gives anyone read write or execute permissions so first we want to change the user and group for every directory and file in our folder and then we want to set all of the directories to have a permission level of 775 and all the files to have a permission level of 664 now that might sound like a lot of work and if you did this manually then it could take an extremely long time now this is sometimes where I see people who write overly complicated scripts to do something like this but we don't have to write something like a Python script for this if we know how to use these terminal commands properly so we're going to do this everything that we want in three easy steps so first of all we wanted to change the user and group for every file and directory within our website demo folder so remember find will return every file and directory by default so if we run find on the website demo directory then this will return every file and directory within that folder so now if I want to set the user and group on all those results then we could use this exec in here to execute a command on those results now the command that I want to execute is this CH own and that will change the owner of each result so for the user here I'm just going to type in Corey Schaeffer as the user and for the group I'm going to make this wwa de so now if we were to run this command on a normal file or directory' then this is normally the place where we would put in the name of that file or directory but since we are getting these results through the fine command then we need to put a placeholder here instead and the placeholder for these commands they are just these curly braces and now all we have to do is end our command so we can either put in a plus sign or we can put in a backslash semicolon now I like to use the plus sign but that's just personal preference whatever you want to use is completely up to you so now if I run this then you can see that it looks like not much happen but we also didn't get any errors so now if I do an LS l a on our website demo directory then you can see that our user and group work change for all these files and folders now these two dots here are just the parent directory so that's why they still have the old group so that's fine so now let's say that we wanted to set all of the directory permissions to 775 and all the file permissions to 664 and this time I'm not going to go as in-depth just so you can see how quickly we can run these types of commands so first of all we want to so first I'm going to go ahead and delete the command that we just ran now we want to find all of the directories so I'm going to say type D and now I want to run a command on all these directories and that command I want to be changed mod which we can use to change permissions and I want that to set all of those directories to 775 I'm going to put in my placeholder for all the results and then a plus sign to end that command so now if I wanted to see if that worked that I can do a find within that website demo directory and I can search for permissions of seven seven five if I run that and you can see that returned all our directories so our directories within website demo did get those new permissions of 775 okay so now let's change the file permissions to 664 I'm just going to pull up this old command here and instead of the directories I'm just going to go ahead and find the files and instead of 775 I'm going to change these to 664 and we can run that okay so just like we did with our directories now let's search for the permissions of 664 and make sure that all of those were set correctly and you can see that when I run that that it returned all of our files underneath that website demo directory so they all have those new permission levels of 664 ok so now let's look at a slightly more complicated example so let's say that I wanted to delete all the image files in my current directory that ended with a dot jpg extension now first let's go ahead and just build up the command so I'm going to do a find within my current directory and I want to find a type of F which is a file and I want the name to be star dot jpg now see this is important here because if you're doing something like the leading or modifying files then you should always run the find command to see what your results are before doing anything to those results because you might not be removing the files or folders that you expect so running the find command first is like a dry run that allows you to see the results that you'll be working with so you can see when I ran that command that our results aren't what we wanted because I only wanted to remove all the files in the current directory I don't want to delete anything in the subdirectories and you can see here that our fine command returned all of the JPEGs from our website folder as well now this is a mistake that people make sometimes so it's important to be careful so instead to find only the files in our current directory then we can set this max depth option so I can set a max depth here and I'm just going to set this to 1 now setting a max depth of 1 means that you're only going to search down one directory which is the current directory so you can see when I ran that that it got our correct results and that it excluded the matching files within our sub-directories so now if we wanted to delete these files then we know that fine is returning what we expect so we can just bring back up that command and now we can just add on to the command what we want to run so we want to execute an RM and then put in our placeholder for those results and then just end that with a plus sign and if I run that and now if I do an LS within this current directory then it deleted all of those JPEG images within the current directory so you can see how these fine commands can be extremely detailed as to the results that you're getting back and allow you to execute certain commands on those results so this can be extremely useful in your project so for example sometimes I'll see Python programmers who maybe want to clean up any other pyc files that they have lingering around and I sometimes see questions where people ask how to delete those and it would be as easy as running a command like this except instead of a star jpg you could do star dot pyc and you can set that max depth to any level that you wanted or you could take it out all together and that would delete all of the pyc files in your entire project okay so I think that's going to do it for this video like I said there's a ton of different things that you can do with this fine command if you just take the time to play around with it now if you get good with commands like this then it's going to save you a lot of time so like I said instead of writing complicated scripts to do these exact same things you can just run a quick terminal command to do exactly what you want now if you do have any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those now if you enjoy these tutorials and I'd like to support them then our several ways you can do that the easiest way is to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone you think would find them useful and also if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you

32 Replies to “Linux/Mac Terminal Tutorial: How To Use The find Command”

  1. Jacob Foster

    Quick question, why is it necessary to add the plus sign or /; at the end of the command and not just hitting enter or even just a regular ; without the backslash?

  2. Brian Ribas

    There are some commands that I was not able to use them. I know this video is from 2016. Should that be it? I don't know if the terminal updates with the mac's software.

  3. Farhanking7864

    I literally learnt so much through this video. It covered bash commands, made me look up all the options on the man pages, showed a little wildcard, thank you so much

  4. Egawati Panjei

    Your channel is a treasure!
    I found your channel a month ago and have been watching your playlists on python, git, sublime and linux terminal tutorial. Thank you so much sharing your knowledge!

  5. Giuseppe Mezzofanti

    This inconsistency (different behavior: -R for "rm", and manxdepth for "find" on the other hand) is very bad design, IMHO. Because it confuses.

  6. Sajith S.K

    Awesome !! A quick question
    How did you change the prompt ? so that while you type the command title always stays top "coreyschafer at Coreyss-imac in Commands-find"

  7. Michal Przybylowicz

    There is a difference between the {} and + sign:

    {} Is a placeholder for the result found by find
    + All result lines are concatenated and the command is executed only a single time with all found results as a parameter

    You can check this Yourself by using find with ls -lh …

  8. Deepak Singh

    In case anyone curious, by changing semicolon (;) to plus (+) in -exec rm {} ';' make the command efficient since by doing so command (in this example rm) run only once . In short, + sign activate the ability of
    find to combine the results of the search into an argument list for a single execution of
    the desired command.
    btw another great video #Corey.

Leave a Reply

Your email address will not be published. Required fields are marked *