7

I want to find all files, and print the path and filename, for any file where the text "Numlock" is used - whether it is lower-, upper- or mixed-case.

What command should I use ?

belacqua
  • 23,570
ksor
  • 199
  • i think you edited a file and forgotted it's path. Am i correct? – Avinash Raj May 04 '14 at 12:25
  • this tail -10 ~/.bash_history command will show you the last 10 commands you runned on the terminal. From that you can indentify the one. – Avinash Raj May 04 '14 at 12:27
  • No, I'm trying to get my NumLock to be ON in the logog dialog and want to find ALL files where NumLock is mentioned - just to see what's going on. – ksor May 04 '14 at 12:32
  • you mean the the word numlock in the filenames. – Avinash Raj May 04 '14 at 12:35
  • No, in the content of the files – ksor May 04 '14 at 12:51
  • You can use grep. But better look here to solve your problem http://www.askubuntu.com/questions/155679/how-to-enable-numlock-at-boot-time-for-login-screen – TuKsn May 04 '14 at 12:56
  • I'm just working through this list of methods for toggling the NumLock but NONE of them works on my machine, that's why I want to find out where there might be files that manipulate the NumLock by ubing the numlockx command. I tried this in grep: grep -l -i 'numlock' /. - but I think it only looks in the top directory. – ksor May 04 '14 at 13:56
  • would you care for a python script that gives you the files + paths + occasions of the word, no matter their combination of upper/lower? – Jacob Vlijm May 04 '14 at 14:21
  • Jacob Vlijm - I'm not that skilled in this Linux beep so I would like it as simple as possible - I don't know what a python script is or how to "run" it ;-((

    – ksor May 04 '14 at 14:27
  • it is really quite simple, copy the text, paste it into an empty textfile (gedit), save it as search.py. Then open a terminal and type "python3" + space, then drag the script on to the terminal window (press return). – Jacob Vlijm May 04 '14 at 14:31
  • Yeah, it's looks simple enough - teach me ;-)) – ksor May 04 '14 at 14:36

2 Answers2

14

You can use grep -r to do a recursive search of file contents e.g.

grep -Iri 'numlock' /path/to/search/dir/

where /path/to/search/dir/ is the top-level directory from which you want to start the search - you could use / but be prepared for it to take a long time.

Some variations, depending on your exact requirement:

  • change the -r option to -R if you wish to follow symbolic links
  • add the -l option to print just the names of the files found

The I tells grep to ignore binary files and the i makes the search case-insensitive.


If your version of grep does not support recursive searching, you can achieve the same thing using a combination of find and grep e.g.

find /path/to/search/dir/ -type f -exec grep --color -HIi 'numlock' {} +
steeldriver
  • 143,099
3

The script below searches (text)files in a given directory recursively, for occurrences of a given string, no matter if it is in upper or lowercase, or any combination of those.

It will give you a list of found matches, the paths to the files, combined with the filenam and the actual occurrences of the string in the file, looking like:

/path/to/file1 ['numlock', 'numlocK']
/longer/path/to/file2 ['NuMlOck']

etc.

To limit the search time, I would look for matches in specific directories, so not for 2TB of files ;).

To use it:

1] Copy the text below, paste it into an empty textfile (gedit). 2] Edit the two lines in the headsection to define the string to look for and the directory to search. 3] Save it as searchfor.py. 4] To run it: open a terminal, type python3+space, then drag the script on to the terminalwindow and press return. The list of found matches will appear in the terminalwindow

In case of an error, the script will mention it.

#!/usr/bin/python3
import os
#-----------------------------------------------------
# give the searched word here in lowercase(!):
searchfor = "string_to_look_for"
# give the aimed directory here:
searchdir = "/path/to/search"
#-----------------------------------------------------
wordsize = len(searchfor)
unreadable = []
print("\nFound matches:")
for root, dirs, files in os.walk(searchdir, topdown=True):
    for name in files:
        file_subject = root+"/"+name
        try:
            with open(file_subject) as check_file:
                words = check_file.read()
                words_lower = words.lower()
                found_matches_list = [i for i in range(len(words_lower)) if words_lower.startswith(searchfor, i)]
                found_matches = [words[index:index+wordsize] for index in found_matches_list]
                if len(found_matches) != 0:
                    print(file_subject, found_matches)
                else:
                    pass
        except Exception:
            unreadable.append(file_subject)
if len(unreadable) != 0:
    print("\ncould not read the following files:")
    for item in unreadable:
        print("unreadable:", item)
Jacob Vlijm
  • 85,675
  • I created a file on the desktop with the line: "#!/usr/bin/python3" and all to the (including) "pass" - then opens a terminal Ctrl+Alt+t and type puthon3 and 1 space, then drag the file icon from the desktop to the terminal window ... and it just "run" back to the desktop again ... nothing happens. I don't think the script is "piped" into the python3 - right ? – ksor May 04 '14 at 15:53
  • nothing wrong with the filename nor the content of the file - but when I Drag&Drop the file icon from the desktop to the open terminal window, where the "python3 " is written, then the icon just "runs" back to the desktop when I drop it - exactly the same with your file too – ksor May 04 '14 at 18:17
  • YES, it worked ! – ksor May 04 '14 at 18:35
  • @ksor AHA! That is nice. the old version or the new one? – Jacob Vlijm May 04 '14 at 18:37
  • Both, I just have to WRITE the filename in the terminal window. The new version id the best, and I just left out the "matches" it found, so I ONLY have the filenames in the list then I "piped" it to a file on the desktop - that's what I was looking for ;-)) – ksor May 04 '14 at 20:20
  • @ksor here comes another one 5x as fast :) (in a minute) – Jacob Vlijm May 04 '14 at 20:27
  • 3
    I'm not clear on why this is preferable to recursive grep? – belacqua May 07 '14 at 22:48