Register now for better personalized quote!

How to search for files in Linux from the command line

Jan, 23, 2024 Hi-network.com
group of penguins looking around
Mint Images - David Schultz/Getty Images

I've been using Linux for decades and, although the command line is second nature to me, I still opt to use GUI tools because they tend to be far more efficient than their terminal-based counterparts. There is, however, one task for which I always turn to the command line -- and that's finding files. 

Yes, there are plenty of GUI tools available for this purpose and many desktop environments integrate the feature into their application menus. However, I findsome of those options aren't great for locating files and their indexing can sometimes lead to system slowdowns. For these reasons I prefer to open a terminal window and locate the file in question.

Also: Two tricks that make using the Linux command line a lot easier

The one drawback to this method is that, unlike when using a GUI, once a file is found, you have to manually open it. The GUI tool will find the file and then allow you to simply click (or double-click) to open it. With the command line option, you'll be presented with the location of the file. Then you'll need to open the associated application and open the file via the application's File > Open menu. However, this manual process is a trade-off I'm willing to accept for the speed and accuracy of the command line tool.

Let me show you how to use the tool. 

How to find a file from the CLI

What you'll need:The only thing you'll need for this task is a running instance of just about any Linux distribution. I use thefindcommand for two reasons: it's installed by default, and it's easy to use. Let me show you how easy it is to find a file in Linux.

1. The basic use of the find command

Open a terminal window from your desktop menu. With the terminal app open, type the syntax for the basic find command:

find -name FILE

In the above command, FILE is the name of the file you're searching for.

Now, let me explain a couple of things. First off, you 'name' to match a pattern. You can usefindwithout name, but it won't find the file you're looking for unless you're in the right directory housing the file. That restriction, of course, is probably of little to no use, so always remember to add-nameto the command and it will locate the file in question, regardless of where you've run the command.

Also: Linux is not just for developers and command line pros

Second, FILE is case senstive. If you're looking for a file named MyFile and you use the commandfind -name myfile, find won't be able to locate the file. Third, the find command will only be able to search for files within directories you have permission to view. If find comes across a directory where you don't have access, you'll receive thePermission Deniederror.

2. A trick and a trap

Let's say you're looking for MyFile.txt. You could simply run the command:

find -name MyFile.txt

But what about if you have files named MyFile.txt, MyFile,odt, and MyFile.rtf, and you want to know where they're all located? In that situation, you could use the * wildcard, like so:

find -name MyFile.*

Now, find will search out all instances of MyFile and report back where they are.

However, there's a trap in this trick. Let's say you have MyFile.rtf in your home directory (aka ~/), MyFile.odt in your Downloads directory, and MyFile.txt in your Documents directory. 

If you're in your home directory and you issue the commandfind -name MyFile.*, the command will find MyFile.rtf in the home directory and stop looking. 

Also: How to install Linux applications from the command line

But if you're in the root directory of your system (aka /), and you run thefind command, it will find all three files, with output similar to this:

./jack/MyFile.rtf./jack/Downloads/MyFile.odt./jack/Documents/MyFile.txt

There's another way around this trap, where you can place the file name in quotes, like so:

find -name "MyFile.*"

Even if you're in your home directory, find will continue searching after it locates the first instance of the file. My advice is to get into the habit of always using quotes for file names.

And that's how easy it is to find files on Linux from the command line. 

tag-icon Hot Tags : Tech Services & Software Operating Systems

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.