Today’s article isn’t going to be all that complicated, as we’re just going to cover one way to search for files by name. It is a pretty useful skill to have, and a skill that one should probably learn early on. If you want to learn how to search for files by name, read on!
So many of us will parse the output of the ‘ls’ command, but you really shouldn’t parse the output from ‘ls’. Sure, it’s nice and easy to use something like ls -la | grep <filename>
, but that’s a bad idea for reasons better explained by clicking the above link.
One of the options you can use instead of ‘ls’ would be the ‘find’ command. We’ve used it a few times before, but this time we’ll be using it to search for files by name. It’s relatively easy and I’m sure you can figure it out. The find command’s man page defines itself as:
search for files in a directory hierarchy
Which is a good description, because that’s what it does. Which is also good, because that’s what we want it to do! So, let’s get this article started rather than wasting time in the intro…
Search For Files By Name:
This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press
With your terminal now open, the command you’re looking for would look a little like this example:
1 | find <dir> -type f -name <file_name> |
As you can see, you must declare a directory and a file name. If you want some level of leeway, you use ‘-iname
‘ which indicates that it isn’t case-sensitive. As a matter of habit, I use ‘-iname
‘ instead of ‘-name
‘. This works for me and we’ll use ‘-iname
‘ from here on out.
Let me give you an example…
Say you wanted to find the ‘firefox.desktop’ file and ensure it’s there. You could use this command:
1 | find /usr/share/applications/ -type f -iname firefox.desktop |
Of course, if you aren’t sure it’s a desktop file, you can use:
1 | find /usr/share/applications/ -type f -iname firefox* |
The asterisk means anything, so you’ll get results for firefox.desktop as well as anything else.
If for some reason, you wanted to search your entire system for anything containing firefox, it’d look like this:
1 | find / -type f -iname firefox* |
That’ll likely throw a whole lot of errors, as you don’t have permission to look in those areas, so you just add sudo to the command:
1 | sudo find / -type f -iname firefox* |
Which should show you every instance of a file starting with firefox on the entire computer. The ‘find’ command can seem pretty difficult to tackle for a new person, but if you take it in small bites you’ll start to learn that it’s not all that complicated. Be sure to check man find
for more details.
Closure:
There you go… It’s yet another article. This time, we’ve covered how to search for files by name, a task that you may do with some frequency. If you know what you’re doing, it’s possibly faster than rooting around in a GUI hoping you find the right directory.
You don’t even need to know the entire file name to at least narrow it down a great deal. So, it’s a great tool to add to your toolbox and is more reliable than parsing the output of the ‘ls’ command.
Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.