LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-12-2012, 10:35 AM   #1
linuxandtsm
Member
 
Registered: May 2011
Posts: 194

Rep: Reputation: Disabled
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 {} \;

Last edited by linuxandtsm; 06-13-2012 at 03:51 PM.
 
Old 06-12-2012, 11:17 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651

Rep: Reputation: 190Reputation: 190
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.
 
Old 06-12-2012, 11:21 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by linuxandtsm View Post
(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 View Post
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.
 
Old 06-13-2012, 03:05 PM   #4
linuxandtsm
Member
 
Registered: May 2011
Posts: 194

Original Poster
Rep: Reputation: Disabled
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!

Last edited by linuxandtsm; 06-13-2012 at 03:51 PM.
 
Old 06-14-2012, 04:46 AM   #5
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651

Rep: Reputation: 190Reputation: 190
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.
 
Old 06-14-2012, 01:18 PM   #6
linuxandtsm
Member
 
Registered: May 2011
Posts: 194

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-14-2012, 04:15 PM   #7
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Put your \; before your >>
 
Old 06-14-2012, 04:16 PM   #8
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
Old 06-14-2012, 04:29 PM   #9
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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

Last edited by Kustom42; 06-14-2012 at 04:35 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to find and remove similar lines from multiple files linuxquestion1 Programming 9 07-13-2011 02:45 AM
[SOLVED] Find and delete option remove wrong files ZAMO Solaris / OpenSolaris 1 11-24-2010 05:56 AM
Help with `find -exec` : Remove selected files from a backup folder bobkatz Linux - Server 12 01-29-2010 11:29 AM
How to use find and remove folders and files Drigo Linux - Newbie 1 06-01-2009 12:53 AM
How to Find and remove 7 days before modified/created files neel.gurjar Linux - Software 2 05-14-2008 09:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration