find and remove only if the file is present - shell

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

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.

How to make this bash script NOT delete all my files?

I have a cron job, every 5 minutes, backing up my MYSQL to files ending in .sql.gz. But this is hundreds of files a day. So I searched the internet and found this bash script which I expected to just work on the files in the /backup folder specified and only on .sql.gz files. but I soon found that it deleted everything in my root folder. :-) I was able to FTP the files back and have my site back up in half an hour, but I still need the script to work as intended. I'm new to bash scripting so I'm asking what did I do wrong in editing the script I found on the internet to my needs? What would work?
Here is the rogue script. DO NOT run this as is. its broken, thats why im here:
find /home/user/backups/*.gz * -mmin +60 -exec rm {} \;
Im suspecting its that last backslash should be /home/user/backups/
And also I should remove the * before -min
so what I need should be:
find /home/user/backups/*.gz -mmin +60 -exec rm {} /home/user/backups/;
Am I correct? Or still missing something?
BTW Im running this on Dreamhost shared hosting CRON. Their support don't want to help with BASH questions really, I tried.
The filename arguments to find should be the directories to start the recursive search. Then use -name and other options to filter down to the files that match the criteria you want.
find /home/user/backups -type f -name '*.sql.gz' -mmin +60 -exec rm {} +
-type f means only select ordinary files
-name '*.sql.gz' means only filenames ending in .sql.gz
-mmin +60 means files more than 60 minutes old
And using + instead of \; at the end of -exec means that it should just run the command once with all the selected filenames, rather than separately for each filename; this is a minor efficiency improvement.

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

Error with Unix shell script Find and delete based on hours

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

Deleting files from an AIX system

We have an AIX system, which gets files on a daily basis, so we manually delete the previous days files manually. Is it possible to write a script which will take the files 15 or 20 days before today and delete the files from the folder?
Or you can use native AIX find command:
find /dir/to/files -type f -mtime +15 -exec rm {} \;
where:
-type f - Find only files, not directories
-mtime +15 - Find files, that modification time more then 15 days
-exec rm {} \; - Run command rm on each matched file
You can run this command with -exec ls -l {} \; for testing, that found files correspond to your criteria.
If you can/may install GNU!find, them it's simple, e.g.:
#!/bin/sh
cd /var/log/apache
gfind . -name '*log*Z' -mtime +30 -delete
this script is run by cron; a line from crontab:
02 23 1 * * /root/cmd/httpd.logdelete >/dev/null 2>&1
Edit:
-mdays + means files of which last modification date is earlier than now-
-delete means deleting the files that match the criteria

Resources