I have a directory with many text files.

Out of these files, I am interested in a word "abcdefghi". I need to list all possible cases of this word such as

  • abcdefghi
  • abcdefghI
  • abcDefghi

and all other possible combinations.

It is possible with grep or egrep?

I know, I can write a shell script with combos of grep and inverse grep, unique and achieve the outputs, but I am looking for portable solution.

  • 2
    Have you tried the man page? – Joseph R. May 10 '14 at 14:31
  • Tried, failed. Maybe too dumb to make sense of man pages. – user1263746 May 10 '14 at 14:32
  • 1
    What you want is called case-insensitive match. Use the -iswitch for that. – Joseph R. May 10 '14 at 14:36
  • @JosephR. It will print while whole line, I just need the matched word. See Avinash's answer below. – user1263746 May 10 '14 at 14:44

With GNU grep, try this:

grep -io -- 'abcdefghi' *.txt

I assumed all the files you want to search for a particular text would be ended with .txt (and you don't want the hidden ones).

From man grep on a system where grep is GNU's implementation (as is typical on Linux-based systems).

-o, --only-matching       show only the part of a line matching PATTERN
-i, --ignore-case         ignore case distinctions
  • It appends name of the matched file before the output. Is there any way to suppress that? Except that the solutions is perfect. – user1263746 May 10 '14 at 14:35
  • @user1263746 To do what you want cat *.txt | grep ... – Joseph R. May 10 '14 at 14:37
  • yes you can.grep -io 'rah' *.txt | awk -F: '{ print $2}'. This command removes the file-name from the output. – Avinash Raj May 10 '14 at 14:37
  • 1
    grep -ioh does the job :) Thanks guys! – user1263746 May 10 '14 at 14:45
  • @AvinashRaj: This doesn't find the combinations of string as the OP want. – cuonglm May 10 '14 at 15:55

As a beginner in Bash scripting I was looking exactly for this, and based on the accepted answer above, I wrote the following Nautilus script, which I named "Search Text in Directory...". As this will be useful for me from time to time, I thought it might also be useful for others as well.

# Nautilus Script to search text in selected folder
# Determine the path
if [ -e -n $1 ]; then
    base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
# Determine the type and go
if [ -f "$obj" ]; then
    /usr/bin/canberra-gtk-play --id="dialog-error" &
    zenity --error --title="Search Directory" --text "Sorry, selected item is not a folder."
elif [ -d "$obj" ]; then
    cd "$obj"
# Get text to search
    SearchText=$(zenity --entry --title="Search Directory" --text="For Text:" --width=250)
    if [ -z "$SearchText" ]; then
        notify-send "Search Directory" "Nothing entered; exiting..." -i gtk-dialog-info -t 500 -u normal &
        if [ -f "/tmp/Search-Directory-Results.txt" ]; then
            rm "/tmp/Search-Directory-Results.txt"
        im="zenity --list --radiolist --title=\"Search Directory\" --text=\"Please select one of the search options below:\""
        im=$im" --column=\"☉\" --column \"Option\" --column \"Description\" "
        im=$im"TRUE \"case-sensitive\" \"Match only: Text\" "
        im=$im"FALSE \"case-insensitive\" \"Match: TEXT, text, Text...\" "
        choice=`echo $im | sh -`
        if echo $choice | grep -iE "case-sensitive|case-insensitive" > /dev/null
            if echo $choice | grep "case-sensitive" > /dev/null
                grep -- "$SearchText" *.* > "/tmp/Search-Directory-Results.txt"
            if echo $choice | grep "case-insensitive" > /dev/null
                grep -i -- "$SearchText" *.* > "/tmp/Search-Directory-Results.txt"
    zenity  --class=LIST --text-info \
            --editable \
            --title="Search Directory" \
exit 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.