107

Is there any utility to make searches for a string inside ASCII files to avoid command line searches?

How to make a command line search, for example for the string "test" inside all files in the directory /var/x/?

Braiam
  • 67,791
  • 32
  • 179
  • 269
Nuno
  • 1,187

9 Answers9

166

I assume that your first question is about a GUI alternative to the grep command. I can't help you with that, I always find the command line very effective.

As for the command line, try

grep "test" /var/x/*

If you want to search recursively (i.e. not only in /var/x/, but also in subdirectories thereof), do

grep -R "test" /var/x/

To avoid grepping the files which grep thinks to be binary, use the -I option:

grep -I "test" /var/x/

If grep thinks a file is binary (based on first few bytes of the file), it will assume it does not match instead of going through the whole file.

January
  • 35,952
  • 1
    Will it work if the folder contain some non-ascii files? – Anwar Oct 09 '12 at 08:47
  • 1
    Well, grep will also try search for the string in a binary file, and report it if it matches: Binary file file.jpg matches – January Oct 09 '12 at 08:49
  • but it may take a long time which is unnecessary – Anwar Oct 09 '12 at 08:51
  • However, formally there is no difference between binary and ASCII files on a POSIX system. Therefore, I don't see why grepping "binary" files should take more time than equally large text files. Unless you completely want to avoid grepping binary files (yes, it can be useful). – January Oct 09 '12 at 08:54
  • 1
    @January When a binary file is read as text, it often has extremely long "lines" (because a character or character sequence that would be interpreted to designate the end of a line may not appear for a long time, or ever). Depending on the way a text search utility is implemented, this could cause performance problems if each "line" is read fully into memory and then checked to see if it matches the search string (which is a reasonable way for grep to be coded, though I don't know if Ubuntu's grep is written that way). – Eliah Kagan Oct 09 '12 at 09:30
  • Well, it all depends on the files you have. I've got plenty of ascii files with extremely long lines. Also, grep (Ubuntu uses the standard GNU grep) has a bit more complex algorithm (you can see the sources with apt-get source grep), and, in short, it reads long lines in chunks. – January Oct 09 '12 at 09:48
  • 1
    "grep -i" for case insensitive search – Czarek Tomczak May 24 '13 at 15:32
  • 3
    There should be a * after the final slash in the path or grep does not appear to do any work in 14.04 – DavidScherer May 19 '16 at 13:35
  • @January Is there any option for grep to print the actual lines matching the string? Currently I only get Binary file myFile.pdf matches. Thank you. – codeaviator Oct 22 '17 at 20:52
  • This is better than the accepted answer. – WinEunuuchs2Unix Dec 18 '20 at 01:10
81

You can use searchmonkey. The tool is available in the repositories, so you can simply

sudo apt-get install searchmonkey

On the other hand, command line search with grep is really intended for that...

Here is a screenshot from searchmonkey

enter image description here

Anwar
  • 76,649
alci
  • 5,839
21

You can use regexxer it is a great GUI search/replace tool for regular expressions.

you can download by:

sudo apt-get install regexxer

enter image description here

Seth
  • 58,122
  • it is super fast, but the issues i have with this application are: 1. it does not have a readonly mode, so I don't accidentally replace or delete anything 2. it shows the text, but it will not move to the specific line that the pattern has matched. (I scrolled around 2000 lines of code to find what I was after after changing the color to bright red!) – Mehrad Mahmoudian Sep 26 '17 at 10:58
8

Try Recoll, best GUI one I ever used. To install recoll in all currently supported versions of Ubuntu open the terminal and type:

sudo apt install recoll

It needs some time to index the files first (you can define blacklist path or extensions or mime).

karel
  • 114,770
user244846
  • 81
  • 1
  • 1
  • I can confirm that Recoll is completely another world with respect to gnome-search-tool or search-monkey. It uses Xapian, so it's able to inspect and index word documents (both .doc and .docx), pdf files, mail folders, etc. – Avio Mar 22 '17 at 10:51
  • Uh, it's available through deb packaging (sudo apt install recoll) and it does its job where even grep fails, e.g. with .docx documents that are compressed with pkzip compression. – Avio Mar 22 '17 at 10:56
  • note that recoll requires building index first. – Yrogirg Sep 18 '19 at 01:40
6

You can use the grep command from terminal:

grep -r string *

This command will find all occurrences of "string" in all the files under the current directory (or subdrectories).

For hidden files, you can use:

grep -r string ..

2

Unfortunately grep does a very poor job of searching inside Word (.doc) files, but you can pipe catdoc output into grep. I'm no programmer but this little script works well for me:

#!/bin/bash
export GREP_OPTIONS='--color=auto'
echo -e "\n
Welcome to scandocs. This will search .doc (NOT .docx) files in this directory for a given string. \n
Type in the text string you want to find... \n"
read response
find . -name "*.doc" | 
while read i; do catdoc "$i" | 
grep -iH --label="$i" "$response"; done

All improvements and suggestions welcome!

Ralph
  • 129
2

I've just released a simple tool to do the job. Thought mainly for software developer, it has the (unique?) characteristic of openning several files in the same window. It presents the results in the browser using Ace editor (recomended!) or html textarea. It is a java based tool so it runs in windows as well as in linux.

check it out!

https://sourceforge.net/projects/queacetu.gastona.p/

1

per https://askubuntu.com/a/1141367/47073

You can use mate-search-tool, which is the same thing as gnome-search-tool that was removed. I had problems with searchmonkey, it was missing some results and was slow, do not recommend.

To install mate-search-tool:

sudo apt install mate-utils
mate-search-tool
Yrogirg
  • 371
0

You can use Sublime Text Find in Files option from Find menu to search for strings inside files from a folder.

To Install Sublime Text 3

sudo add-apt-repository ppa:webupd8team/sublime-text-3
sudo apt-get update
sudo apt-get install sublime-text-installer

You can also follow this question for other possible ways of installing Sublime on Ubuntu.

To search in all files for a string from a folder in Sublime Text you can follow this question for more clear answers.

M.A.K. Ripon
  • 3,139