find command search pattern - linux

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'

Related

[linux find cmd]: limit the find command to search directories matching a given word or regex

Can I limit the find command to search directories matching a given regex only?
I looked at
$ man find
but couldn't find any --include-directories option.
Using RHEL GNU/Linux.
Thanks.
One way you can try is :
find . ! \( -name . -o -regex ".*/dir" \) -prune -name file
The problem for this solution is you have to include starting directory (. in this case)
-prune means to exclude all directories except . or .*/dir

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

How to find files which contains preferred character in Linux

How can I find files and directories which contains one character for example "a"
I know that there is command ls a* but this find all files which starts with character
Try the find command to copy files which begin with the "a" character.
find /your/source/path -name 'a*' -exec cp {} /your/target/path \;
To find files which contain the "a" character use the following command.
grep -r 'a' /your/source/path/* | xargs cp /your/target/path
As the others have mentioned,
find . -name '*a*'
should do what you are looking for.
However, note that this only looks for lowercase 'a'. If you want it to be case insensitive you can use
find . -iname '*a*'
The '*' is a wildcard and means that it matches any random selection of text. If for example you had instead written
find . -name 'a'
'find' will only find files with the name 'a', without any extensions.
Thus,
find . -name '*a'
will find all files ending with the letter 'a', while
find . -name 'a*'
will find all files starting with the letter 'a'.
find [root_of_the_search] -name [pattern]
for example
find . -name "*a*"
you can copy them like this
cp `find . -name "a"` /destination/path

Why does find . -name foo* fail?

OK, probably a stupid question, but why doesn't this work?
find . -name Orna* -ls
It seems that "find" should be able to handle this simple request... Am I missing something here? This should be a basic execution of the "find" command, but linux is being stupid, or maybe I am.
correct way of using Find Command are the following phrases
find . -type f -name "filename" # this command used to find files from the curent dir
find . -type d -name "dir name" # this command used to find dirs from the curent dir
find /. -type f -name "filename" # this command used to find files from the system
find /. -type d -name "dir name" # this command used to find dirs from the system
I wish it be a helpful for you
You need to quote the name parameter so the shell doesn't expand the wildcard, e.g.
find . -name "Orna*" -ls
To explain the "why" a little more than existing answers do -- wildcards are expanded by the shell before the command being invoked is run. Thus, let's say your current directory contains files Orna1 and Orna2.
In that case, when you run
find . -name Orna* -ls
...what's actually invoked by the shell is:
find . -name Orna1 Orna2 -ls
...thus, find never sees the wildcard expression at all!
Quoting the expansion, as in:
find . -name 'Orna*' -ls
...prevents the shell from trying to expand the wildcard before running your command, thus preventing this issue.

Ignore case when trying to match file names using find command in 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*'

Resources