Spread the love

Now this article is going to be as simple as possible and straight forward as possible. The idea is to help you understand the pure basics in Linux searching. In addition we are going to use several searching tools to help us with this quest.

The First Search Method Is With The Find Command:

With the find command you can search a folder for files with a specific extensions. For example we are going to find all our files that contain .html in our home directory:

# How to find files with specific extensions using the find command
cd /home/userprofile/
find -name "*.html"Code language: PHP (php)

Now lets assume that we have several .html documents in this directory, the output will be something like this:

searching with the command find in Linux for files with specific extensions
searching with the command find in Linux for files with specific extensions

But if we are in the /home folder we can ask find to search for a name or extension in the subfolders too like in this example:

# Search with find in subfolders
find /home -name index.htmlCode language: PHP (php)

The find command is going to search all subfolders in /home directory and find all files that are named index.html:

search in subfolders with the find command in Linux
search in subfolders with the find command in Linux

Another cool way to search with find is by owner and it can be very useful when you need to find all the files for a specific user like in the next example:

# Find files who belong to
find / -user sonik 2> /dev/nullCode language: PHP (php)
Find files who belong to in linux
Find files who belong to in linux

Now this one is very good in this next example we are going to search for files witch specific permissions like 777

# Search for files with permissions
find /home/sonik -perm 777Code language: PHP (php)
Search for files with permissions
Search for files with permissions

The Second Search Method Is Going To Be Using The Locate Command

Install Locate

By default locate may not be installed on your distribution so depending on your distro it has to be installed. In this example we are going to install it in Ubuntu Linux like so:

# Install locate in Ubuntu 21.04 Linux
sudo apt-install locate

#update locate db
sudo updatedbCode language: CSS (css)

Locate has several alternatives like mlocate and plocate, but we are going to use locate because it’s most popular.

Search for a file with locate

Now locate is going to search in the whole Linux system and the most common and basic search with locate is just locate + files search name like in this example

# Search for a file with locate
locate filenameCode language: PHP (php)
searching with locate in ubuntu 21.04
searching with locate in ubuntu 21.04

Search for exactly only specified criteria

If you want to find all files or directories that contain exactly and only your search criteria, use the -b option with the locate command, like in this example:

# Search for exactly only specified criteria
locate -b '\mydata'Code language: PHP (php)
Search for exactly only specified criteria
Search for exactly only specified criteria

The Third Method Is With The Ack Tool

How to install?

Now ack has to be installed on your distribution so here is a guide on how to do it.

How to use ack?

If you use ack just typing ack “text” the command will work perfectly fine, but it can give you so much more power. Here are some examples:

01 – Basic usage

# Basic usage
ack string-to-search

# Example:
cd /var/lib
ack name
Code language: PHP (php)
Basic usage ack linux command

02 – To find how many files contain the string you are searching use

# Find how many files contain string
ack -f | wc -l

# Output
2331
Code language: PHP (php)

03 – To search for instances of our pattern surrounded by word boundaries use -w

# Surrounded by
ack -w string-to-search
Code language: PHP (php)
To search for instances of our pattern surrounded by word boundaries use -w
Surrounded by

04 – Use -c to get detailed information about the string you are searching for

# Detailed information about string
ack -c string-tosearch

# Output
Doxyfile:8
Makefile:2
uncrustify.cfg:1
.travis.yml:2
neovim.rb:0
vim-license.txt:52
Code language: CSS (css)
Use -c to get detailed information about the string you are searching for
Detailed information about string

05 – Search for file type like css or python or txt

# Search for file type like css or python or txt
ack string-to-search --css
Code language: PHP (php)
Search for file type like css or python or txt
Search for file type like css or python or txt

Bonus Search tools

Search Tools
ripgrepRecursively search directories for a regex pattern
fzfCommand-line fuzzy finder for your shell
pecoInteractive filtering tool
McFlyNavigate through your shell history
catfishVersatile search GUI powered by locate and find
FSearchFast file search utility based on GTK+3
ANGRYsearchLike FSearch, a search tool inspired by Everything Search Engine

Here is a quick video on the tutorial

We hope you enjoyed this article. if that is so please rate this page with the stars bellow and subscribe to our YouTube channel.

Leave a Reply