Find directory pattern - regex

I am trying to get all the zip files that are present under the directory named "target".
I want to search recursively under a given directory for this file pattern - "target/.zip"
This is the command that I have:
find . -type f \( -path '*/target/*' -a -name '*.zip' \)
This one gives me answers that don't strictly match my criteria.
This one also matches files like "target/foo/bar.zip"
I want to prune these results and only get the zip files that are present directly under the target directory. can someone help me with one?

How about this:
$ find . -type f ! -path '*/target/*/*.zip' -a -path '*/target/*.zip'

Related

How to ignore file with .<numberic>.ext in git?

I have a list of file in my project:
For example:
1. src/index.1.js
2. src/screens/index.1.js
3. src/screens/index.2.js
I want to ignore all the files having the numeric number.
I have tried using **/*.1.* , **/*.2.*. Is there a way to ignore all the file with numeric value?
You can use a range. For your example:
**/*.[0-9].js
Would match a js file in any directory that ends with .(number).js
Git uses glob pattern to match ignored files. Use the following to ignore all such above-mentioned files (with multi-digit numbers also).
**/*.[0-9]*.js
Why don't you run the following find command after eventually adapting the \.js part if you do not want to take into account only the .js files:
find . -type f -regextype sed -regex '.*\/.*\.[0-9]\+\.js'
./src/screens/index.2.js
./src/screens/index.123.js
./src/index.1.js
when you find all the files you are interested in, change your find command into:
find . -type f -regextype sed -regex '.*\/.*\.[0-9]\+\.js' -exec git checkout {} \;
to checkout those files.

Searching files in directory using regex and grep

So I have to find every file in the /etc directory that start with a,b or c
what i have is: grep -l '/^[a-cA-C].*/g' /etc/* though i keep getting every file in the /etc directory.
I use grep -lto get every file (I guess using find or grep doesn't matter
'/^[a-cA-C].*/g' to find everything that starts with a,b or c uppercase or lowercase followed by zero or more characters ending with a global search so it doesn't stop after the first match
I know the regex is right cause i've checked it with a regex-checker online.
EDIT: found the solution --> ls /etc/[a-cA-C]*
Here my example:
find ./ -type f -exec basename {} \; | grep -Ei '^(a|b|c)'
It search recursively and find all files, but return in output only basename of file, is it ok for you?
You can try this one:
find | grep '^\./[abc]'

Script for deleting files whose name do not contain certain phrases?

If I had a folder of files, what script could I write to remove the files whose names don't have certain phrases?
My folder contains
oneApple.zip
twoApples.zip
threeApples.zip
fourApples.zip
I would want to remove the files whose names don't contain "one" or "three" anywhere within their filename.
After executing the script, the folder would only contain:
oneApple.zip
threeApples.zip
Using bash
With a modern bash with extglob enabled, we can delete files whose names do not contain one or three with:
rm !(*one*|*three*)
To experiment with how extglobs work, just use echo:
$ echo !(*one*|*three*)
fourApples.zip twoApples.zip
If the above doesn't work properly, then either your bash is out of date or extglob is turned off. To turn it on:
shopt -s extglob
Using find
find . -maxdepth 1 -type f ! -name '*one*' ! -name '*three*' -delete
Before running that command, you probably want to test it. Just remove the -delete and it will show you the files that it found:
$ find . -maxdepth 1 -type f ! -name '*one*' ! -name '*three*'
./twoApples.zip
./fourApples.zip
How it works:
.
This tells find to look in the current directory.
-maxdepth 1
This tells find not to recurse into subdirectories
-type f
This tells find that we only want regular files.
! -name '*one*'
This tells find to exclude files with one in their name.
! -name '*three*'
This tells find to exclude files with three in their name.
-delete
This tells find to delete the files that it found.

How to use grep to find in a directory by a regex?

I tried
grep -R '.*invalidTemplateName.*' -regex './online_admin/.*/UTF-8/.*'
to find all occurences of possible mathces of the '.invalidTemplateName.' regex within a directory regex pattern './online_admin/.*/UTF-8/.*', but it doesn't work. I got the message:
grep: ./online_admin/.*/UTF-8/.*: No such file or directory
If I use
grep -R '.*invalidTemplateName.*' .
it looks up in all subdirectory of the current directory that's overwhelming. How can I specify a directory pattern in grep? Is it possible?
Find might be a better choice here:
find ./online_admin/*/UTF-8/* -type f -exec grep -H "invalidTemplateName" {} \;
Find will locate all files in the locations you want, including subdirs of UTF-8 and then execute grep on each file. the -H argument ensures the filename will be printed along with the match. If you want only the filename, use the -L switch instead.
with find you could do something like that:
find /abs/path/to/directory -maxdepth 1 -name '.*invalidTemplateName.*'
using the name argument you can directly filter by names. you can also use wildcards for the filter-string.
using the maxdepth argument you can specify the level of recursion to look up the files. 1 means to look up in /abs/path/to/directory, 2 means to look up in /abs/path/to/directory and in the first level of directories in /abs/path/to/directory as well.

How to exclude a directory in a recursive search using grep?

How to do a recursive search using grep while excluding a particular directory ?
Background : I have a large directory consisting of log files which I would like to eliminate in the search. The easiest way is to move the log folder. Unfortunately I cannot do that, as the project mandates the location.
Any idea how to do it ?
are you looking for this?
from grep man page:
--exclude-dir=DIR
Exclude directories matching the pattern DIR from recursive searches.
As an alternate, if you can use find in your search, it may also be useful:
find [directory] -name "*.log" -prune -o -type f -print|grep ...
The [directory] can actually be the current directory if you want (just a . will do).
The next part, -name "*.log" -prune is all together. It searches for filenames with the pattern *.log and will strip them OUT of your results.
Next is -o (for "or")
Then, -type f -print which says "print (to stdout) any type that is a file."
Those results should include every file (no directories are returned) found in [directory] except those that end in .log. Then you can grep the results as you need.