The Linux operating system allows you to search both directory structures and directory files for specific text string matches. A common tool that aids us in achieving these directory searches is a grep (global regular expression print) command. The grep’s use of regular expressions makes it possible to initiate any string pattern search towards a matching textual output on the command line.
[ You might also like: Ripgrep – The Fastest Command Line Search Tool for Linux ]
However, the drawbacks of grep are in its speed and non-flexible features. This is where the ack tool takes over. It gives speedy searches that extend to source code directory searches while gifting a Linux user the flexibility of excluding certain outputs from search results.
How to Install Ack in Linux
Any Perl-supported platform can accommodate the ack search tool. We install it by referencing its main package, ack-grep.
$ sudo apt-get install ack-grep [On Debian, Ubuntu and Mint] $ sudo yum install ack-grep [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/ack-grep [On Gentoo Linux] $ sudo pacman -S ack-grep [On Arch Linux] $ sudo zypper install ack-grep [On OpenSUSE]
Ack Directory Search
Since ack is attributed to be source-code-oriented, we can demonstrate its string pattern search prowess through a source code directory. Github is a renowned resource for thousands of source code directories. An ideal project source code directory for this article guide is the neovim project.
Install Git in Linux
$ sudo apt-get install git [On Debian, Ubuntu and Mint] $ sudo yum install git [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/git [On Gentoo Linux] $ sudo pacman -S git [On Arch Linux] $ sudo zypper install git [On OpenSUSE]
Clone Neovim Project
The many different file types in this neovim text editor project are necessary for our article guide.
$ git clone https://github.com/neovim/neovim.git
Navigate to the neovim cloned directory to kick-start our tutorial.
$ cd neovim && ls
The listed top-level directory exposes markdown files
(.md), text files
(.txt), and a YAML file.
Implementing Simple Ack Search Commands
Since most search queries from the Linux terminal window lead to uncontrollable scrolling because of infinite outputs, we can pipe these search results to “less” for result instances that exceed the normal terminal window size.
$ echo '--pager=less -RFX' >> ~/.ackrc
The above ack configuration command intelligently handles overflowing result outputs.
Finding Total Number of Files in a Directory
This first command example helps us distinguish the logical efficiency of ack search from grep search. Through grep, we can find out the total number of files on the cloned neovim project through the following command:
$ find . | wc -l
Ack will only count files it feels are logically relevant, hence its output will be different.
$ ack -f | wc -l
The above 7% (2984-2772) disregarded files are ignored from all ack search queries.
Finding a Specific String Pattern Variation from a Directory
Let us try to query the instantaneous occurrences of the string pattern “restrict”.
$ ack restrict
The above outputs point to the exact line numbers in the files containing the string pattern match. The outputted screen pattern results “restrict” are also portions of words like restricted and restriction.
Finding a Specific String Pattern Word from a Directory
If you are not interested in a search result occurring as a variation but as a complete word, you will need to implement your search string patterns differently.
$ ack -w restrict
As you can see, the search string pattern searches for “restrict” as a complete word and not part of any other word occurrence.
Finding a Specific String Pattern from a Specific File Type
Maybe you want your string pattern searches to target a specific file like a Python
(--python) file, c
(--c) file, vim
(--vim) file or any other file type, your string pattern query should reference the file type you seek.
$ ack -w --python restrict
The above command output points to the occurrence of the word “restrict” on line 110 inside the Python file “src/clint.py”.
Finding the Total Occurrence of a String Pattern Search in Each File
Since the cloned neovim directory we are using has different file types, this command counts the occurrence of a specific string pattern search (restrict) in each of these file types.
$ ack -c restrict
Controlling the Output of a String Pattern Search
The above command output includes files with zero string pattern matches for the specified string input. To avoid them, the following command prints the total file lines that are a match for a specified string pattern input.
$ ack -ch restrict
We can make the above output smaller and accurate by requesting the search output to only consider complete words and not word variants.
$ ack -ch -w restrict
We can also narrow down the above command result to only consider Python files:
$ ack -ch -w --python restrict
We can also use the time command to monitor the speed of our searches:
$ time ack -ch restrict
By being more specific in our search pattern criteria, we will get a faster output as demonstrated below in comparison to the above results.
$ time ack -ch -w --python restrict
We could also output the actual file names that matched a search pattern.
$ ack -f --python
The ack search pattern could also target the naming convention of a file. Any C file with the “log” can be matched with the following command:
$ ack -g log --cc
The ack search tool is very flexible when handled under a source code directory. You can still adopt its use to files inside your Linux environment. It is extensible and fast. Use the
man ack command to find more options for exploring this fast and lightweight tool.