Ignore case when trying to match file names using find command in Linux - linux

Right now, all I know to use is:
find / -name string.*
that is case sensitive and it won't find files named:
1string.x
STRing.x
string1.x
How can I search so that all the above would be returned in the search to a case-insensitive matching?

Use the -iname option instead of -name.

Or you could use find / | grep -i string

This works as well, if you want to avoid the single quotes:
find . -iname \*string\*

Use -iname in find for case insensitive file name matches.

If the system you are in does not have the find command provided by the GNU utils package, you can use the -name tag alone with POSIX bracket expressions as
find . -name '*[Ss][Tt][Rr]ing*'

Related

Unix search ignore caps

I want to search for a specific file by name in Solaris.
But I don't know whether the file has caps in the name or not, so I want to ignore the caps.
If i use:
find . -name 'word'
it won't find me file that is named WoRd.
I know I have to use -i somehow, but I just can't manage to find the correct syntax.
Thanks.
Please try this one. It will be solved your problem
$find . -iname 'word'
Where i for ignore case sensitive
Try this, -type f matches files only, you were correct to assume using the -i flag
find . -type f -print | grep -i "word"
Also check out this answer that goes deeper: https://unix.stackexchange.com/questions/40766/help-understanding-find-syntax-on-solaris

find command search pattern

I have below 4 files
a_ROLLBACK2to3__test.sql,
a_1to2__test.sql,
a_2to3__test.sql,
a_2to2__test.sql
I want to write a find command to return the files a_1to2__test.sql, a_2to3__test.sql and a_2to2__test.sql, the file a_ROLLBACK2to3__test.sql should not be included in the search.
my find command looks like
find . -name "*_*to*__*.sql"
but this returns all files but I don’t want a_ROLLBACK2to3__test.sql.
basically the files with ROLLBACK after the first _ should not be included..
Can anyone help me to write the search pattern for my requirement?
Thanks
Simply filter the results with grep:
find . -name '*_*to*__*.sql' | grep -v ROLLBACK
Or use the AND clause -a with negation !:
find . -name '*_*to*__*.sql' -a ! -name '*ROLLBACK*'
You could simply look for the underscore followed by a digit:
find . -name '*_[0-9]*to*__*.sql'
or for an underscore not followed by R:
find . -name '*_[!R]*to*__*.sql'

Find file without spaces in file name

I'm trying to find all *.cpp, using find files under current directory, which do not contain spaces in neither dirname nor basename. I understand I need to use -wholename flag, but I can not find an appropriate regex syntax.
Use find with a regex:
find . -type f -regex "[^ ]*.cpp"

Find in Linux combined with a search to return a particular line

I'm trying to return a particular line from files found from this search:
find . -name "database.php"
Each of these files contains a database name, next to a php variable like $dname=
I've been trying to use -exec to execute a grep search on this file with no success
-exec "grep {\}\ dbname"
Can anyone provide me with some understanding of how to accomplish this task?
I'm running CentOS 5, and there are about 100 database.php files stored in subdirectories on my server.
Thanks
Jason
You have the arguments to grep inverted, and you need them as separate arguments:
find . -name "database.php" -exec grep '$dbname' /dev/null {} +
The presence of /dev/null ensures that the file name(s) that match are listed as well as the lines that match.
I think this will do it. Not sure if you need to make any adjustments for CentOS.
find . -name "database.php" -exec grep dbname {} \;
I worked it out using xargs
find . -name "database.php" -print | xargs grep \'database\'\=\> > list_of_databases
Feel free to post a better way if you find one (or what some rep for a good answer)
I tend to habitually avoid find because I've never learned how to use it properly, so the way I'd accomplish your task would be:
grep dbname **/database.php
Edit: This command won't be viable in all cases because it can potentially generate a very long argument list, whereas find executes its command on found files one by one like xargs. And, as I noted in my comment, it's possibly not very portable. But it's damn short ;)

What's the best way to find a string/regex match in files recursively? (UNIX)

I have had to do this several times, usually when trying to find in what files a variable or a function is used.
I remember using xargs with grep in the past to do this, but I am wondering if there are any easier ways.
grep -r REGEX .
Replace . with whatever directory you want to search from.
The portable method* of doing this is
find . -type f -print0 | xargs -0 grep pattern
-print0 tells find to use ASCII nuls as the separator and -0 tells xargs the same thing. If you don't use them you will get errors on files and directories that contain spaces in their names.
* as opposed to grep -r, grep -R, or grep --recursive which only work on some machines.
This is one of the cases for which I've started using ack (http://petdance.com/ack/) in lieu of grep. From the site, you can get instructions to install it as a Perl CPAN component, or you can get a self-contained version that can be installed without dealing with dependencies.
Besides the fact that it defaults to recursive searching, it allows you to use Perl-strength regular expressions, use regex's to choose files to search, etc. It has an impressive list of options. I recommend visiting the site and checking it out. I've found it extremely easy to use, and there are tips for integrating it with vi(m), emacs, and even TextMate if you use that.
If you're looking for a string match, use
fgrep -r pattern .
which is faster than using grep.
More about the subject here: http://www.mkssoftware.com/docs/man1/grep.1.asp
grep -r if you're using GNU grep, which comes with most Linux distros.
On most UNIXes it's not installed by default so try this instead:
find . -type f | xargs grep regex
If you use the zsh shell you can use
grep REGEX **/*
or
grep REGEX **/*.java
This can run out of steam if there are too many matching files.
The canonical way though is to use find with exec.
find . -name '*.java' -exec grep REGEX {} \;
or
find . -type f -exec grep REGEX {} \;
The 'type f' bit just means type of file and will match all files.
I suggest changing the answer to:
grep REGEX -r .
The -r switch doesn't indicate regular expression. It tells grep to recurse into the directory provided.
This is a great way to find the exact expression recursively with one or more file types:
find . \\( -name '\''*.java'\'' -o -name '\''*.xml'\'' \\) | xargs egrep
(internal single quotes)
Where
-name '\''*.<filetype>'\'' -o
(again single quotes here)
is repeated in the parenthesis ( ) for how many more filetypes you want to add to your recursive search
an alias looks like this in bash
alias fnd='find . \\( -name '\''*.java'\'' -o -name '\''*.xml'\'' \\) | xargs egrep'

Resources