Atlantic.Net Blog

How to Install and Use the Ack Command in Linux

Hitesh Jethva
by Atlantic.Net (468 posts) under Tutorials, VPS Hosting
0 Comments

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.

Prerequisites

  • A server running Linux on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing any Linux operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

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!

Get A Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Best Cloud Consultancy or MSP in 2022
BAA Red Flags: What Should Your HIPAA-Compliant Hosting Company Be Willing to Accommodate?
HIPAA Compliance for Remote Workers: How to Maintain HIPAA Compliance with a Remote Team
How to Make the Best Use of the Cloud in the Internet of Things (IoT)
How to Make the Best Use of Artificial Intelligence in Cloud Computing

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2008 Lookout Dr,

Garland, Texas 75044

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources