Linux Essentials - The find command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to another episode of linux essentials in this video we are going to cover the find command it's one of my favorite commands i use it to find things when i can't find things on my linux systems and that's great because i'm constantly forgetting things i mean half the time i don't even know where i left my car keys and sometimes i even lose my train of thought in the middle of wait why are you here oh the video right we're talking about the find command in this video because with the find command i never lose anything on my linux systems and i love this command and i'm really excited to tell you about it and another thing that i'm also excited to tell you about is my sponsor for this video lenode lynode has been doing cloud computing since 2003 which is actually before amazon web services was even a thing on the nodes platform you can get your server up and running in minutes and they include all of the popular distributions such as centos debian ubuntu fedora and get this also arch linux and let's be honest what could be better than a linux cloud server provider that allows you to tell all of your friends i run arch lenod has multiple server plans available to make any app scalable and flexible you can use it to host a blog set up a vpn server a minecraft server or you could do what i did and set up a website for your youtube channel because the official website for learn linux tv runs on lenode and lenode offers 24x7 365 support regardless of plan size so you can get live help from a real person when you need it new users can get started right now with one hundred dollars in credit towards a new account and i highly recommend you check them out because lenote is awesome thank you so much to lenode for sponsoring this video and many others on learn linux tv i really appreciate it and now without any further hesitation let's get into the topic at hand and explore the find command now first things first the find command has a ton of different options that you can use so it's impossible for me to go over every option in one video and even though i'm not going to go over every option in this video the examples i will be giving you will give you all the information you need in order to start using this command and you'll be able to add it to your daily workflow so let's start with a very simple example of the find command so we'll start out by typing find and then what we'll do is we'll type the path where we want to start our search from and it could be any directory on the file system that you have access to and i want to start my search in my home directory so i just type the path where i want to start my search from and i can add some additional options here to help narrow down the search and tell the find command exactly what i'm looking for so let's add the option hyphen name so i'm telling the find command so far that i want to start my search in my home directory slash home slash j and i want to find objects by name and i'm going to search for any file that ends in txt the asterisk is a wild card it basically means anything so anything dot txt if i have more than one file that ends in txt in my home directory it should return more than one result let's see what happens and you can see that i have several files here in my home directory that end in txt now we can ignore the dot cache directory because i'm not really interested in that this is the only actual text file that i've created myself in my home directory or at least on purpose we have hidden directories that begin with a period as you can see here so i have this text file right here that i might use again in another example in this video but you get the point i was able to use the find command so i started it out with find i typed a path where i want to start my search from i use the option dash name i want to find things by name and this is the search criteria right here now how can i actually search my home directory and not have these hidden files show up here maybe i am not interested in things that are in my cache directory or my dot mozilla directory and i'm actually truthfully not interested in those so let's see how we can change the find command to take that into account and again here's the command that we've just used and the beauty of linux as well as unix is that we can chain commands together so i'll add the pipe symbol i'll pipe it into grep i'll add the dash v option and then i'll type dot cache let's see what happens so as we can see here we are not seeing lines of output that include cache because i changed the find command into grep and now i can withhold those particular results now what if you want to include results that are only of a particular type for example let's say that you are only interested in files or perhaps you are only interested in directories sometimes it's possible that you could have directories and files that are named similarly and you might see both in the results so i'll illustrate the problem first and then i'll show you how to solve it so i'll type find and what i'm going to do instead of typing the entire path to my home directory is i'll type a period because i'm currently in my home directory so for example if i do pwd i'm in my home directory so i'll type find and then dot because i want to start my search from right here and i want to find items that are named documents now notice that i'm not using a star so i'm not globbing anything here i want to find something that is exactly named documents so i'll press enter and two things showed up here and i set it up like this on purpose to trick you guys so let's actually look at the results in greater detail to understand what exactly you're seeing right here if i list the storage of my local working directory you can see that i have a documents directory right here so if i list the storage of that you can see that i have a file that is also named documents and as a reminder from a previous video when i went over permissions we could see in the permission string i have a d right here which means that this particular item is a directory and then here inside the documents directory i have a file named documents i did that specifically to trick you guys we have a hyphen right here which shows that it is actually a file so let's change the find command and only return files and not directories so here's the find command that we've just used and i'm going to add a brand new option here it's going to be dash type just like that and i'm going to add f to that option so i could do d for directory or f for file so with this example i want to find in my current working directory any objects that are exactly named documents and i want to return results only when the item is a file again that's what dash type is for so i'll press enter and you can see that it only returned the documents file that's inside the documents directory it did not return the documents directory itself for comparison if i take that option off you can see that i get two lines of output the documents directory itself and then the documents file that's inside the documents directory so this is just an example of the fact that you can actually narrow down the results based on the type of item that it is a file or a directory so what i'm going to do right now is show you guys another example and this time i'm going to add the exec option so i'm going to type find yet again and then dot i want to start my search where i am right now nothing new there and i want to find items that are of type file and the name is going to have to be documents and what i want to do is get rid of that file i want to delete it because it's very confusing to have a documents file inside a documents directory so i want to get rid of it now to be fair i could just use the rm command it'd be a lot faster but even though this example isn't going to be very practical it'll show you what the exact option actually does and we'll actually be using the exact option in at least one more example here coming up so i want to get rid of that file so i'm going to type rm open curly brace closing curly brace and then plus and don't worry i'll explain everything that this command is doing here in just a moment but let's execute it and see what happens okay well it doesn't look like anything has happened at all and i'll list the storage of my documents directory let's see what's inside and only one file is inside that directory so the documents file has been removed so let's explain this command right here so you can understand everything that's going on here first of all we have the find command itself you already know what that is and you know what that is as well and we've also gone over the type option already and we've gone over the name option as well exec means that we want to execute a command against every item in the results and the command that i want to execute against every item is the rm command i want to remove everything that shows up when i run this command so be very careful when you are chaining anything into the rm command because you could remove more than you think you are so this is probably one of those that you might not want to execute on a production server but anyway what i'm doing is i'm going to execute the rm command against everything that comes up in the results and only one item would have came up anyway because i only had one file that meets this criteria the curly braces that we have right here is a placeholder for the actual item itself so every time the find command finds something it's going to put that something in place of the curly braces so you could almost think of this as a for loop it's going to execute this against every item that it finds and the curly braces represent each item that comes up and the plus at the end is a terminator it's the end of the command we're basically closing each iteration off now another variation that you'll often see is slash semicolon like that a semicolon is another way to terminate a fine command but when you use a semicolon you have to have a slash in front of it just like you see here to escape it out and that's an issue with the shell i'm not going to get into that but basically you need to terminate the find command anytime you are using the exec option like i'm doing here so you can have semicolon or you could just add a plus just like that and either method is valid now let's see a more practical example and what i've done is i've added a bunch of files here to my laptop to serve as examples so inside my pictures directory i copied over this directory right here and this is from a nature trail walk that i went on last year i love going on nature trails one of my favorite things and i also love photography as well and inside that directory i have a bunch of files what i'm going to do is add the dash l option so you can see the permissions and there you go so let's say for example that i want to edit all of the permissions of everything under my pictures directory and i want to do it all in one shot now you don't actually need the find command in order to change permissions so for example i could use the chmod command that's the standard command that we use in linux to change the permissions on an object i'll use the dash r option because i want to change the permissions recursively i want the changes that i'm about to make to apply to everything from that folder where i start this command all the way down to everything underneath it what i want to do is make the permissions of everything to be six hundred i want to remove everything other than the permissions for the user itself and i have an entire video that goes over permission so if any of this confuses you don't worry about it you can check out that video if you want to learn more about permissions and i'm going to start this command from my pictures directory because again i want to change the permissions to 600 which effectively removes any access for anyone other than the user so i'm going to press enter and now we have a problem the permission string is completely gone here i mean i can't even see anything at all that's really weird so if i list the storage of my home directory we have the pictures directory right here but when i take a look at that i can't actually see what's inside that directory so let me go inside that directory to see if i could fix this problem except i can't why can't i here's my pictures directory i am the owner of that directory but i can't go inside that directory what is going on here well when i executed that chmod command i gave it a permission of 600 which actually removes the execute bit that would have been right here now 600 is fine for files you don't actually have to have execute on files but you do need to execute on directories in order to go inside of the directory so i have effectively locked myself out of that directory so what i'm going to do is run the chmod command again and i'm going to fix the permissions and then i'll show you how to do it with the find command which will avoid this problem in the first place i'm going to execute the chmod command and again i'm going to use the recursive option that's the capital r right there and i typed u plus x which means user plus execute so the user that owns this object i want to add the execute bit so i'll press enter and now we have the execute bit right here and now i'm actually able to see the permission string as well so so far so good but what's not so great is that i have execute on everything and that's not really a problem these are just pictures but i really don't like to have execute on things that aren't actually a program things that aren't actually something that i'd like to execute so what i want to do at this point is correct the permissions on everything but i only want to do it with one command so back in my home directory you can go ahead and get started and see how can we actually do this properly and this is where the find command comes in so i'm going to type find and i'm going to start the search in my pictures directory and i want to look for files only i don't want to return directories that was my problem i want to basically remove the execute bit in fact i want to remove the group access also from every file but i don't want to do this against folders which again will lock me out okay so now we're looking for files but how do we actually change the permissions on the things that the find command returns again we can use exec and what we'll do is exec chmod i'm not going to use the dash r option though and i want the permissions on all the files that this returns to be 600 i'll put in the curly braces and then i'll add a plus sign at the end so let's walk through this you already know what that is and you already know that i am starting my search in my pictures directory but i only want to return files because i certainly don't want to execute the chmod command against folders so i'm going to limit my search just to files and for everything that it returns i'm going to execute something and what i want to execute is chmod 600 then again we have the curly braces as a placeholder there and then we are terminating the find command now let's see if this command works any better so the permissions for a pictures directory that looks okay to me i have the execute bit there and i don't have access on group or world so so far so good let's go inside that directory then and list the storage now here i don't like these permissions but that's okay because with the find command i was specifically looking for files not folders so the find command wouldn't have done anything to this folder right here so inside there i have the correct permission as you can see right here on every single item if i wanted to run the same command and i wanted to run it against directories only i'll just recall it there it is i'll change this to d now change this to 700. those are decent permissions to have for folders in my own home directory so i just change this command a little bit and we are still good on the pictures directory and we can see that the permissions have changed on this item right here on this folder and we still have the correct permissions for the files and the thing is i actually prefer to use the find command to correct permissions because there's some permissions that you want on files and some permissions that you want on directories and you're often going to make them different so using the find command to correct permissions is a good habit to get into especially when you are managing a file server and what i'm going to do now is give you yet another example so in my var log directory i obviously have some logs here but a lot of them end in dot log and the thing is it's very common on a linux server that log files just become very large and they just eat up space so what i'm going to do is actually clear every log file that ends in dot log i'm going to empty them out in one command and maybe this is a situation where i need emergency space my server is going to crash it's a hundred percent full obviously i probably could have had the var directory mounted on a different partition and i probably don't really want to clear logs because logs have very important information in them so you might not want to be so quick to clear logs but sometimes if you have a situation where you just can't do anything because the disk is completely full and perhaps you want to clear log files then what are you going to do the find command is something that you can use for this purpose but what i'm going to need to do is run sudo first before i actually run the command because i need root level privileges for the items inside var log so again i'll execute the find command i'll start my search in the var log directory i want to do a type of f i want to return files and i want the name to be star.log so anything ending in dot log and what i want to do is clear them out so i'm going to execute something to clear them out well wait a minute this is actually a bad idea when you are dealing with a production server before you execute something what you're going to want to do is see what the results are first so that you are sure that what you are executing this command against is what you think you are going to be executing this command against otherwise you could actually clear things that you didn't mean to so we're going to test the command by not executing anything i want to see what the results are going to be so i'll press enter and because i use sudo i need to type in the password and right here we have a list of log files that are in that directory that match my requirements and i'm okay with this as you can see these are all files they're all log files they end in dot log and yeah there's files inside var log that don't end in log but we're ignoring that i'm okay with these results here so here's that command again so i'm going to execute something and what i want to execute is the truncate command dash s for size i want to truncate every file that comes up in the results to a size of exactly zero again i'll add the curly braces and i'll end the command with a plus sign as you see here and let's see what happened so what you can see is that the log files that came up in the results for example snapper.log the wpa log the zipper log they have a size of zero so the truncate command is able to truncate a file down to a specific size in this case i had it truncate the files down to a size of zero so it looks like the command worked now of course there's all kinds of different options that you can use with the find command there's actually quite a bit to it but what i've given you so far it just makes it easy to find something especially if you lost something sometimes you can even use it to simply get a list of files so for example i could find here in my home directory any file with a name that ends in dot mp3 i'll press enter and i have a bunch of files here and of course lacuna coil is my favorite band so any opportunity i can get to mention them i usually take it but that doesn't really matter the command worked these are the mp3s that i just happen to have in my music directory they show up right here so if i wanted to send someone a list of the results that's pretty easy i could just use the greater than sign and this is not specific defined results of anything can be piped into a file [Music] and i'll name the file results.txt and if i cap the contents of that file you can see that i have the same listing right there but that's just yet another example but i'll leave it up to you and your imagination to see where all you can use the find command and i'm sure that i forgot some very obvious examples of the find command but again there's so many different variations it's impossible to show them all to you but with what i've shown you so far i think you should have all the base knowledge you need to start using the find command in your daily toolset so as you can see the find command is awesome once you memorize even just a few of the options that it has available you'll be able to find things on your linux file system no problem so if you haven't already done so please subscribe to this channel i have some awesome videos coming very soon and definitely click that like button if you liked this video that helps youtube understand that you want to see more awesome linux content just like this thanks for watching [Music] you
Info
Channel: LearnLinuxTV
Views: 15,263
Rating: 4.9867697 out of 5
Keywords: Linux, Tutorial, Learn Linux, LearnLinuxTV, LearnLinux.tv, the find command, find command, linux find, linux find command, linux tutorial, command line, find linux, find tutorial, linux commands, linux terminal, linux tutorial for beginners, linux essentials, linux command line tutorial, find command in linux with examples, search in linux, linux search for file, linux find file, linux, cli, command line find, command-line, how to use find, sysadmin, how-to, find command in linux
Id: skTiK_6DdqU
Channel Id: undefined
Length: 25min 56sec (1556 seconds)
Published: Tue Apr 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.