Linux find command gotcha? - find

Hi I am trying to find all js & css files in one find command. I tried all of the below but in vain:
find WebContent -name "*.[jc]ss?"
find WebContent -name "*.[jc]ss{0,1}"
find WebContent -name "*.[jc][s]{1,2}$"
find WebContent -name "*.[jc]s{1,2}"
find WebContent -name "*.[jc]s[s]?"
Now what??

-name accepts arguments that are globs, not regular expressions. You could use -regex if you wanted to use regular expressions, but the -o option (meaning "or") is probably the simplest solution:
find WebContent -name "*.js" -o -name "*.css"

Try this
find WebContent -regextype posix-extended -regex '.*\.(css|js)$'

use the -iname option case insensitive
find WebContent \( -iname "*.js" -o -iname "*.css" \)

you can do boolean expressions with find. -o stands for OR.
find -name "*.js" -o -name "*.cpp"

Related

Running Find command from the directory vs. another directory

I am doing something wrong but cannot figure out what it is. I am trying to move all files in the folder (folder A) to another folder (folder B) without any subdirectories and excluding certain files
When I am in the folder I run...
cd folderA
find . -path './*' -prune -not \( -name "file1.php" -o -name "file2.txt" \) -type f -exec mv -f {} ~/folderB/ \;
That works great. But I want to run the above find command in a bash script and do no want to have to go to the folder so I tried...
find ~/folderA/ -path './*' -prune -not \( -name "file1.php" -o -name "file2.txt" \) -type f -exec mv -f {} ~/folderB/ \;
And nothing! What am I doing wrong.
Thanks in Advance
After stripping the above command down, I found the issue. for some reason -path './*' -prune does not work outside of the direct folder. Here is the command I used to get it to work outside the folder itself...
find ~/folderA/ -maxdepth 1 -not \( -name "file1.php" -o -name "file2.txt" \) -type f -exec mv -f {} ~/folderB/ \;
Again, not sure way -maxdepth 1 would work over the other method, but it did.
FYI... if you want to go further down into the subdirectories you can by changing the 1 value

SunOS. Find files that do not contain the substring 'foo' in the file name

Now I exclude files by their extension.
find ./export/home/ ! \( -name *.log -o -name *.out -o -name *.tmp \)
But I also want to exclude files with the name containing some string 'foo'.
Need some sort of analog " like '%foo%' " (PL\SQL), to exclude files such as "1_foo2".
I can not use the GNU version of the command "find".
You are pretty close:
find ./export/home/ -type f ! \( -name "*.log" -o -name "*.out" -o
-name "*.tmp" -o -name "*foo*" \)

clearcase: find -name not allow multiple patterns?

I wanna find *.cs and *.cpp files through cleartool find command. But it failed.
cleartool find "M:\test_view\code" -name "*.cs *.cpp" -print
Nothing can be found based on above even there are matched files in that folder.
How to set multiple file-name patterns ?
The query language offer some possibility for Compound queries (query || query)
But the cleartool find has none of those operators for the -name option.
The best you can do, following the cleartool wildcard syntax, is
cleartool find "M:\test_view\code" -name "*.c[sp]*" -print
This is a bit late but perhaps this will help someone.
One option is to wrap this is a for loop:
:: namelist.txt contains a list of file types ( *.cs, *.cpp, )
FOR /F "tokens=1" %%A IN (c:\bin\namelist.txt) DO ( cleartool find "M:\test_view\code" -all -type f -name %%A -print)
It lookslike cleartool wraps the unix style find utility.
If that is right you might be able to use '-or'
$ find -type f -name '*.cs' -or '*.cpp' -print

How do I prevent find from printing .git folders?

I have a find command that I run to find files whose names contain foo.
I want to skip the .git directory. The command below works except it prints an
annoying .git any time it skips a .git directory:
find . ( -name .git ) -prune -o -name '*foo*'
How can I prevent the skipped .git directories from
printing to the standard output?
So just for better visibility:
find -name '.git*' -prune -o -name '*foo*' -print
This also omits .gitignore files; note the trailing -print to omit printing, -prune stops descending into it but without -print prints it nevertheless. Twisted C;
find . -not -wholename "./.git*" -name "*foo*"
or, more strictly, if you don't want to see .git/ but do want to search in other dirs whose name also begins with .git (.git-foo/bar/...)
find . -not -wholename "./.git" -not -wholename "./.git/*" -name "*foo*"
If your .git/ directories may not always necessarily be located at the top-level of your search directory, you will want to use -not -wholename ".*/.git" and -not -wholename ".*/.git/*".
A bit odder but more efficient because it prunes the whole .git dir:
find . -not \( -wholename "./.git" -prune \) -name "*foo*"
Try this one:
find . -name '*foo*' | grep -v '\.git'
This will still traverse into the .git directories, but won't display them. Or you can combine with your version:
find . ( -name .git ) -prune -o -name '*foo*' | grep -v '\.git'
You can also do it without grep:
find . ( -name .git ) -prune -printf '' -o -name '*foo*' -print

Solaris find command

In Solaris, what is the syntax of find command to find files having multiple file name formats?
Eg: In my current directory and in its sub-directories if I have files like test.log, sample.out, demo.buf and some other files, how can I write single find command to find these 3 files.
this is the correct one
find . \( -name "test.log" -o -name "sample.out" -o -name "demo.buf" \) -print
Same as all other Unixes:
find . -name test.log -o -name sample.out -o name demo.buf