Omit a directory when searching with find [duplicate] - linux

This question already has answers here:
How do I exclude a directory when using `find`?
(46 answers)
Closed 1 year ago.
Searching whole system using:
find / -type f -size +100M -exec ls -lh {} \; | awk '{ print $9 "|| Size : " $5 }'
How can I omit a directory? I see -prune -o, but I am not sure how to format the option.

find / -path /path/to/ommit -prune -o -type f -size +100M -exec ...
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From man:
To ignore a whole directory tree, use -prune rather
than checking every file in the tree. For example, to skip the
directory src/emacs and all files and directories under it,
and print the names of the other files found, do something like
this:
find . -path ./src/emacs -prune -o -print
Note that the pattern match test applies to the whole file name,
starting from one of the start points named on the command line.

Related

Linux deleteing a folder's content

I can find ./ -type d -name "Debug" -exec rm -f {} + to delete the fold and content.
My question is: How to fine all "Debug" folders and ONLY delete the content and Not the folder?
#!/bin/bash
find . -type d -name "Debug" -print | xargs -I% find % -maxdepth 1 -type f -delete
the first find lists all Debug directories
xargs defines the "%" symbol as what is replaced by the received stdin. You will often see -I{} for xargs, but I chose another string since you might need {} in the find. Any char will do.
the second find, in directory "%" (so here one of the Debug directories received from the first find), deletes all files under that directory.
-maxdepth 1 is used in the second find to make sure it only deletes the files in the current Debug directory, and does not recursively deletes all files.

Use Linux Find to search for directories that contain a file with properties

I'm trying to find projects in an enormous directory. The projects are always several levels of depth in and have a config file which contains the project name. So basically...
Given a path and string
Return any directory that has a depth of 3 from the and contains a file named "config"
that contains the
I learned that find combined with grep will work... but print out the grepped text and not the path of it's parent directory
find <starting-dir> -maxdepth 3 -mindepth 3 -type d -exec grep '<project-name>' {}/config \;
Just prints out the project name :(
Perhaps there any way to switch back to find's default behaviour of printing out the found file path only if the grep is successful? Or is there another tool I should try to use to solve this?
To get -print, you need to add it explicitly after a succesful -exec.
For example, using grep's -q:
find <starting-dir> \
-maxdepth 3 -mindepth 3 \
-type d \
-exec grep -q '<project-name>' {}/config \; \
-print
As you discovered, grep already has -l.
You can reduce the number of grep processes:
find <starting-dir> \
-maxdepth 4 -mindepth 4 \
-type f -name config \
-exec grep -l '<project-name>' {} +
Adding the -l flag to my output fixes the issue, for some reason I thought that would just print out "config" and not the whole path of that config file, but here we are.
find <starting-dir> -maxdepth 3 -mindepth 3 -type d -exec grep -l '<project-name>' {}/config \;
This will print out the full path of the config file of the project you search for.

Count files, except the hidden files [duplicate]

This question already has answers here:
Why does find . -not -name ".*" not exclude hidden files?
(2 answers)
How to count all files inside a folder, its subfolder and all . The count should not include folder count [closed]
(2 answers)
Closed 4 years ago.
I'm trying to count all the files from the current folder and each of it's sub-folders.
find . -type f \(! -iname ".*" \) -print
You can use this find command:
find -type f ! -regex ".*/\.[^/]+$" | wc -l
It will find all files in the current directory with filename not starting with a ., aka hidden files.
find . -type f -not -path "./.*" | wc -l
explanation:
find in the current directory(.) all things from type file(f) which does not have a path that starts with ./.(./ is prepend on every output item of find which means current directory) and then give the output to wc which is a program to count and the -l parameter tells wc to count lines.
Just pip wc -l to it like so:
find . -type f -not -path "*/\.*" | wc -l

Find all files contained into directory named

I would like to recursively find all files contained into a directory that has name “name1” or name “name2”
for instance:
structure/of/dir/name1/file1.a
structure/of/dir/name1/file2.b
structure/of/dir/name1/file3.c
structure/of/dir/name1/subfolder/file1s.a
structure/of/dir/name1/subfolder/file2s.b
structure/of/dir/name2/file1.a
structure/of/dir/name2/file2.b
structure/of/dir/name2/file3.c
structure/of/dir/name2/subfolder/file1s.a
structure/of/dir/name2/subfolder/file2s.b
structure/of/dir/name3/name1.a ←this should not show up in the result
structure/of/dir/name3/name2.a ←this should not show up in the result
so when I start my magic command the expected output should be this and only this:
structure/of/dir/name1/file1.a
structure/of/dir/name1/file2.b
structure/of/dir/name1/file3.c
structure/of/dir/name2/file1.a
structure/of/dir/name2/file2.b
structure/of/dir/name2/file3.c
I scripted something but it does not work because it search within the files and not only folder names:
for entry in $(find $SEARCH_DIR -type f | grep 'name1\|name2');
do
echo "FileName: $(basename $entry)"
done
If you can use the -regex option, avoiding subfolders with [^/]:
~$ find . -type f -regex ".*name1/[^/]*" -o -regex ".*name2/[^/]*"
./structure/of/dir/name2/file1.a
./structure/of/dir/name2/file3.c
./structure/of/dir/name2/subfolder
./structure/of/dir/name2/file2.b
./structure/of/dir/name1/file1.a
./structure/of/dir/name1/file3.c
./structure/of/dir/name1/file2.b
I'd use -path and -prune for this, since it's standard (unlike -regex which is GNU specific).
find . \( -path "*/name1/*" -o -path "*/name2/*" \) -prune -type f -print
But more importantly, never do for file in $(find...). Use finds -exec or a while read loop instead, depending on what you really need to with the matching files. See UsingFind and BashFAQ 20 for more on how to handle find safely.

Want to find any reference in any file to a certain string in linux [duplicate]

This question already has answers here:
how to find files containing a string using egrep
(7 answers)
Closed 8 years ago.
I am trying to search All .PHP files or ALL .SH files for any reference that contains:
'into tbl_free_minutes_mar'
I have command line access to the server but the files may be scattered in different directories.
For all directories everywhere,
find / -type f \( -name '*.php' -o -name '*.sh' \) \
-exec fgrep 'into tbl_free_minutes_mar' {} \+
For fewer directories elsewhere, just give a list of paths instead of /. To just list the matching files, try fgrep -l. If your file names might not always match the wildcards in the -name conditions, maybe scan all files.
find / -type f \( -name \*.php -o -name \*.sh \) -exec grep 'into tbl_free_minutes_mar' {} /dev/null \;
Change find / ... to to something less all-encompassing if you know the general area that you want to look in, e.g. find /home ...
Provided /base/path is the path where you want to start looking this will get you a list of files:
find /base/path -type f -iregex '.*\.\(php\|sh\)$' -exec grep -l 'into tbl_free_minutes_mar' '{}' \;

Resources