combining results of two find commands linux - linux

I have a set of configuration backups that are organized like
backup/site1/10-11-2019
backup/site1/11-11-2019
backup/site1/12-11-2019
backup/site2/10-11-2019
backup/site2/11-11-2019
backup/site2/12-11-2019
backup/site3/10-11-2019
backup/site3/11-11-2019
backup/site3/12-11-2019
I need to list the configuration files that are less than specific size for one particular date for all sites. In a way so that I could combine following two commands from backup directory
find . -type d -name "11-11-2019"
find . -name "*.cfg" -size +500c
Please help me combining these two commands

Have you tried this?
find 11-11-2019 -name "*.cfg" -size +500c
Source: https://unix.stackexchange.com/questions/60849/find-files-in-multiple-folder-names
Can't check if it works right now :(

Thanks for the link. It gave me the hint to develop my command which serves my purpose.
find . \( -type f -and -path '*/2019-11-11/*' \) -name "*.cfg" -size -50c -print

Related

Using find to find files WITH a certain pattern and not other pattern

I want to use find to find files with _101_ in the name and not .jpg or .wsq extensions, but I cannot get this to work.
I tried things like this:
find . -type f -name '*_101_*' -o -not -name *.jpg -o -name *.wsq
but it doesn't work.
What am I doing wrong?
Your attempt does "matches _101_, or does not end in .jpg, or does not end in .wsq". That'll match every single file, based on the two extensions alone, as a file can only have one.
You have to group differently:
find . -type f -name '*_101_*' -not -name '*.jpg' -not -name '*.wsq'
This applies all rules (logical AND is implied).
You also should quote the parameters to -name, or they might be expanded by the shell instead of find.
You need to use parenthesis (or #Benjamin's solution)
otherwise a or not b or c is evaluated as (a or not b) or c.
And you need and instead of or to filter only files that satisfy both conditions (pass both tests). a and not (b or c)
find -name '*_101_*' -not \( -name '*.jpg' -o -name '*.wsq' \)

Replace a file if path of the file contains a string in Linux [duplicate]

I know how to find files using
find . -name "file_name"
But if I am given one part of a path, say "folder1/subfolder2/", how do I get all the full path that contains this partial path?
Example
partial path: folder1/subfolder2/
desire result:
/bob/folder1/subfolder2/yo/
/sandy/folder1/subfolder2/hi/
Use the -path option:
find . -path '*/folder1/subfolder2/*'
You may do it like below:
find . -path "*folder1/folder2" -prune -exec find {} -type f -name file.txt \;
With -prune you don't recurse after first match in a directory
This one worked for me (using bash)
ls -l /**/folder1/subfolder2/**
I came up with this, other solutions did not work for me,
find1 is a function
find1 ()
{
for file in `find -name $1`;
do
full_path=$PWD/$file;
echo $full_path;
done
}
If you dont want to stay posix-compliant, at least on Linux you can also use the -regex (and -regextype) option for this purpose.
For instance:
find folder/ -regextype posix-extended -regex "(.*/)?deer/(.*/)?beer"
will match
folder/deer/beer
folder/deer/dir/forest/beer/
folder/forest/deer/dir/forest/beer/
etc.
See linux man for details.

Exclude hidden files and folders in linux find

I am trying to exclude hidden files and folders when doing a find in linux.
I have to exclude files or folders that start with a dot (.hidden) but also have to exclude folders that start with an # (like #eaDir).
So far I have the following command which seems to work but maybe there is a more elegant way?
find /path/to/start/search/ -not -path '*#eaDir*' -not -path "*/\.*" -type f -mtime -2
I did see examples using regular expression like so:
find . \( ! -regex '.*/\..*' \) -type f
but not sure how I would also exclude #eaDir directories with the -regexoption?
I believe there can also be hidden files that start with two dots? like "..hidden"? Is this already covered with my command or would I simply add a third option like -not -path "*/\..*" to exclude those as well?
Then I saw some examples of using -prune so that find won't descend in hidden directories, however I am unsure how I would use this correclty in my example. I would be interested in this to speed things up.
Thanks!
Use -not -name '.*'. This will exclude any names that begin with ..
Exclude files and folders starting with a . or an #:
find /path/to/start/search/ -not -path '*/[#.]*' -type f -mtime -2
Exclude files starting with a . and files and folders starting with a . or an #:
find /path/to/start/search/ -not -path '*/.*' -type f -mtime -2 | grep -v '/#.*/.*'

I want to know exact command of "find . -name '*.c' -or -name '*.cpp'" in Linux

I'm studying shell in Linux these days. and I've had one question.
Please, look at below command:
$ find . -name '*.c' -or -name '*.cpp'
Exact command of above command is processed like below command?
$ find . -name '*.c' -and -print -or -name '*.cpp' -and -print
You are combining different search expressions with the logical operator or.
Basically your command will find all files in the current directory ending with .c or .cppand will print them to STDOUT.
For further info check the man page of find command.
Also note that this question would be more suitable to ask here.

Is there a way to specify exceptions to exclusions in the find command via Bash?

I would like to find all of the files within a directory and its subdirectories except for any settings files and anything in settings or dependency directories.
For example, I want to exclude from my results entire directories like .git, .idea, and node_modules as well as files like .DS_Store and config.codekit, but I want to include .gitignore.
What I want is something like the results of the following Git command, but including any untracked files and able to be easily and safely operated upon (e.g., to change permissions).
git ls-tree -r master --name-only
Here is what I have so far and although it is rather unwieldy it seems to mostly do what I want except for leaving out .gitignore:
find . -type f -not -name ".*" -not -name config.codekit -not -path "./.*" -not -path "./node_modules/*"
I have experimented with -prune without much success.
Is there a way to specify exceptions to exclusions in the find command via Bash—to say something like exclude all the things that match this pattern EXCEPT this thing or these things?
By the way, I am presently using OS X, but I also use Ubuntu and I plan to try Ubuntu on Windows when the Windows 10 Anniversary Update is generally available, so ideally I would like to have a command that works across all of those.
Thank you in advance for any solutions, insights, or optimizations!
Update
Thanks to help from gniourf-gniourf, I have revised my command. This seems to do what I wanted:
find . -type f \( \! -name ".*" \! -name config.codekit \! -path "./.*" \! -path "./node_modules/*" -o -name .gitignore \)
A quick example first: to find all files, pruning the .git directories and ignoring the .DS_Store files:
find . -name .git -type d \! -prune -o \! -name .DS_Store -type f
For example, I want to exclude from my results entire directories like .git, .idea, and node_modules as well as files like .DS_Store and config.codekit, but I want to include .gitignore.
find . \( -name .git -o -name .idea -o -name node_modules \) -type d \! -prune -o \! -name .DS_Store \! -name config.codekit -type f
When building your command, make sure you stick with the POSIX standard: it's a guarantee that your command will work on any (POSIX compliant) system. For example, -not is not POSIX compliant: use ! instead (you'll have to escape it so as to not clash with your shell history expansion).
Is there a way to specify exceptions to exclusions in the find command via Bash—to say something like exclude all the things that match this pattern EXCEPT this thing or these things?
Find files, excluding everything (pattern *) except the files one and two:
find . \( \! -name '*' -o -name one -o -name two \) -type f

Resources