Error with Unix shell script Find and delete based on hours - shell

I'm trying to delete a folder contents in unix based on the date created using the following statement in a shell script.
find /mypath -mmin +$((60*24*1)) -exec rm -rf {} \;
I have configured to run this script from Control M. This deletes the folder however the script ends with the error code 1. How can i avoid getting the error so that my job does not get failed?
find: '/mypath/Xdb/20170802_001028': No such file or directory
find: '/mypath/Xdb/20170802_001027': No such file or directory
find: '/mypath/Xdb/20170801_142539': No such file or directory

I don't understand why do you use
min +$((60*24*1))
use this:
find /mypath -mtime +1 -exec rm -rf {} \;
or this removed directory without checking it is empty or not
find /mypath -mtime +7 -type d -print0 | xargs -0 rm -rf
+7 remove older then , -7 removes from today to 7

In addition to the answer from SamOl, if you want to stick with your original command then you can tell the Control-M to accept the string No such file or directory as "OK".
To do this simply add an On/Do Action in the last tab of the job def =
On Do Actions
Specific statement output
Statement = *
Code = *No such file or directory*
Do = Set job to OK
There is a YouTube clip here - https://www.youtube.com/watch?v=Y3S7GdAwjQ8

Related

How to concatenate string to -print in unix find command?

I have this command to run in Unix machine where I'm deleting all directories from specific folder if they are older then 1 day:
find /path_with_dirs_to_delete/* -type d -mtime +1 -prune -exec rm -rf {} + -print
It's currently printing out the folder names and i want to concatenate to the printing output the text " was deleted successfully".
How can I do that?
If using GNU tools, a superior solution is to add the -v (verbose) flag to rm.
For a POSIX solution you could do this:
find /path/* -type d -mtime +1 -prune -exec rm -rf {} \; -exec echo {} was deleted successfully \;
You were running a single rm to remove multiple directories. But if you want to test rm's success for each directory, it has to run once per directory (note ; instead of +). Note that this a bit less efficient.
-a ("and") is implied between find actions, meaning the second -exec action (echo) only runs if the previous one (rm) succeds. So this can be used to log a successful removal.
You will receive an error message from rm if a removal fails (eg. permission denied).
rm -f suppresses "does not exist" errors, and you wouldn't get a success message either, however find won't pass a non-existent directory.

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.

Delete files in linux without "No such file and Directory" and "Permissions Denied" errors

I have set up a bamboo job to delete files and folders on a server at a particular location for files/folders older than 15 days and the job runs fine and deletes the files/folders but then the shell command exits with code 1 due to "Permissions denied" and "No such files or directory" errors which are always expected and hence the whole build fails. We want it to report success.
Is there a way to skip these errors and delete files modified older than 15 days for those files only where the shell command can return always 0.
find ./* -type d -ctime +15 -exec rm -rf {} \;
Since the exit code of a script is that of its last command, you can add any "true" command in the end. There's true, as well as : though tricky:
find ./* -type d -ctime +15 -exec rm -rf {} \;
: always true here
For the explanation of :, See Bourne Shell Builtins.
You could try something like:
find ./* -type d -ctime +15 -exec rm -rf {} \; || true

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).

find and remove only if the file is present

I am working on a shell script to clear out files from a folder if they are older than 60 days. I am using find command to do that. I have one more requirement in that as if the file exist then the script can remove such files. If no files are there which are older than 60 days, i don't want my command to give any error messages.
find AA* -mtime +60 -exec rm {} \;
I am using above given command. It gives the error message "No such file or directory" if there is no files are older than 60 days as well as if they are not starts with "AA". I don't want any such messages reported for my command.
Please help.
TIA.
Perhaps you are missing the target, i. e. AA* files in current directory?
How about changing it to
find . -name 'AA*' -mtime +60 -exec rm {} \;
or if you need to check only files or directories starting from 'AA' you could use -regex:
find . -regex './AA.*' -mtime +60 -exec rm {} \;
I don't want any such messages reported for my command.
Redirect command's standard error to /dev/null
... 2>/dev/null

Resources