Linux search for string in folders and files with ack
Spread the love

One of the most important aps I have ever needed in Linux was ack. With it you can search the whole system or a folder for a specific text and it searches it in file and folder names and in them. Once I searched for the config file of one CMS and ack found the username and password for a second, god bless ack 🙂

But what is ack exactly?

Ack is designed as a replacement for 99% of the uses of grep.

Ack searches the named input FILEs (or standard input if no files are named, or the file name – is given) for lines containing a match to the given PATTERN . By default, ack prints the matching lines.

Ack can also list files that would be searched, without actually searching them, to let you take advantage of ack’s file-type filtering capabilities.

How to install ack?

# Install ack in Ubuntu \ Debian sudo apt-get install ack or sudo apt-get install ack-grep # Install ack in CentOS yum install epel-release yum install ack # Instal ack in opensuse zypper addrepo https://download.opensuse.org/repositories/openSUSE:Factory/standard/openSUSE:Factory.repo zypper refresh zypper install ack # Install ack in Fedora dnf install ack # Install ack in FreeBSD pkg install p5-ack
Code language: PHP (php)

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

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

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

Conclusion

As you can see, the ack app is a very flexible tool for searching files, folders and inside them for a string. Even if you are just using it to find files within your Linux environment, most of the time, the increased power of ack will be useful.

Quck Install and Review 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.

Facebook Comments Box