How to search for text within files in Linux using the terminal

Last update: 18/03/2026
Author Isaac
  • Grep is the primary tool in Linux for searching for text patterns in files, with options for recursive searches, ignoring case, or displaying line numbers.
  • Find complements grep by locating files and directories by name, type, date, or size, and combines with grep using -exec for advanced searches.
  • Awk and sed allow you not only to find text, but also to process and transform it by fields or through substitutions, automating analysis and editing tasks.
  • Ack offers a streamlined alternative to grep for code projects, while graphical search engines make basic searches easier without using the terminal.

Searching for text within files in Linux

Working with many files on a GNU/Linux system without good search tools can be a real headache. This is especially true when the project grows or when you have to review huge logs. Being able to search for text within files in Linux from the terminal It makes the difference between wasting time and being truly agile.

The good news is that Linux comes loaded with utilities designed precisely for this. Commands like grep, find, awk, sed or ack allow you to locate words, phrases or patterns in virtually any file on your system, combining simple searches with very powerful regular expressions.

grep command: the classic tool for searching text in Linux

The core of most text searches in Linux is The grep command, a command-line utility designed to find patterns within filesIts name comes from "global regular expression print", and that already gives you a clue: it is made to work with regular expressions and show all the lines that match what you are looking for.

To put it in more practical terms, grep receives a search pattern (plain text or regular expression) and one or more files or directoriesIt reads the contents and writes matching lines to standard output. You can use it in development projects as well as for reviewing configuration files or system logs.

The basic syntax is very straightforward, and it usually follows the same pattern: grep "text_or_pattern" file_or_pathFrom there, it's all about adding options as you want: recursive searches, ignoring uppercase letters, showing line numbers, etc.

Basic use of grep on specific files

To start with the simplest example, imagine you want to locate the word "prices" within a specific PHP file of a web projectFor example, a Laravel controller:

grep "prices" ./app/Http/Controllers/PricesController.php

That command will read the specified file and will display all lines that contain the string "prices" exactly as writtenrespecting uppercase and lowercase letters. If you work with code, you'll often want to know on which line each match appears.

To achieve this, simply add the option -nso that the result will include the line number before each match:

grep -n "prices" ./app/Http/Controllers/PricesController.php

In other cases, you might be interested in the exact opposite: View only the lines that do NOT contain a specific word.For example, all the lines in a controller where the statement does not appear. use.

That's where the option comes into play -vwhich reverses the selection criteria and excludes matches:

grep -vn "use" ./app/Http/Controllers/Controller.php

Ignore uppercase and lowercase letters in your grep searches

