Is there a simpler form of find . -name «*substring_of_filename*» on Mac OS X and Linux?
which is to find all files having names containing substring_of_filename within the current directory (or some other given directory). Because it’s a common thing to do, is there a way to make the command shorter and easier to type, possibly using an alias or function in Bash? Or is there some other easy-to-type command that can be used? Such as:
findfile . substring_of_filename
10 Answers 10
On Mac OS X you may also put an alias using mdfind into .bashrc:
# mdfind uses Spotlight alias findf='mdfind -onlyin . -name' findf app_config
Here’s your bash function, put it in .bashrc :
(Note that -iname makes the search case-insensitive. If you don’t want that, use -name instead.)
Au contraire! There is an even more complicated one, but one, which works:
The dot isn’t neccessary for gnu-find:
But you have to mask the asterixs, else they will be interpreted by the shell, and you will only find filenames which match the ones in the current directory, which match «app_config«.
What seems not true? That you can omit the dot? Or that a file which contains ‘app_config’ in its name and which is in the current directory, will restrict the search to his name.
@BenU: I don’t have a System with Mac OS X, but on Linux, the need for quotes arises from the shell. If I don’t quote the «*», the shell tries to expand it. It is not needed needed for find, and not, if the * doesn’t match something in the current dir — then it is passed unchanged to find. You may try single quotes, which are allowed too in bash, but would prevent variable expansion (which isn’t the case, here, but sometimes else) -name «*.pdf.$id» for example.
For the record, quotes should be used in OS X just the same way as in Linux, since OS X has the same Bash shell as Linux.
Put this either in your ~/.profile or ~/.bashrc , not sure which one Mac uses:
then call the function by findname name_of_file
if you wanted to use multiple words, you’d have to get more complicated..the easiest way I can think of offhand is this:
That will find every file in your current dir, search for string 1, then search that result for string 2, and etc, till it gets to 4.
The second function doesn’t work unless the variables are quoted like find . |grep «$1» |grep «$2» |grep «$3» |grep «$4» . Also, it searches for parts of the path rather than parts of filenames.
Instead of doing this in a bash, zsh, tcsh, sh, etc. script I made a TextExpander shortcut. I type sh≤Space≥ and TE types
and puts the cursor between the asterisks.
On OS X, Why not just use locate? Granted the locatedb isn’t updated very often by default, but you can change that easily (change the periodic job scheduling on OS X). locate is a BSDism, don’t know if it’s on Linux or not.
In some scenarios locatedb is a security risk. LUKS-encrypted home directory + not encrypted root + updatedb indexing /home and storing its cache somewhere in /var (which is not encrypted) leaks filenames from /home.
On most Linux distributions, you can use locate. For example:
$ locate find | grep bin /sbin/findfs /usr/bin/find /usr/bin/find2perl /usr/bin/findsmb /usr/bin/gst-typefind-0.10 /usr/bin/memdiskfind /usr/bin/oldfind /usr/bin/sane-find-scanner /usr/include/c++/4.5/ext/pb_ds/detail/bin_search_tree_/find_fn_imps.hpp /usr/include/c++/4.5/ext/pb_ds/detail/binary_heap_/find_fn_imps.hpp /usr/include/c++/4.5/ext/pb_ds/detail/binomial_heap_base_/find_fn_imps.hpp
If you use bash 4 (with shopt -s globstar ) or zsh:
prinftf %s\\n **/*substring_of_filename*
The answer is «no». There is no shorthand for «find file by filename with substring in current directory or below» in GNU find . The aliases I use to work around this limitation are as follows (for zsh ):
# find file in cwd by [S]ubstring, case [I]nsensitive, change to first sub[D]irectory finds() < find . -name "*$**"; >findsi() < find . -iname "*$**"; >findsid()
The last one is my favorite. These can be used as follows:
> ls -R .: file1 File2 HOLEFILE test ./test: hello
> findsi file ./HOLEFILE ./File2 ./file1 > finds file ./file1 > finds le ./File2 ./file1 > findsid ell > ls hello
I think just grep can do it fine, no need for find. Relevant options from the manpage:
-l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.) -H, --with-filename Print the file name for each match. This is the default when there is more than one file to search. -R, -r, --recursive Read all files under each directory, recursively; this is equivalent to the -d recurse option.
Note that even if -H is the default when there’s more than one file to search, I still suggest you to use it explicitly. This switch solves a particular problem of yours, it’s better to remember this bit of directly related knowledge than the edge case (searching multiple files) when this option is the default.
So the grep version of your original find command will look like this:
How to find lines containing a string in linux [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I have a file in Linux, I would like to display lines which contain a specific string in that file, how to do this?
5 Answers 5
The usual way to do this is with grep , which uses a regex pattern to match lines:
Each line which matches the pattern will be output. If you want to search for fixed strings only, use grep -F ‘pattern’ file . fgrep is shorthand for grep -F .
addition grep -rn ‘string’ /path/ if you want to search a string in a folder in which file including and line number
Besides grep , you can also use other utilities such as awk or sed
Here is a few examples. Let say you want to search for a string is in the file named GPL .
Your sample file
$ cat -n GPL 1 The GNU General Public License is a free, copyleft license for 2 The licenses for most software and other practical works are designed 3 the GNU General Public License is intended to guarantee your freedom to 4 GNU General Public License for most of our software;
$ grep is GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to
$ awk /is/ GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to
$ sed -n '/is/p' GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to
Finding a substring in files across subdirectories with a single built-in command?
However, in Linux (say, Ubuntu) I have found no other way than some piped command involving find , xargs , and grep (an example is at this page: How can I recursively grep through sub-directories?). However, my question is different: is there any single, built-in command that works through this magic, without having to write my shell script?
5 Answers 5
GNU grep allows searching recursively through subdirectories:
grep -r --include='*.h' 'the string' .
I tried this exact command line in Ubuntu but get «grep: invalid option — ‘M'» although I can’t see any ‘M’ anywhere. puzzling
In a suggested edit TomasG recommends changing the last * to ‘*’ : «Quoting the last wildcard is needed to avoid errors when some filename starts with -«
@Guido: Your problem may have been due to a file in the current directory called -Msomething , or to a GREP_OPTIONS setting.
grep -r searchpattern /path/to/start/in
Where /path/to/start/in/ can be just » . » for the current directory.
No. find -name \*.h -print0 | xargs -0 grep -l ‘the regex’ is as magic as it gets.
To find string from given directory use below command
find /apps_bev/apps/xfer/export/02210 -name '*' -exec grep -l '38221000001032' <> \;
grep can take more than one file as arguments, so you should use + instead of \; to avoid running one grep invocation per file which is quite inefficient.
Note that -name ‘*’ restricts to files whose name is valid text in the current locale (in some find implementations at least), but the other directory components may still contain sequences of bytes that don’t form valid characters. If your intention with that -name ‘*’ was to make sure the output is valid text (by omiting the files with improper names), you’d rather use find . ! -name ‘*’ -prune -o -exec grep . <> + , which would also stop find from descending into directories with improper names.
is there any single, built-in command that works through this magic . ?
To be pedantic, no, you cannot assume such a command exists.
There are many different implementations of Unix, and each has its different quirks. POSIX, the common denominator (and closest thing to a standard across Unices) does not specify such an option for grep .
As mentionned in other answers, GNU’s implementation of grep has a non-standard option that does what you want. While this particular implementation might be common on Linux systems, you cannot assume its availability on any Unix, even some Linux systems.
Finally, I should mention that it is the Unix philosophy to favor the combination of several primitive programs, over the use of one big monolithic executable attempting to do everything at once.
In your case, crawling the file system and matching regexp in a stream are two separate tasks. It is only normal to treat each in a separate program.