Without a doubt when we enter the world of Linux we find a totally different scenario (if we use Windows or macOS systems) at the level of environment, structure and daily tasks, one of the most striking is to search for words in the system since this is ideal when you need to know what path or file it is in for later editing..
Using commands makes life much easier for us, since with just a few words we can execute complex actions without having to have a menu available. When we have large amounts of files or we have a lot of information in just one, being able to search for specific words that we need in Linux will be really interesting. TechnoWikis will explain various alternatives so that searching for a word in Linux is not something terrified and in this case we will use Ubuntu.
To stay up to date, remember to subscribe to our YouTube channel! SUBSCRIBE
1 How to search for a Word inside a File in Linux with Grep
Grep is in charge of looking for patterns in the files, these patterns are objects separated by line characters and the grep command is in charge of printing each one of these that matches the entered pattern. With Grep a file with the form "-" means standard input and if we do not indicate the file where to search, the recursive search will be used in the entire directory.
Grep Options
Some extra options to use with Grep are:
- -E, --extended-regexp: make use of patterns with extended regular expressions
- -F, --fixed-strings: Interpret patterns as fixed strings but not as regular expressions
- -G, --basic-regexp : use the patterns as basic regular expressions
- -P, --perl-regexp : Interpret patterns supported by Perl expressions
- -i, --ignore-case: Ignore case
- -v, --invert-match: invert the direction of the match
- -c: suppress normal output
- -L, --unmatched-files: display the name of each input file that did not have a match
- -o, --only-matching: print only non-empty matching parts of a match
Step 1
To use Grep we open the terminal and execute the syntax:
grep -rw '/file_path' -e 'word'
Step 2
There we find in which files is the word we are looking for, it is possible to exclude file types with the syntax:
grep --exclude='*.type' -rw '/file_path' -e 'word'
This is ideal when you need to search for a word in a certain type of file.
2 How to search for a Word inside a File in Linux with Find
It is one of the most used ways to search for words in Linux but this only allows you to find the name of the file but not the word within it, its use allows you to search descendingly and recursively in the directory or hierarchy of each file by analyzing a boolean value.
Find Options
It validates the results giving rise to the fact that said file is really available there, some options to use with Find are:
- -name: is the name to recursively look for in the directories
- -path: allows you to indicate the path where the file should be searched
- perm [-]mode: this parameter allows to represent file mode bits
- -type: refers to the type of file to search for, the options are 'b', 'c', 'd', 'l', 'p', 'f', these represent block special file, character special file , directory, symbolic link, FIFO, normal file, etc
- -exec: is an added function which are a group of functions to perform advanced searches through symbols and characters
To use Find in Linux, we open the terminal, access the library or path where the file is 8if we know it) and execute the syntax:
find . -name "file.type”
3 How to search for a Word inside a File in Linux with Ack
The ack utility is a replacement for Grep for current Linux distributions, Ack will search for the files that we indicate or use the input to go to the lines that contain the match with the entered name, by default ack prints the lines that match that name ..
ack options
The options to use with Ack are:
- --a, --all: run on all files
- --c, --count : print a count of matching lines for matching files
- --color, --nocolor - parameter highlights the matching text and --nocolor suppresses the color
- --color-filename=color: allows to define the color to use for filenames
- --color-match=color: defines the color to use for matches
- --flush: flush the output immediately
- -F: prints only the files that will be searched
Step 1
We open the terminal and install Ack:
sudo apt install ack
Step 2
We accept the process:
Step 3
We execute:
ack 'word'
Step 4
This will recursively search the entire system for that word:
step 5
To search in a specific route we execute:
ack 'word' '/file_path'
4 How to search for a Word within a File in Linux with Finder
Step 1
Finally we can resort to the integrated method, for this we open the Linux Explorer (Files) in the desired folder, click on the magnifying glass icon and enter the desired word to list the results.
Step 2
We can visualize the results:
Each of these options is ideal for looking up words in Linux.