• Post Reply Bookmark Topic Watch Topic
  • New Topic

grep just file name containing a string  RSS feed

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys, trying to touch up my linux commands, however, when i check on stack overflow and super user, it just confuses me.

I found out that grep -r vim .
goes through all my in files that have the string "vim" but not file..

instead, how do I grep all file name ONLY in multiple directories with a matching string?


so lets say i have
/Dropbox
folder 1
folder 2
folder 3

only folder 2 has a file name called "Iseevimhere.txt" -- if i were to do grep -r vim . it would just look for the in-file for vim, but not the file name (if that makes sense)

how would I search all the directories for the file containing vim? I know I can use the command find . -name "Isee*" and that would display all, but lets I do not remember the file name, but I do remember a string of it.

thanks a ton!
 
Marshal
Posts: 4054
241
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
grep just searches contents of files, as you've just explored. To search for files, you need find. For example:

find . -name "*vim*"

... will find all files with "vim" in the name.
 
Tim Cooke
Marshal
Posts: 4054
241
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to get really fancy and search for files with "vim" in the name that also contain the text "vim" you can do this:

find . -name "*vim*" -exec grep -H "vim" {} \;

(note: the -H flag instructs grep to additionally print the file path)
 
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:If you want to get really fancy and search for files with "vim" in the name that also contain the text "vim" you can do this:

find . -name "*vim*" -exec grep -H "vim" {} \;

(note: the -H flag instructs grep to additionally print the file path)


And one I use allot, if you just want the names of the files whose content contains "vim" then

find . -type f -exec grep -l "vim" {} \;
 
william chaple
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you guys are awesome thank you so much! I am definitely brushing up with find and grep commands; eventually i will start doing awk
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!