File and Text Searches via SSH: Find, Grep, and Beyond

Learning Center
Tuesday October 10, 2023
Learning Center - Mastering Find and Grep


In the world of system administration and managing remote servers, the ability to efficiently locate files and search through their contents is indispensable. Secure Shell (SSH) offers a powerful set of commands for these tasks, enabling you to quickly pinpoint files or search for specific text within them. In this comprehensive guide, we'll explore how to harness the full potential of SSH for file and text searches. We'll delve into the find and grep commands, uncovering their various options and use cases. Additionally, we'll introduce alternative commands like whereis and which, which can be invaluable in certain scenarios. By the end of this guide, you'll have a deep understanding of these essential SSH tools and be well-equipped to navigate and search your server's file system and contents.

1. Locating Files with Find

Introduction to Find

In the realm of remote server management, the find command is your trusty companion when you need to hunt down files and directories. Find is a versatile and powerful tool that allows you to pinpoint specific files based on various criteria, such as file name, type, or modification time. This section introduces you to the basics of using find and its pivotal role in file searches via SSH.

Examples of Using Find

Let's start with a straightforward example. Suppose you need to locate a file named "myFile.txt" within the current directory and all its subdirectories. The find command comes to your rescue:

find . -name myFile.txt

This command instructs find to scour the directory tree starting from the current location (represented by the dot '.') for a file named "myFile.txt". It's a simple yet effective way to locate a specific file.

Using Wildcard Patterns

But what if you're not entirely certain about the file name, or you want to match files with similar names? You can employ wildcard patterns with find. For instance, to search for files starting with "myFile", you can use the following:

find . -name "myFile*"

This command employs the asterisk (*) wildcard to match any characters following "myFile", giving you a broader search capability.

Filtering Only Directories

While searching for files is valuable, there are instances when you need to filter out directories and focus solely on file locations. Find provides an option to do just that. If you want to list only directories and exclude files from the results, use this command:

find . -type d

By specifying -type d, you instruct find to display only directories in the search results.

Filtering by Modification Time

Another handy use of find is filtering files based on their modification time. If, for instance, you want to identify files modified in the last two days, you can use the following command:

find . -mtime -2

The -mtime option, followed by a negative number (in this case, -2), instructs find to search for files modified within the specified timeframe. This is just a glimpse of find's capabilities. In the following sections, we'll explore advanced techniques and unleash the full potential of this versatile command.

2. Advanced Find Techniques

Fine-Tuning Your Searches

The find command offers a myriad of options and techniques for refining your file searches. While basic find commands can help you locate files, you can fine-tune your searches to meet specific criteria or deal with complex scenarios. This section delves into advanced find techniques that empower you to perform targeted searches with precision.

Regular Expressions and Complex Patterns

One of the strengths of find is its ability to work with complex patterns and regular expressions, opening the door to more sophisticated searches. For instance, to find files with names containing either "report" or "document," you can use the -o (OR) operator:

find . -type f -name "*report*" -o -name "*document*"

In this example, we use the -o operator to combine two name-based searches. The -type f option ensures that we're only searching for regular files.

Size-Based Searches

You can employ find to locate files based on their size. For example, to search for files larger than 1 megabyte (MB), you can use the -size option like this:

find . -type f -size +1M

Here, the +1M argument signifies files larger than 1MB. You can modify the size value to suit your requirements.

Filtering by Owner and Group

Find also allows you to filter files based on their ownership. If you're looking for files owned by a specific user, you can use the -user option:

find . -type f -user username

SSH Find Command
SSH Find Command

Similarly, you can search for files owned by a particular group using the -group option:

find . -type f -group groupname

Time-Based Searches

Beyond modification time, you can perform searches based on access or change time. The -atime and -ctime options are available for these purposes. For instance, to locate files accessed within the last 7 days, you can use:

find . -type f -atime -7

The -7 argument represents the number of days. You can adapt it to match your desired timeframe.

Combining Criteria

One of the powerful features of find is its ability to combine multiple search criteria. Let's say you want to find files modified in the last 3 days and larger than 100 kilobytes (KB). You can merge the -mtime and -size options like this:

