Stop bash script when find gets to a folder with permission denied - bash

How can I stop script when command "find" gets to a folder with permission denied.
I make a list of all folders in my PC.
I would like to stop the searching process when "find" finds a permission denied folder.
DIRS=$(find . -type d)
Thanks a lot

Can't test this now, but you should check with exec if the folder is executable.
Something like
find -type d -print0 -not -exec test -x '{}' \; -quit

Related

Why does "find . -name somedir -exec rm -r {} \;" exit nonzero whenever it successfully deletes a directory?

I want to find a folder in a known folder but with unknown exact path, thus I must use find. When I find it, I use -exec to remove it, but I cannot evaluate if this has succeeded. Behavior somewhat confuses me; if I don't find the folder, I get return code 0:
>find . -name "testuuu" -exec rm -r {} \;
find: ‘./.docker’: Permission denied
>echo $?
0
But when I do find the folder and manage to delete it, it returns error code 1:
> find . -name "test" -exec rm -r {} \;
find: ‘./.docker’: Permission denied
find: ‘./test’: No such file or directory
> echo $?
1
Is this expected behavior? Why?
Further, how can I get return code of "rm" and not "find" and thus evaluate if the folder was deleted?
Use the -depth option to order a depth-first traversal -- that way find doesn't try to find things under your directory after the directory has already been deleted (an operation which, by nature, will always fail).
find . -depth -name "test" -exec rm -r -- '{}' \;
This option is also turned on by default when you use the -delete action in GNU find to delete content.
By the way -- if there are lots of test directories, you could get better performance by making that -exec rm -r -- {} + (which passes each copy of rm as many filenames as will fit on its command line) instead of -exec rm -r -- {} \; (which starts a new copy of rm for each test that is found), at the expense of no longer collecting individual error codes.

MacOS - One line command is working in terminal but not in .sh script?

MacOS High Sierra
I have a command that I currently use in terminal:
find /private/my/tmp -mindepth 1 -maxdepth 1 -type d -not -name -specialfolder -exec rm -r {} \;
which searches the folder private/my/tmp and removes all folders except one that is named "specialfolder".
This is working perfectly from the terminal, but I cannot get it working as .sh shell script. I have used chmod +x /scripts/myscript.sh to make it executable but this has not made any difference.
The shell script is:
#!/bin/sh
find /private/my/tmp -mindepth 1 -maxdepth 1 -type d -not -name -specialfolder -exec rm -r {} \;
Running it from the terminal as "sh myscript.sh" does not produce any result, no errors, nothing.
What do I need to fix to get this to work as a .sh script?
PS - I had considered it may be a permissions issue, but I have set permissions with chmod -R 777 on the /private/my/tmp folder and still no difference.

Linux find command permission denied

I want to filter out the unnecessary information "Permission denied".
these are outputs from command "find -type f -name sources.list"
find: './run/lxcfs': Permission denied
find: './run/sudo': Permission denied
find: './run/lvm': Permission denied
find: './tmp/systemd-private-99eef94819d84080adc7df3e60efee5b-systemd-timesyncd.service-HE48k9': Permission denied
find: './lost+found': Permission denied
find: './dev/vboxusb': Permission denied
find: './root': Permission denied
./etc/apt/sources.list
find: './etc/sudoers.d': Permission denied
I tried to use "! -readable -prune" in conjunction with the find command as above to suppress the "Permission denied" information, but it still doesn't work.
try the following
find -type f -name sources.list 2>/dev/null
This will redirect stderr output stream, which is used to report all errors, including the "Access denied" one, to null device.
Something like this should work
find -type d ! -readable -prune -o -type f -name sources.list
The following worked for me:
find / -mount -readable -name "<whatever>" -print
Here I only wanted to search the root file system, and not descent into any of the mounted file systems. Hence -mount.
The problem files that were throwing errors were not readable (yielding "permission denied"). Hence -readable.
The rest is obvious.
(Note: In Ubuntu 16.04 the files in /var/lib/lxcfs are not readable, even for root. The above solved the problem for me.)
First you can check the ACL of folders with the help of
getfacl -R /foldername
if read permission is there to particular folder,then run;
find foldername -type f -name sources.list
else
skip command

How to find and move from the current directory in bash

I'm trying to move a script from the directory I'm in to another directory after I have performed a find in the current directory. Although I don't get an error nothing happens. I don't know why. Can you help?
find . -name ScriptsFlowchart.xml -execdir mv {} Users/me/Desktop/SequencingScripts/{} \;
Try using -exec instead of -execdir and drop {} in target definition.
find . -name ScriptsFlowchart.xml -exec mv {} Users/me/Desktop/SequencingScripts/ \;
{} will retrieve the file path relative to current dir. So you must run the mv command from the current dir as well (using -exec).
From find manual page:
-execdir command ;
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.
If that's in a bash script, try reading $? right after running the command to check if there were any errors (if equals 0, runs successfully).

bash script rm cannot delete folder created by php mkdir

I cannot delete folder created by php mkdir
for I in `echo $*`
do
find $I -type f -name "sess_*" -exec rm -f {} \;
find $I -type f -name "*.bak" -exec rm -f {} \;
find $I -type f -name "Thumbs.db" -exec rm -f {} \;
find $I -type f -name "error.log" -exec sh -c 'echo -n > "{}"' -f {} \;
find $I -type f -path "*/cache/*" -name "*.*" -exec rm -f {} \;
find $I -path "*/uploads/*" -exec rm -rdf {} \;
done
I want to delete under /uploads/ all files and folders please help me thanks...
You should consider changing your find command to use the -o pragma to join your conditions together as the final exec is basically the same. This will avoid recursing the file system repeatedly.
The other answers address your concern about php mkdir. I'll just add that it has nothing to do with the fact it was created with php mkdir rather than any other code or command. It is due to the ownership and permissions.
I think this is most likely because php is running in apache or another http server under a different user than you are invoking the bash script. Or perhaps the files uploaded in uploads/ are owned by the http server's user and not the user invoking it.
Make sure that you run the bash script under the same user as your http server.
To find out which user owns which file do:
ls -l
If you run you bash script as root, you should be able to delete it anyway, but that is not recommended.
Update
To run it as root for nautilus script use the following as your nautilus script:
gksudo runmydeletescript
Then put all the other code into another file with the same path as whatever you have put for runmydeletescript and run chmod +x on it. This is extremely dangerous!
You should probably add -depth to the command to delete sub-directories of upload before the directory itself.
I worry about the -path but I'm not familiar with it.
Also consider using + instead of \; to reduce the number of commands executed.

Resources