How to Search File Content in Linux Mint
In this tutorial, we'll see several examples of file content search for Linux Mint. The examples will cover built in tools and additional software which can be added to Linux Mint. The examples are applicable for other Debian like systems - Ubuntu etc. The default file explorer tool Nemo is not capable of searching the file content.
Step 1: Use grep to search in file content with terminal
So let's start with a simple example where we are searching (exact match) for term pandas
in home folder /home/user/datasets
:
grep -rnw '/home/user/datasets' -e 'pandas'
Here is the flags used for this example:
-r
/-R
- recursive search-n
- show line number-w
- match the whole word-e
- pattern. Several regex patterns can be provided
Note: Additional useful parameters for more accurate and faster search are :
-i
- case insensitive search. It will slow down the process a lot-l
(lower-case L) show only the file name of matching files--exclude-dir={dir1,dir2}
- exclude directories from the search--include=\*.{py,ipynb}
- search only files ending with py and ipynb--exclude=\*.xls
- search all files except those which end with xls
Step 2: Use grep to search recently updated files for text
Next, we'll see how to search only in the recently updated files. In this example files updated in the last hour:
grep -r "^$(date -d '-1 hour' +'%H')" . | grep -r -a 'pandas' .
In the example above we are searching in the current folder - denoted by dot - .
for all files updated in the last hour:
grep -r "^$(date -d '-1 hour' +'%H')" .
Next we search the content of found files by piping the results of the first command with |
:
grep -r -a 'pandas' .
This speed up the search process with magnitudes as you can see in this practical example: Restore Notebook from the browser cache
Step 3: Advanced Search Tools - Search Monkey
If you prefer to use search tools there are many option available for Linux Mint - we will start with Search Monkey. It can be installed from the Software Center or by:
sudo apt-get install searchmonkey
It's a powerful tool for finding file contents by regular expression and customization. You can find the interface below:
It allows:
- history of search results
- find based on create/modify date of the files
- limit results
- size limits
It's a fast and easy tool to find text inside files with many constraints.
Step 4: Simple Search Tools - Catfish
Another simple option for text file content search is Catfish. It can be installed from Software Center or by:
sudo apt-get install catfish
It's a bit simpler than Search Monkey but it's good enough to find files based on their content. It has several options like:
- search hidden files
- exact match
- file creation time
Step 5: Recoll search in Office files content - excel, docs, pdf
Another powerful tool is the Recoll. Two installation option are available:
sudo apt-get install recoll
or the Software Center.
It has special query language - for example search in a given directory:
dir:/home/user/Documents youtube
It can be used for search in different file formats like:
- Excel - xls, xlsx, ods
- Word - doc, docx, odt
- epub
To find more about this you can read the official documentation:
It may require additional packages to be installed if you like to search in word and excel files. Libraries depend on the version of the OS that you have.
Note: A disadvantage of this program is the indexing which takes considerable disk space.
Step 6: Search pdf files with pdfgrep
There is a special tool called pdfgrep
which can be installed with:
sudo apt-get install pdfgrep
Several options are possible - search recursively for pandas in PDFs(the PDF files should be text and not images!):
pdfgrep -r "pandas"
or search for pdf files in a folder and then search inside them by:
find /home/user/Documents -iname '*.pdf' -exec pdfgrep pattern {} +
If none of these options work for you then you can write a custom python script in order to search and modify Excel file content. Several
examples can be found here: How to Search and Replace in Excel File using Python