Find text within a file in Linux

Linux can be difficult and can be challenging for those who do not want to learn and get their hands dirty, System administrators work hard enough to acquire the skills to tame Linux, it’s not an easy journey and often requires years of practice. This requires them to work with text, configuration files, log files, and all sorts of text files. With so many files, it’s not often easy to find the configuration items you want to find. So I decided to list down some ways that can help you find what you are looking for in Linux

1. Find using Grep

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive, t’s worth noting: the -r option is lazy (traverses depth-first, than stops after the first directory), while R is greedy (will traverse the entire tree correctly)
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search

Along with these, --exclude--include--exclude-dir flags could be used for efficient searching:

  • Search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
  • This will exclude searching all the files ending with .o extension
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
  • For directories it’s possible to exclude one or more directories using the --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
/* modified from @hubgit and http://stackoverflow.com/questions/30328636/google-apps-script-count-files-in-folder 
for this stackexchange question http://webapps.stackexchange.com/questions/86081/insert-image-from-google-drive-into-google-sheets by @twoodwar
*/
function listFilesInFolder(folderName) {

   var sheet = SpreadsheetApp.getActiveSheet();
   sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Image"]);


//change the folder ID below to reflect your folder's ID (look in the URL when you're in your folder)
    var folder = DriveApp.getFolderById("1iSk-d1LUOaO2jo0agmVO6JE2oOfhz4Mn");
    var contents = folder.getFiles();

    var cnt = 0;
    var file;

    while (contents.hasNext()) {
        var file = contents.next();
        cnt++;

           data = [
                file.getName(),
                file.getDateCreated(),
                file.getSize(),
                file.getUrl(),
                "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
                file.getId(),
                file.getDescription(),
                "=image(\"https://docs.google.com/uc?export=download&id=" + file.getId() +"\")",
            ];

            sheet.appendRow(data);



    };
};

2. For die-hard find fans

If you strictly want to use find then use find + grep

find /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;

Add -s (--no-messages) to grep, and 2>/dev/null at the end of the command to avoid lots of Permission denied messages issued by grep and find

find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null

This will print the lines in the files where the text appears, but it does not print the file name.

find . -name '*.js' -exec grep -i 'string to search for' {} \; -print

3. Doing the same with Awk

pattern is the string you want to match in the files.

awk '/^(pattern)/{print}' /path/to/find/*

4. Searching withing GUI

Searchmonkey: Advanced file search tool without having to index your system using regular expressions. Graphical equivalent to find/grep. Available for Linux (Gnome/KDE/Java) and Windows (Java) – open-source GPL v3, some features include

  • Advanced Regular Expressions
  • Results shown in-context
  • Search containing text
  • Panel to display line containing text

5. Alternatives

While you should never replace (or alias) a system command with a different program, due to the risk of mysterious breakage of scripts or other utilities, if you are running a text search manually or from your own scripts or programs you should consider the fastest suitable program when searching a large number of files a number of times.

Andrew Gallant’s Blog claims: “ripgrep is faster than {grep, ag, git grep, ucg, pt, sift} Of particular interest is his section on regex implementations and pitfalls.The following command searches all files, including hidden and executable:

$ rg -uuu foobar

The Silver Searcher (ag) claims it is 5-10x faster than Ack. There are some stackoverflow answers which refer to this application. GitHub doesn’t appear as recent as ripgrep’s and there are noticeably more commits and branches with fewer releases, it’s hard to draw an absolute claim based on those stats. In short ripgrep is faster and efficient with a tiny learning curve.

I took the liberty to add some answers from stackoverflow.

%d bloggers like this: