LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find and remove old files (https://www.linuxquestions.org/questions/linux-newbie-8/find-and-remove-old-files-4175411033/)

linuxandtsm 06-12-2012 09:35 AM

Find and remove old files
 
Hi all,

trying to remove older files more than 90 days old.
Want to make sure if below command is the right one to do that job. (i could have tested this but on test machine i don't have files older than 90 days and on the prod machine i want to make sure this works before applying it)
and also can somebody explain what {} \ will do at the end ?

Code:

find .  -name "*.txt" -mtime +90 -exec rm -rf {} \;

414N 06-12-2012 10:17 AM

Seems almost valid for your goal.
The {} is a placeholder for each of the files find finds. The \; is an escaped ; because the shell would, otherwise, expect another command to execute sequentially after find ends (e.g: cmdA; cmdB executes first cmdA then cmdB when cmdA ends).
Given that you just want to remove the files you could use the -delete command instead of -exec and rm:
Code:

find . -name "*.txt" -mtime +90 -delete
WARNING: make sure to execute that find command without -delete first to print out the files that would be removed before actually removing anything.

unSpawn 06-12-2012 10:21 AM

Quote:

Originally Posted by linuxandtsm (Post 4701491)
(i could have tested this but on test machine i don't have files older than 90 days

Well you could have created a test directory, touched some files, aged them say 3 days and use say "-mtime +3"... See http://www.linuxquestions.org/questi...5/#post4701512 for a shorter invocation.


Quote:

Originally Posted by linuxandtsm (Post 4701491)
and also can somebody explain what {} \ will do at the end ?

See 'man find' and search for "-exec command": the string '{}' is replaced by the current file name being processed.

linuxandtsm 06-13-2012 02:05 PM

Hi 414N and unSpawn, thanks for the replies.

If i use the following code, will it search in all directories under the path recursively to find ".txt" files and remove ?
Code:

find /Test -type f -name "*.txt" -mtime +90 -delete
and also how can i log everything to a file on what has been deleted and if there are any error (like permission denied etc)?
I want to run this as a cron job.

Thanks in advance!

414N 06-14-2012 03:46 AM

Of course. Like unSpawn already said, you should always test the command inside a test directory before putting it inside a cron job. Be aware that using -delete implies -depth, so all the sub-directories inside the specified path will be searched too.
If an error occurs during deletion, find prints an error message that you could log in a way like this:
Code:

find [...] &>> /path/where/to/log/file
This way, delete errors get appended to the log file you specify.
To log the files that got successfully deleted you can't use the -delete command anymore because it only prints a message when something goes wrong, but you could use something like this:
Code:

find /Test -type f -name "*.txt" -mtime +90 -exec rm -vf {} &>> /path/where/to/log/file \;
The -v option to rm makes it print a message every time a file is successfully deleted.
The -r rm option you previously specified is useless in this context because you're searching for regular files, not directories.

linuxandtsm 06-14-2012 12:18 PM

Thank you 414N,

I tried below just to make sure it redirects the output to a file but ended with an error.
Code:

# find . -type f -name "*.txt" -exec ls -ltrh {} &>> /root/listoffiles.txt \;
-bash: syntax error near unexpected token `>'

Could you please help me to find the error.

Kustom42 06-14-2012 03:15 PM

Put your \; before your >>

Kustom42 06-14-2012 03:16 PM

Code:

find . -type f -name "*.txt" -exec ls -ltrh {}  \; &>> /root/listoffiles.txt
The >> is to redirect the output which needs to come after the command and all of its options are terminated.

Kustom42 06-14-2012 03:29 PM

Heres your cron for you(I Got bored).

Add the following to your /etc/logrotate.conf modifying the rotate number variable to the number of days you want to maintain logs for:

Code:

/var/log/listofoldfiles.log {
        size 10k
        create 700 root root
        rotate 4 
}
/var/log/listofoldfilesremoved.log {
        size 10k
        create 700 root root
        rotate 4
}

Code:

#!/bin/bash/


filemtime=`stat -c %Y /var/log/listofoldfiles.log`
currtime=`date +%s`
diff=$(( (currtime - filemtime) / 86400 ))

if [ $diff -ge 1 ]
then
echo "Previous log file has not been rotated! Exiting cron."
exit 1
elif [ $diff -eq 0 ]
then
find /UPDATE/TO/YOUR/FULL/FOLDER/PATH -type f -name "*.txt" -mtime +90 -exec ls -ltrh {}  \; > /var/log/listofoldfiles.log
fi

for file in $(cat /var/log/listofoldfiles.log)
do
echo "Removing file $file" >> /var/log/listofoldfilesremoved.txt
rm -f $file
done



All times are GMT -5. The time now is 08:05 PM.