I’ve lost my files, how can I find them?

We’ve talked about finding things in files and using grep and more to do that, let’s talk about finding files. I’ve lost my file and I can’t look through my file until I find my file.

And that’s what the command Linux® uses to find files is called. find. How’s that for imaginative naming? We’re going to find with find.

Let’s begin by cd‘ing into our home directory. Remember we can do that just by typing cd.

We want to find that file blog1.txt we created a while back. We know it exists, we know we put it somewhere, but we forget where. (It was in the home directory but let’s pretend otherwise for right now.) We tell find to start in the home directory and look for a file named blog1.txt. This is done by:

Alpha:~ computerlamp$ find . -name blog1.txt

See that ‘.’ after find? That’s really important. That tells find to start in the directory you’re in and look from there. The flag ‘-name’ tells the command ‘look for the file with this name’.

We can do other neat things. Suppose we know the file we’re looking for ends with the letters txt but we’ve forgotten if it started with blog, or blogg, or maybe bloggo, or even MyBlog. But we know it ends with txt, so that’s a start! In this case, we can do this command:

Alpha:~ computerlamp$ find . -name '*txt'

See that asterisk? That tells find to match any file that ends with txt and starts with anything else. It could be blog or bloggo, but it could even be thisfile.txt. Or turtles.txt. Or turtles-in-txt. It can match all of those and find will return every single one.

We can also find directories with find. Remember that Sewer directory we made a while back? find will find that as well:

Alpha:~ computerlamp$ find . -name Sewer

We can also use find to look into the sewer.

Alpha:~ computerlamp$ find Sewer

I didn’t give it a name flag that time. That means it’ll look into that directory and find whatever is in there.

 

Leave a Reply

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