I'm on Ubuntu, and I'd like to find all files in the current directory and subdirectories whose name contains the string "John". I know that grep
can match the content of the files, but I have no idea how to use it with file names.
-
3Possible duplicate of How can I recursively find all files in current and subfolders based on wildcard matching? – phuclv Aug 12 '16 at 4:30
Use the find command,
find . -type f -name "*John*"
-
1Creating a custom bash script with
#!/bin/bash if [ -z $1 ]; then echo "Error: Specify pattern for search"; else /usr/bin/find . -type f -name "*$1*"; fi
would let you just runF search-string
as a perfect shortcut – Ilia Rostovtsev Jul 29 '15 at 21:13 -
-
@Joe Ah, right. You mean in case a path has spaces? But here we only check for existence of $1 space would make $2 appear and that's it. You mean it's theoretically better, right? – Ilia Rostovtsev Oct 8 '15 at 9:34
-
@IliaRostovtsev - Actually, I was wrong. If $1 is null then the test becomes
if [ -z ]
. I thought that would be a syntax error, but it works. I can simplify some of my code from now on. – Joe Oct 8 '15 at 10:34
A correct answer has already been supplied, but for you to learn how to help yourself I thought I'd throw in something helpful in a different way; if you can sum up what you're trying to achieve in one word, there's a mighty fine help feature on Linux.
man -k <your search term>
What that does is to list all commands that have your search term in the short description. There's usually a pretty good chance that you will find what you're after. ;)
That output can sometimes be somewhat overwhelming, and I'd recommend narrowing it down to the executables, rather than all available man-pages, like so:
man -k find | egrep '\(1\)'
or, if you also want to look for commands that require higher privilege levels, like this:
man -k find | egrep '\([18]\)'
-
3I'm curious ... a question that shows no attempt to resolve the problem at hand via a search gets an up-vote, a friendly explanation how to find out about possible commands gets trodden on. What are the criteria? :) – tink Oct 30 '12 at 3:05
-
1i upvoted you. lesson is few can man it. but we can see samples and adapt :) – tgkprog Jul 2 '13 at 5:53
-
Yes, this answer should be downvoted as it runs counter to the guidelines of SO – Grand Phuba Sep 1 '20 at 23:28
-
Thanks @GrandPhuba - how about having the question which also runs counter those guidelines not only downvoted but deleted? ;) – tink Sep 2 '20 at 0:20
-
@tink there's the flag and downvote feature for that. Two wrongs don't make a right. – Grand Phuba Sep 2 '20 at 4:04
This is a very simple solution using the tree
command in the directory you want to search for. -f
shows the full file path and |
is used to pipe the output of tree to grep
to find the file containing the string filename
in the name.
tree -f | grep filename
The find
command will take long time because it scans real files in file system.
The quickest way is using locate
command, which will give result immediately:
locate "John"
If the command is not found, you need to install mlocate
package and run updatedb
command first to prepare the search database for the first time.
More detail here: https://medium.com/@thucnc/the-fastest-way-to-find-files-by-filename-mlocate-locate-commands-55bf40b297ab
use ack its simple.
just type ack <string to be searched>
-
It seems this will also include all paths containing the search text, not just files: stackoverflow.com/questions/7698867/… – underscore_d Nov 18 '15 at 17:37