How to search files & folders in Linux command line

If you're having difficulty locating files in your Linux distribution, a built-in command line tool can simplify the process significantly.

For those struggling to find files on their Linux system, there's a built-in command line tool that can greatly ease the task.

Having used Linux for decades, the command line has become second nature to me, yet I still prefer GUI tools for their superior efficiency compared to terminal-based options. Nevertheless, when it comes to finding files, I always rely on the command line.

While many GUI tools and desktop environments offer file searching features, I often find them lacking in effectiveness and prone to causing system slowdowns due to their indexing. Therefore, I stick to using a terminal window for file location tasks.

One downside of using the command line is that, unlike GUI tools where you can directly click or double-click to open a file, you only receive the file's location. This means you must manually open the file by starting the relevant application and navigating to File > Open. Nevertheless, I’m willing to accept this extra effort for the enhanced speed and precision that the command line offers.

How to find a file from the CLI

What you'll need: For this task, all you need is a running instance of virtually any Linux distribution. I prefer using the find command because it’s installed by default and is straightforward to use. Let me demonstrate just how simple it is to locate a file in Linux.

The basic use of the find command


Open a terminal window via your desktop menu. After the terminal application starts, type in the syntax for the basic find command:

find -name FILE
In the command shown, FILE refers to the name of the file you’re looking for. A couple of important details to consider: You must use -name to define a search pattern. While you can run find without -name, it won’t find your file unless you’re in the correct directory containing it. This restriction is usually not very useful, so always include -name in your command to locate the file no matter where you are running it.

Secondly, keep in mind that FILE is case-sensitive. So, if you’re looking for a file named MyFile but use find -name myfile, the command won’t find it due to the case mismatch. Also, the find command can only search directories you have permission to view. If it encounters a directory you don’t have access to, you’ll see a "Permission Denied" error.

A Trick: To make your file search more flexible, you can use wildcards with the -name option. For instance, find -name "*.txt" will find all text files in the specified directory and its subdirectories, regardless of their specific names.

A Trap: Be cautious with case sensitivity. If you’re searching for a file but aren’t sure of its exact case, you might miss it if your search doesn’t match exactly. For example, searching for find -name "myfile" won’t find MyFile or MYFILE. To avoid this, you can use -iname instead of -name for a case-insensitive search.

Suppose you're looking for MyFile.txt. You could run the command:

find -name MyFile.txt

But what if you have multiple files with similar names, such as MyFile.txt, MyFile.odt, and MyFile.rtf, and you want to locate all of them? In this case, you can use the * wildcard to simplify the search:

find -name "MyFile.*"

This command will search for all files that start with MyFile and have any extension, reporting their locations.

However, there’s a catch with this approach. Suppose you have MyFile.rtf in your home directory (~/), MyFile.odt in your Downloads directory, and MyFile.txt in your Documents directory.

If you run the command find -name "MyFile.*" from your home directory, it will locate MyFile.rtf and then stop searching. This is because find will search the directory and its subdirectories, but if it finds a match in the current directory, it may not continue to other directories unless explicitly told to do so.

To search all directories starting from your home directory, use:

find ~ -name "MyFile.*"

This will search through your entire home directory and its subdirectories, ensuring that it locates all instances of MyFile with any extension.

But if you're in the root directory of your system (aka /), and you run the find command, it will find all three files, with output similar to this:
./sufian/MyFile.rtf
./sufian/Downloads/MyFile.odt
./sufian/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 locating the first instance of the file. To avoid issues and ensure proper matching, it's a good practice to always use quotes around file names.

And that’s how straightforward it is to find files on Linux using the command line.

Comments

Popular posts from this blog

Remove browser ads on Windows startup using Windows Registry Keys | Remove & block annoying ads

What are some common mistakes bloggers make? and How to avoid the mistakes and become proficient is writing blog articles

Reddit communities for entrepreneurs and startups | Curated Roundup List