How to Install and Use Ack Command in Linux with Examples

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
Neovim Directory Listing
Neovim Directory Listing

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
Find Total Number of Files in Directory
Find Total Number of Files in Directory

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
Find Specific String in Files
Find Specific String in Files

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 
Find Specific Word in Files
Find Specific Word in Files

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
Find Specific String in File Types
Find Specific String in File Types

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  
Find Total Occurrence of String in Files
Find Total Occurrence of String in Files

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
Prints Total File Lines
Prints Total File Lines

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 
Prints Total Words in Files
Prints Total Words in Files

We can also narrow down the above command result to only consider Python files:

$ ack -ch -w --python restrict
Prints Total Word in File Types
Prints Total Word in File Types

We can also use the time command to monitor the speed of our searches:

$ time ack -ch restrict
Prints Total Time Taken for Searches
Prints Total Time Taken for Searches

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
Faster Searches Using Ack Command
Faster Searches Using Ack Command

We could also output the actual file names that matched a search pattern.

$ ack -f --python
Prints File Names for Search String
Prints File Names for Search String

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
Naming Convention of File
Naming Convention of File

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.

Got something to say? Join the discussion.