HomeTech PlusTECH & OTHER NEWSHow to search for files in Linux from the command line

How to search for files in Linux from the command line

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 find some 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 the find command 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.

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 use find without 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 -name to 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 command find -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 the Permission Denied error.

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 command find -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 the find 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. 

Source Link

Technology For You
Technology For Youhttps://www.technologyforyou.org
Technology For You - One of the Leading Online TECHNOLOGY NEWS Media providing the Latest & Real-time news on Technology, Cyber Security, Smartphones/Gadgets, Apps, Startups, Careers, Tech Skills, Web Updates, Tech Industry News, Product Reviews and TechKnowledge...etc. Technology For You has always brought technology to the doorstep of the Industry through its exclusive content, updates, and expertise from industry leaders through its Online Tech News Website. Technology For You Provides Advertisers with a strong Digital Platform to reach lakhs of people in India as well as abroad.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

spot_img
spot_img

CYBER SECURITY NEWS

TECH NEWS

TOP NEWS

TECH NEWS & UPDATES