Search string in directory, sub-directories and linux files

1

I have this string : MY_STRING and I have this directory :

|root
|-MY_STRING      // <- directorio con MY_STRING por nombre
|--MY_FILE.txt   // <- archivo con MY_STRING escrito en el
|home
|-MY_STRING.txt  // <- archivo con MY_STRING por nombre

I would like to be able to search MY_STRING with a command and return something like:

/root/MY_STRING
/root/MY_STRING/MY_FILE.txt
/home/MY_STRING.txt

At the moment I have tried with commands more or less like this:

grep -ril "MY_STRING" /
grep -rnw '/'-e "MY_STRING"

But these only return the path of the file:

/root/MY_STRING/MY_FILE.txt
    
asked by Jorius 11.11.2016 в 18:00
source

2 answers

2

find <DIRECTORIO_INICIAL> [ OPCIONES ] -n <NOMBRE BUSCADO>

In your specific case, assuming you are in the directory /

find . -name MY_STRING

If you are in another directory, and you want to search from the root

find / -name MY_STRING

If you need wildcards , you have to escape them, so that the shell does not make its own:

find / -name que_empiece_asi\*

If you want to shorten the time, and do not get into other file systems, such as /dev , /sys , /proc , ...

find / -mount -name \*que_contenga_esto\*

If you want a detailed listing

find / -mount -name \*yo_que_se\* -exec ls -l '{}' \;

The manual page will show you all the options: man find . It is a very complete and useful command.

EDITO

I had not read your question well. What you want is to show you any appearance of MY_STRING, either in the name or in the route.

In that case, you have to chain 2 commands:

find / | fgrep TEXTO

find shows you the contents of all the directories.

fgrep shows you only the lines that contain the characters you indicate.

    
answered by 11.11.2016 / 18:37
source
1

try the find command

If I'm not wrong it would be like this:

find "my string"
    
answered by 11.11.2016 в 18:05