find . -type f -mtime -3 -size +100k

By applying these advanced techniques, you can precisely target the files you're seeking. The versatility of find makes it an invaluable tool for efficient file location in a variety of situations.

3. Searching for Text with Grep

The Power of Grep

While the find command excels at locating files, when your objective is to search within the content of those files, the grep command is your go-to tool. Grep is a versatile and potent command that enables you to scan the content of files for specific text patterns. In this section, we'll introduce you to the world of grep and showcase its capabilities in searching for text within files.

An Example of Using Grep

Let's start with a basic example. Imagine you need to find occurrences of the word "database" in a file named "configuration.php." Grep comes to your rescue with a simple command:

grep "database" configuration.php

This command instructs grep to search the content of "configuration.php" for the string "database" and display the lines that contain this text. Grep not only locates the pattern but also extracts and presents the context around it.

Searching Across Multiple Files

If you're uncertain which file contains the text you're looking for, grep offers a solution. By using the -r option, you can instruct grep to search recursively through all files under the current directory. Here's how:

grep -r "database" *

This command scans all files in the current working directory and its subdirectories for the string "database." The -r option ensures a thorough and recursive search.

Listing File Names

At times, you may only be interested in knowing which files contain the text you're searching for, without displaying the lines themselves. Grep provides the -l option for this purpose. For example:

grep -l "database" *

This command will display the filenames that contain the word "database" but will not list the lines themselves. It's a practical way to identify the files of interest.

Filtering Results from Other Commands

Grep's versatility extends beyond standalone usage. It can be combined with other commands, such as ls, to filter and refine results. For instance, you can use the following command to list files that contain the text "configuration.php":

ls -la | grep configuration.php

In this command, the output of ls is piped into grep, which then filters and displays only the lines containing "configuration.php." This technique is handy for pinpointing specific files among a long list.

Advanced Grep Options

Grep offers a wealth of advanced options, such as case-insensitive searching, using regular expressions, and specifying multiple patterns in a single search. To explore these advanced capabilities, it's advisable to consult the manual pages by typing:

man grep

In the manual pages, you'll find detailed information on grep's vast array of features and options. Grep is a formidable tool for text searches and can significantly expedite your data retrieval tasks. In the following sections, we'll delve into the mastery of grep, unlocking its full potential.

4. Mastering Grep Commands

Unleashing the Full Potential of Grep

Grep is a versatile tool that offers numerous command line arguments to fine-tune your text searches. In this section, we will explore advanced grep commands that will empower you to extract specific information from files, navigate complex text searches, and enhance your efficiency in locating text patterns.

Case-Insensitive Searching

By default, grep is case-sensitive, meaning it will only find exact matches. However, you can enable case-insensitive searching using the -i option. For example:

grep -i "database" configuration.php

This command will find all occurrences of "database" regardless of their letter case. It's a handy option when you need to be more flexible in your searches.

Using Regular Expressions

Grep's true power shines when you harness the capabilities of regular expressions. Regular expressions (regex) are patterns that describe sets of strings. They allow you to create complex search patterns. For instance, to find any line that starts with "error" in a log file, you can use:

grep "^error" logfile.log

In this example, the caret (^) symbol signifies the start of a line, and "error" is the pattern you're searching for.

Specifying Multiple Patterns

Grep enables you to search for multiple patterns in one command. For instance, if you want to find lines containing either "error" or "warning," you can use the -E option (for extended regex) and the pipe (|) operator:

grep -E "error|warning" logfile.log

This command will display lines that contain either "error" or "warning."

Counting Matches

To determine the number of matches without displaying the actual lines, you can use the -c option. For example:

grep -c "error" logfile.log

This command will output the count of lines containing the word "error" in the "logfile.log" file.

Showing Line Numbers

Sometimes it's helpful to know the line numbers where matches are found. You can enable line numbers with the -n option. For instance:

grep -n "error" logfile.log

This command will display lines containing "error," along with their line numbers.