When you are looking for a word that can appear in different variants (for example home, Home, HOME or similar combinationsIdeally, you should forget about the distinction between uppercase and lowercase letters and focus only on the text.

For this, grep has the option -i, which forces a case-insensitive search on the specified file, regardless of how the word is spelled:

grep -i "texto" nombre-del-archivo

If you want to apply that same approach to a web project file, you could, for example, Search for the word "home" in a Laravel controller without worrying about the case combining -i and -n:

grep -in "home" ./app/Http/Controllers/HomeController.php

It is also very common to use -i along with recursive searches, to scan an entire project at once in search of a specific word or name that you may not remember exactly how you spelled it.

Recursive searches with grep in directories and subdirectories

True power comes when you no longer limit yourself to a single file, but You run grep on an entire directory and let it iterate through all its subdirectoriesThe following options are used for this purpose: -r o -R.

In its simplest form, you could start by searching for a word within all the text files in your home folder, for example the string "text" in the .txt files of /home:

grep "texto" /home/ *.txt

If you want grep to also navigate down into subdirectories, Add -r to activate recursive search and you keep the .txt extension filter:

grep -r "texto" /home/ *.txt

The moment you don't care about the file type and you want find the word "text" in any file within /home and its subdirectoriesSimply indicate the route:

grep -r "texto" /home/

Something very similar applies to development projects. If you work, for example, in the directory ./app/Http/Controllers and you need to locate the word "index" in any controller, you can use:

grep -rn "index" ./app/Http/Controllers

In this case -r makes the search recursive and -n adds the line number to each resultIf you only want to know which files contain the word, and you don't need to see the line, you can use the option -l to list only filenames:

  How to open a PDF directly in Word and edit its text

grep -rl "index" ./app/Http/Controllers

Differences between -ry and -R, exclusions and practical examples

In addition to -r, grep offers The -R option, which also performs recursive searches but follows symbolic linksIt's a subtle difference, but in systems with many symbolic links to other directories it can significantly change the behavior.

In a typical user environment, you might search for the word "text" within any directory in your home So:

grep "texto" ~/directorio

If you want it to crawl included subdirectories, you could use either of the following:

grep -R "texto" ~/directorio

O well:

grep -ir "texto" ~/directorio

In this last example, You combine -i to ignore case with -r to recursively traverse the entire directory tree, a very common combination in everyday life.

Another very useful case involves locate a word like "linux" in the contents of all files in the current directory and view the path of each file that contains it. The instruction would then be:

grep -ril "linux" .

In large projects, it is also key to be able to Exclude certain directories or file types from the searchFor example, to skip .git, build folders or large documentation files.

With grep you can easily do it by combining --exclude-dir y --exclude, in addition to highlighting color matches:

grep -ir --color --exclude-dir={directorio1,directorio2,.git} --exclude={*.txt,*.md} /

Special patterns, comments, and additional examples with grep

Grep is not only useful for searching for individual words, it also It allows you to define slightly more elaborate patterns to locate, for example, comments in a file.A typical example involves searching for all lines that begin with the symbol. #, very common in configuration files.

In that case, you could say something as direct as:

grep ^# nombre-del-archivo

Here the character ^ indicates the beginning of a line, and the pound sign (#) indicates that you are looking for comments that begin with that symbol.This works great on files like /etc/fstab, /etc/hosts or any typical Linux configuration file, or for search for configurations in make menuconfig.

Another very common real-world situation in large projects is the need to search for a specific phrase or expression in all the files of a directory and its subdirectoriesA commonly used variant is:

grep -ri "frase o palabra a buscar" *

In this case, -r performs recursive searching, -i ignores uppercase letters and the wildcard * This indicates that you want to apply the search to all files and folders in the current directory. For those who develop web applications or complex scripts, this is probably one of the most frequently used combinations.

If you want to delve even deeper into grep, you can always turn to the official GNU documentation on the project website or the Wikipedia entry dedicated to this tool, where all the advanced options and full support for extended regular expressions are detailed.

Using regular expressions and advanced options with grep

When things get complicated and simply searching for a single word is no longer enough, grep's regular expressions allow you to build more powerful patternsThanks to them you can search for several words at once, ranges of characters or repeating patterns.

A very practical way to do this is to use grep with the -E optionwhich enables extended regular expressions. For example, If you want to locate lines containing "error" or "warning" in a system logYou could do something like this:

grep -E 'error|warning' /var/log/syslog

In this example the character "|" acts as a logical ORSo the line will match if either word appears. This is great for filtering logs and focusing only on relevant messages.

Another very typical combination consists of filter error messages without distinguishing between capitalization on the same system log file, using the -i option to facilitate the search:

grep -i 'error' /var/log/syslog

If you also want recursively traverse the entire /var/log tree in search of that word, you can launch:

grep -r 'error' /var/log/

There are many combinations, but the mental pattern is always the same: Define what pattern you are looking for and where you want to look for it, adding options to refine the result according to your specific needs.

find: locate files and combine it with grep

Besides searching for text within files, you very often need First, locate which files interest you by their name, size, or modification date.The key command for this is find, another classic of any Linux system.

Find is designed for traverse directory hierarchies recursively and return files or folders that meet a conditionIt does not search for text within the content on its own, but combined with grep it becomes a very powerful tool for sophisticated searches.

The general syntax is usually expressed as find route -options valueFrom there, conditions such as file name, type, modification date, or size are chained together.

For example, if you want search the current directory for a file whose name is exactly "file", you can run:

find . -name "archivo"

If what you need is launch that same search over the entire file system starting from /The order would be:

find / -name "archivo"

When you don't remember exactly how the word was spelled, you have the option of not distinguishing between uppercase and lowercase with -iname, very useful in shared systems where everyone names things in their own way:

  DeepSeek Introduces Fire-Flyer File System (3FS), an AI-Optimized File System

find . -iname "archivo"

You can also use wildcards to locate files whose names you only partially remember, for example, if you think they contained the string "chiv" at some point:

find . -iname "*chiv*"

Frequent options for find and combined search with grep

Find is more than just a name. Among its most used options are -type, -mtime and -size, which allow filtering by resource type, date and sizerespectively. With -type f You specify that you only want files, whereas with -type d You limit the search to directories.

The option -Mtime It's used to search for files. modified a certain number of days ago. For example, -mtime -7 Select files changed in the last 7 days, which is very useful for reviewing only recent logs.

For its part, -size lets you search for files by sizeThis is ideal if you're trying to identify which large files are taking up space on your system or in your project. All these filters can be combined to further refine the results.

The really interesting part comes at Combine find with grep using -exec, so that you first select which files you want and then search for text within themFor example, if you want to locate the word "error" in all files .log de /var/logYou could run:

find /var/log -name '*.log' -exec grep 'error' {} +

That command will make `find` generates a list of .log files and, on each of them, runs `grep` searching for the specified text string. The bookmark {} is replaced by the name of the found file and the sign + It allows processing multiple files at once.

If you want to fine-tune even further and limit yourself to files modified in the last week, you can add the condition -mtime -7 and leave the order like this:

find /var/log -type f -mtime -7 -exec grep 'error' {} +

With a single line you achieve narrow down the search by date, file type, and contentThis is really powerful when you're working with thousands of log files on a production server.

awk: search and process text by fields

Although it is usually associated more with the manipulation of structured data, awk can also be used to search for text within files, with the added advantage that it allows treating each line as a set of fields separated by a delimiter.

Awk is actually A small, special-purpose programming language designed for analyzing and processing text files.Its basic use, however, is not complicated: simply define a pattern between bars. /patron/ and an action between keys, normally {print} to show the line.

For example, if you want Show all lines in /var/log/syslog that contain the word "error"You can do something as direct as:

awk '/error/ {print}' /var/log/syslog

This way of working is similar to grep, but awk shines especially when You need to extract specific fields from each lineFor example, a username, an IP address, or an identifier separated by a colon.

The option -F allows you to define the field delimiterand the variables $1, $2, $3... They refer to the first, second, third field, etc. If in a file the fields are separated by :You could do something like:

awk -F':' '/error/ {print $1}' /var/log/syslog

With this example, awk It searches for lines containing "error" and displays only the first field. before the colon. Applied to logs, it allows you to display only the date, only the service name, or any other column you are interested in.

sed: find and replace text in stream

Another very powerful tool for working with text in Linux is sed, a flow editor oriented towards performing transformations on text strings as they pass through standard input. Although its most well-known use is search and replace, it can also be used to apply bulk changes to a file.

The most commonly used syntax is based on the command s/patron/nuevo/Where s It comes from "substitute". With that you can replace the first occurrence of a text with another on each line of the indicated file.

For example, if you want Change the first occurrence of "error" to "ERROR" on each line of /var/log/syslog Without modifying the original file, you will be able to:

sed 's/error/ERROR/' /var/log/syslog

If what you need is replace all occurrences of that word in each line, we add the modifier g (from “global”) at the end of the instruction:

sed 's/error/ERROR/g' /var/log/syslog

When you are already clear about the change you want and desire Apply it directly to the file, without creating a copy.The key option is -i, which indicates “in-place”:

sed -i 's/error/ERROR/g' /var/log/syslog

For safety reasons, it is usually recommended. First, do a test without -i to see the resultAnd only when you are sure that the replacement is correct should you add that option to modify the actual file.

ack: a modern alternative to grep for developers

In addition to the classic tools, many modern distributions allow you to install ack, a utility designed as an improved replacement for grep, primarily focused on searching for text in source code trees.

Ack behaves similar to grep, but By default, it ignores certain directories and file types that are not usually of interest to the programmer.as the .git, build directories or generated binaries. This makes many searches faster and more useful without having to add manual exclusions.

  Purism in GNU/Linux versus everyday use: philosophy, practice and future

To start using it on a Debian or Ubuntu-based distribution, you would simply need to install the corresponding package from the package manager:

sudo apt install ack

Once installed, you can search for a word recursively throughout the current directory tree with a command as simple as:

ack 'palabra'

If you prefer limit the search to a specific routeFor example, to specify a specific project on your homepage, you can indicate it directly as the second argument:

ack 'palabra' '/ruta_archivo'

In this way, ack will traverse the files in the specified path, showing the lines that contain the searched string and highlighting the matches in a visually appealing way. In large development projects, it can become your best ally for locating functions, classes, or text strings spread across many files.

Search for words in large files and across the entire system

When you encounter Linux systems that have been in use for many years, it's common to find with enormous volumes of files and directories, where locating a specific word can seem like an impossible missionThis involves combining several of the above techniques.

On one hand, you have the use of grep with recursive search and file type filtersThis allows you to focus only on relevant files and discard noise. A good example is searching for a term across an entire project while avoiding certain version control or documentation directories.

On the other hand, you can start by Locate the appropriate file using find, filtering by name or extensionand then apply grep only to that reduced list of files, or use tools like strings to extract hidden text from binaries.

In the specific case of wanting locate a word within a set of files located at a specific pathA commonly repeated syntax with grep could be:

grep -rw '/ruta_archivo' -e 'palabra'

Here, -r activates recursive search, -w searches only for complete word matches, and -e allows you to define the pattern to search for.If you want to exclude certain file types, you can use:

grep --exclude='*.tipo' -rw '/ruta_archivo' -e 'palabra'

Thus, You ignore certain extensions that you're not interested in reviewing.This is very interesting in directories that mix binaries, images, temporary files, and text files.

In case you want search only by file name and not by contentRemember that find is the right tool. With a structure as simple as:

find . -name "archivo.tipo"

You can traverse the current directory in a descending and recursive manner in search of a specific file, instantly seeing the exact path where it is located and then editing it with your favorite editor.

Searching for text in Linux without using the terminal

Although the command line offers brutal control, some prefer perform certain searches from the graphical environmentespecially if you're just starting out with Linux or if you simply find it more convenient.

On desktops like GNOME, KDE or similar, the The file explorer includes a built-in search engine. which allows you to enter a word and locate files that contain it or whose name matches the entered text.

In practice, you can Open the file manager, navigate to the folder where you want to search, click on the magnifying glass icon, and type the desired word.. You can too Create shortcuts in Linux to quickly access frequently used folders.

While not reaching the level of flexibility offered by grep, find, or ack, this method is still effective. Very convenient for quick searches or for users who don't want to touch the terminal, and it integrates well into the everyday workflow of the graphical environment.

By combining these options, both graphical and console-based, you will have All the necessary tools to search for text in Linux files and directories without getting lost in the process, from the typical log with errors to the code of a large project with hundreds of files.

Master text search in Linux to work faster

Having commands like grep, find, awk, sed, and ack give you a huge advantage when navigating complex projects and large volumes of information.Each tool fulfills its function: grep locates patterns within files, find finds the appropriate files and directories, awk and sed process and transform text, and ack speeds up work in code trees.

As you get used to using these tools, Tasks that used to take minutes or even hours are reduced to a few seconds.And working with logs, configurations, or source code becomes much smoother. Ultimately, it's about gradually incorporating these commands into your daily routine so they become almost second nature.

It doesn't matter if you're coming from Windows, macOS, or have been using Linux for years: Learning to search for text effectively within files and directories is one of those skills that you'll put to good use from day one..

How to use Strings to extract hidden text in binaries
Related article:
How to use strings to extract hidden text in binaries