Use find command with -perm and -maxdepth [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
When I Enter this command:
$ find . -perm 777 -maxdepth 1
The following error occures:
find: warning: you have specified the -maxdepth option after a non-option argument -perm, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
What does that mean?

The order of find arguments is very important, because they are evaluated as a boolean expression left-to-right with short circuiting:
# Deletes *.tmp files
find . -name '*.tmp' -delete
# Deletes ALL file, because -delete is performed before -name
find . -delete -name '*.tmp'
However, -maxdepth does not behave like this. -maxdepth is an option that changes how find works, so it applies the same no matter where it's placed:
# Deletes all '*.tmp' files, but only in the current dir, not subdirs
find . -maxdepth 1 -name '*.tmp' -delete
# Deletes all '*.tmp' files, still only in the current dir
find . -name '*.tmp' -delete -maxdepth 1
Since you put the -maxdepth 1 after a -perm 777, it looks like you are trying to make -maxdepth only apply to certain files. Since this is not possible, find prints this warning.
It suggests that you rewrite it into find . -maxdepth 1 -perm 777 to make it clear that you intended -maxdepth to apply to everything.

Related

Why do I need '-o' in linux find command? [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 months ago.
Improve this question
I want to list files with certain name pattern under certain directory, and excluding certain sub-directory.
By doing
find "../../" -path "../../backup" -prune -regex "\.*\.v" -print
nothing is outputted.
But by adding -o
find "../../" -path "../../backup" -prune -o -regex "\.*\.v" -print
I get the correct results.
-o means or. But I don't think there is an or logic in my requirements, I think it should be and?
file name with certain pattern & under certain directory & not under certain sub-directory
Am I doing something wrong?
From the find man page:
-prune True; if the file is a directory, do not descend into it.
If -depth is given, false; no effect.
expr1 -o expr2
Or; expr2 is not evaluated if expr1 is true.
The construct -prune -o \( ... -print0 \) is quite common.
The idea here is that the expression before -prune matches
things which are to be pruned.
However, the -prune action itself returns true, so the following
-o ensures that the right hand side is evaluated only for
those directories which didn't get pruned (the contents of
the pruned directories are not even visited, so their contents are irrelevant).

No such file or directory find command on linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I've created script which delete old backup file from directory but this command was worked fine before 1 week and Nothing change on script or packages but still getting below error:
root#:# find /var/backups/abc/* -type d -mtime +6
/var/backups/abc/2016-03-09_0321
root#:~# find /var/backups/abc/* -type d -mtime +6 -exec rm -rf {} \;
find: `/var/backups/abc/2016-03-08_0321': No such file or directory
Problem is that, this script run every day on cron, I getting a mail like " find: `/var/backups/abc/2016-03-08_0321': No such file or directory". files are deleted but such a mails are getting from root.
find /var/backups/abc/* -type d -mtime +6 -prune -exec rm -rf {} \;
Here, we useĀ -pruneĀ on the directories that we're about to delete, so find will then not try to read their contents.
This is because of after having returned your directory, find will try to look in it (to continue his recursive search), and will fail because of you just removed it.

linux pipe argument list too long [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I use the following bash script to remove files older than $days.
find /home/xxx/conf_* -maxdepth 0 -mindepth 0 -type d -ctime +5 -exec rm -rf {} \;
However if the files are more than 32000+, I get
/usr/bin/find: Argument list too long
how do I trim the list down to like 20000 only?
From comment to answer:
Your problem is the glob expansion but you are already using a tool that can perfectly well handle an arbitrary number of found results, namely find. As such you should use a glob at all. Instead you should let find do all the work.
Something like:
find /home/xxx -maxdepth 1 -name 'conf_*' -type d -ctime +5 -exec rm -rf {} \;
Also if your find has -exec \+ you should probably use this instead:
find /home/xxx -maxdepth 1 -name 'conf_*' -type d -ctime +5 -exec rm -rf {} \+
For such a large number of matching directories I imagine the significantly reduced amount of executions of rm might be significantly more efficient.

chmod exclusions [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I know I should assign a group and then set an umask so that groups writable permissions persist but for whatever reason I can't do this. I need to chmod recursively a directory except one sub folder (web10), would the following work?
cd /var/www/clients/
find . -type f -not -path "*web10*" -exec chmod 777 '{}' \;
If you want to exclude files or directories, you use -prune
find /var/www/clients/ -name web10 -type d -prune -o -type f -print0 | xargs -0 chmod 0640
You should also use xargs where possible. With -exec you call the command once for every file found, whereas xargs collects as many files as possible and calls the command once for N files, resulting in a more efficient execution and better performance.

Setting file permission for Files and Directories [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
I need to apply below permission policies to my files under www folder
664 to all files in www recursively,
755 to all directories under www recursively
I tried
find . -type f -exec chmod 644 {} ;
find . -type d -exec chmod 755 {} ;
But always getting error
find: missing argument to `-exec'
What is the solution?
Backslash before semi-colon (or quotes around it):
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
The shell sees the semi-colon you typed as the end of the command and does not pass it to find, which then complains that it is missing.
Use backslash before ';'
find . -type f -exec chmod 644 {} \;

Resources