By mastering these advanced grep commands, you can transform your text searching into a precise and efficient process. Regular expressions, case-insensitive searches, and pattern combinations become invaluable tools for handling complex data retrieval tasks.

5. Combining Find and Grep

A Powerful Synergy: Find and Grep

While find excels at locating files, it may not be the ideal tool for searching within the content of those files. Grep, on the other hand, is specifically designed for text searches but lacks file-finding capabilities. By combining find and grep, you can harness their respective strengths to perform comprehensive searches. In this section, we'll explore practical examples of combining find and grep to tackle complex tasks.

Locating Files and Searching Within Them

Suppose you want to find all files with names containing "log" within the current directory and its subdirectories. Once these files are located, you wish to search for lines that include the term "error" in each of them. The following command accomplishes this task:

find . -type f -name "*log*" -exec grep -H "error" {} \;

In this command:

  • find . -type f -name "*log*" locates files with names containing "log."
  • -exec is used to execute a command for each found file.
  • grep -H "error" {} performs a grep search within each found file for the term "error," and the -H option displays the filename in the search results.

By merging the capabilities of find and grep, you can efficiently locate files and simultaneously search within their contents.

Using Find and Grep for Time-Based Searches

Let's consider a scenario where you want to find all files modified within the last 7 days and then search within those files for a specific text pattern. The following command accomplishes this task:

find . -type f -mtime -7 -exec grep -H "pattern" {} \;

In this command:

  • find . -type f -mtime -7 locates files modified within the last 7 days.
  • -exec is used to execute a grep search for the specified text pattern within each found file.

This approach allows you to perform both time-based file location and text pattern searches in a single command.

Complex Patterns with Regular Expressions

By combining the power of find and grep, you can work with complex patterns that span both file names and content. Consider a scenario where you want to find files with names containing "log" and containing lines that start with "error" or "warning." This command achieves this task:

find . -type f -name "*log*" -exec grep -H -E "^(error|warning)" {} \;

In this command:

  • find . -type f -name "*log*" locates files with names containing "log."
  • -exec is used to execute a grep search for lines starting with "error" or "warning" within each found file. The -E option enables extended regex, and the -H option displays the filename in the search results.

Combining find and grep opens up a world of possibilities for complex searches that span both file and content criteria.

In the world of system administration, the synergy of find and grep is a formidable toolset that enables you to tackle intricate search tasks with finesse and efficiency. Whether you're dealing with file names, content, or both, this combination streamlines your search processes.

6. Alternative Search Commands

Exploring Alternative Commands

While find and grep are potent tools for file and text searches via SSH, there are alternative commands that come in handy in specific scenarios. In this section, we'll explore two such alternatives: whereis and which. These commands serve different purposes and can be valuable additions to your toolbox when searching for files and binaries.

Whereis: Locating Binary Executables

The whereis command is primarily used for locating binary executables and their associated documentation. It's especially handy when you're trying to find the executable file for a specific command, tool, or application. For instance, if you want to find the location of the Perl interpreter, you can use whereis like this:

whereis perl

The command will locate the Perl binary and display the full path(s) to it. This information is useful for identifying the executable's location and verifying its presence on your system.

Which: Identifying Executable Paths

The which command is another tool for identifying the full path of executable files. It helps you determine which executable is launched when you run a specific command or program. For instance, to find the full path to the Perl interpreter, you can use which:

which perl

This command will reveal the full path to the Perl interpreter binary.

Both whereis and which are valuable when you need to pinpoint the locations of binary executables. While find and grep are geared toward file and text searches, these alternative commands offer specific advantages for locating and verifying executable files and their associated documentation.


In the world of system administration and remote server management, the ability to efficiently locate files and search within their contents is indispensable. SSH offers a robust set of tools for these tasks, with the find and grep commands taking center stage. By mastering these commands, you can navigate your server's file system and retrieve specific data with precision and speed.

Check out the OpenSSH Project by OpenBSD.

Not a customer yet? Check out our website hosting plans.

Continue Reading

Join a growing community of thousands of developers and designers!

What will you build today?

Create an account