Linux operating systems offer support for searching both files and directories for specific text strings. One of the most common tools to search for a text string is grep. Grep allows users to search for any pattern with regular expressions within files and directories. However, grep has some limitations. It is a general-purpose tool without any optimization. This is where the Ack tool can help. The Ack tool is specifically designed for developers for searching the source code of programs. The Ack tool is faster than grep and allows us to exclude certain outputs from search results.

In this post, we will show you how to install and use the Ack command in Linux.

Install Ack in Linux

By default, the Ack tool is included in the default repository of all major operating systems.

For Debian and Ubuntu-based distributions, install the Ack tool using the following command:

apt-get install ack-grep  -y

For RHEL, Fedora, and CentOS-based distributions, install the Ack tool using the following command:

dnf install ack-grep  -y

After the installation, verify the Ack version using the following command:

ack --version

You will get the following output:

ack 2.22
Running under Perl 5.26.1 at /usr/bin/perl

Copyright 2005-2017 Andy Lester.

This program is free software.  You may modify or distribute it
under the terms of the Artistic License v2.0.

Also Read

How to Install LFTP to Download and Upload Files in Linux

Download Neovim Source Code

To demonstrate how to use the Ack command, we will download neovim source code from the Git repository.

Run the following command to download the neovim source:

git clone https://github.com/neovim/neovim.git

Once the download is completed, change the directory to the downloaded source and list all files:

cd neovim
ls neovim

You should see all files in the following output:

BACKERS.md   CMakeLists.txt  CONTRIBUTING.md  man        scripts  third-party
BSDmakefile  codecov.yml     LICENSE.txt      packaging  snap     unicode
ci           config          MAINTAIN.md      README.md  src
cmake        contrib         Makefile         runtime    test

Search for the Total Number of Files in the Directory

You can use the Ack command to find out how many files are inside the repository.

ack -f | wc -l

You will get the following result:

2899

Search for a String Pattern Using Ack Command

The Ack command can search for a specific pattern and find data that has either partial or full matches.

For example, to search for the string “restrict” in the repository, run the following command:

ack restrict

You will get the following result:

Ack search for a string

Also Read

How to Compare Two Files in Linux Terminal

Search for a String Pattern Word Using Ack Command

If you want to see the exact match in the result, use the -w option:

ack -w restrict

You will get the following output:
Ack search for exact string
As you can see, the Ack command searches for restrict as a complete word.

Search for a String Pattern from a Specific File Type

If you want to search for a specific string in a specific file type, including Python, C, or Vim, you can use the –python, –c, or –vim option.

For example, to search for a string “restrict” only within Python files, run the following command:

ack -w --python restrict

You will get the following output:
Ack search for a string in specific file

Count the Total Occurrences of a String Search

If you want to count the total number of occurrences of a specified string, use the -c option with Ack command.

ack -c restrict

You will get the following output:
Ack search for total occurrence
If you want to show the total number of occurrences of a string “restrict” in the output, run the following command:

ack -ch restrict

You will get the following output:

232

If you want to check the speed of the searches, run the following command:

time ack -ch restrict

You will get the following output:

232

real	0m0.631s
user	0m0.551s
sys	0m0.076s

Search for a Specific File Type

The Ack command can also find the specific file types from the specified directory.

For example, to find all Python files in your current directory, run the following command:

ack -f --python

You will get all Python files in the following output:

src/clint.py
src/nvim/testdir/pyxfile/py2_magic.py
src/nvim/testdir/pyxfile/py3_magic.py
src/nvim/testdir/pyxfile/py3_shebang.py
src/nvim/testdir/pyxfile/pyx.py
src/nvim/testdir/pyxfile/py2_shebang.py
src/nvim/testdir/test_makeencoding.py
contrib/gdb/nvim-gdb-pretty-printers.py
contrib/YouCompleteMe/ycm_extra_conf.py
scripts/stripdecls.py
scripts/gen_help_html.py
scripts/check-includes.py
scripts/shadacat.py
scripts/gen_vimdoc.py

Conclusion

In this post, we explained how to install and use the Ack command in Linux. Ack is a very useful and flexible search tool for developers when handling multiple projects. Get started now on VPS hosting from Atlantic.Net!