Linux Format

Seek and ye shall locate

How to find files in Linux using Find, Locate and Grep…


Searching for files in a command-line Linux environmen­t can sometimes be a non-trivial task, especially when dealing with numerous directorie­s and large data sets.

Being able to perform these tasks via the Linux command line pays massive dividends, however, and makes you look good when you have mastered search. It also means that it is easy to automate the task and make it repeatable.

It’s important to remember that Linux is all about using the right tool for the right job well, and chaining tools together to achieve an outcome.

The powerful tools that can assist users in finding files include Find, Locate, and Grep. Let’s explore the basics and how to chain them together with pipes.

Finders keepers

Find is a versatile tool that searches for files and directorie­s in a hierarchy based on given criteria.

It’s particular­ly useful when you’re uncertain about a file’s exact location. To find all TXT files in the current directory and subdirecto­ries is quite straightfo­rward:

$ find . -name “*.txt”

Useful, but not awesome. Additional parameters extend it. Below is a useful example of the -type and

-size parameters. As an example, to find all files larger than 10MB in the /home/user directory, use:

$ find /home/sysadmin -type f -size +1M

Obviously, it goes without saying that you can use the redirect > and >> to output the console text to a file. An example is shown below:

$ find .txt -type f -size +1M > results.txt 2> /dev/null

Using the above puts all the files into results.txt and sends any errors to /dev/null.

Locate and behold!

While Find searches through the filesystem in real time, Locate uses an index, making it faster, though potentiall­y out of date. We admit this is one superusefu­l and speedy way to use files and is one of our go-to ways to locate files. To find all instances of a file called notes.txt, use:

$ locate notes.txt

Again, some switches can be used to fine-tune the output. To implement a case-insensitiv­e search for a directory named Projects, use:

$ locate -i projects

Pro tip: if you are looking for a file that matches a specific directory and filename, you can use:

$ locate bin/postgres

This would report only where there was a directory called bin that contained a file called postgres.

The Locate command relies on there being an up-to-date database. By default, it reindexes once every 24 hours. It is possible to manually rerun the indexing by using sudo updatedb . It should take very little time on most systems.

Grep expectatio­ns

While Find and Locate search for files based on metadata, Grep searches within files for a specific pattern. It’s a text search utility that can identify lines in files that match a given pattern:

$ grep [options] pattern [file...]

An example of this is using it to search for the word ‘Linux’ in a file called document.txt:

$ grep “Linux” document.txt

Most admins use case-insensitiv­e search to make sure all instances are picked up (unless it’s specific capitalisa­tion) for the word ‘Linux’ in all TXT files within the current directory:

$ grep -i “Linux” *.txt

Grep can also be used to recursivel­y search for the word ‘Warning’ in all files, starting from the current directory:

$ grep -r “Warning” .

Piping hot commands

These commands are great, but by combining them with others, it lifts the searching power substantia­lly. Pipes, represente­d by the | symbol, enable you to take the output of one command and use it as input for another. This makes it easy to combine the capabiliti­es of different commands for more complex tasks.

Going back to Find, we can use the pipe command to say that any log files that are larger than 10MB should have something done to them:

$ grep -r “Warning” . | tail -n 5

Using a pipe to push the output to Tail means that only the last five warnings are shown, rather than every warning in that folder.

As another example, if we want to archive those files found, it is possible to use the -exec parameter to do so. By default, the gzip command also removes the original, so proceed with caution.

$ find -type f -name “*.log” -size +10MB -exec gzip {};

Note the new -exec switch – it is a way to run a command on the output. To search for files smaller than 10MB, use - to specify ‘less than’, so -10MB could be used. Find also understand­s MB, GB and KB.

Linux provides a rich set of commands to aid in the task of file searching. Whether you’re looking for a file based on its metadata with Find or Locate, or you’re searching for a specific pattern within a file using Grep, Linux has you covered. Additional­ly, by harnessing the power of pipes, you can combine these commands in various ways to achieve your desired result.

Mastering these commands is crucial for anyone aiming to be proficient in Linux, because they’ll undoubtedl­y prove indispensa­ble in your daily tasks.

Newspapers in English

Newspapers from Australia