script to delete contents in a directory by date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to delete contents in a directory by date
# 1  
Old 01-17-2011
script to delete contents in a directory by date

Hi ,

I am trying to make a cron job to delete the contents of a directory [basically log files] in a linux environment.The contents created before 2 days should get deleted by this job.Here is what i have written :

/usr/bin/find / -name *.log -ctime +2 -exec /bin/rm -f {} \;


Is it correct....If not so,please tell me the ammendments.

Thx in advance.
# 2  
Old 01-17-2011
Two small things....firstly "ctime" is not the "create" time but the "change" time - probably best to read the man page to make sure you understand the difference between mtime and ctime. Secondly you will probably need to escape or quote the wildcard, eg:
Code:
/usr/bin/find / -name "*.log" -ctime +2 -exec /bin/rm -f {} \;

Cheers...
# 3  
Old 01-17-2011
Thanks citaylor......Smilie
# 4  
Old 01-18-2011
Hi,

I am using this script to delete the files created 5 days ago in /home/directory/ location.
But somehow its not working.Can you please let me know the error in this.

find /home/directory/ -type f -mtime +5 -exec rm {} \;


Thanks
# 5  
Old 01-18-2011
on a system with GNU touch and find I use
Code:
touch -d "-5 days" /tmp/5daysago
find /home/dir/ -type f ! -newer /tmp/5daysago -exec rm {} \;

# 6  
Old 01-18-2011
on a system with GNU touch and find I use
Code:
touch -d "-5 days" /tmp/5daysago
find /home/dir/ -type f ! -newer /tmp/5daysago -exec rm {} \;

# 7  
Old 01-18-2011
Quote:
I am using this script to delete the files created 5 days ago in /home/directory/ location.
But somehow its not working.Can you please let me know the error in this.

find /home/directory/ -type f -mtime +5 -exec rm {} \;
What happens? Please post any error messages.
Are you user "root"? If not, do you own all the files?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Shell script to delete specify directory

I have the following directories under "0.0.0.0" /scratch/builds/snapshotBuilds/0.0.0.0 drwxr-xr-x+ tested-571 drwxr-xr-x+ tested-576 drwxr-xr-x+ tested-597 drwxr-xr-x+ 600 drwxr-xr-x+ 601 drwxr-xr-x+ 602 drwxr-xr-x+ 603 drwxr-xr-x+ tested-604 drwxr-xr-x+ tested-605... (13 Replies)
Discussion started by: ibad_urs
13 Replies

3. Shell Programming and Scripting

Delete directory on date base

Hi, I have below directories Direct 2013-08-12 23123 Direct 2013-08-13 24121 Direct 2013-08-14 34513 Direct 2013-08-31 15435 ........... Direct 2013-09-12 53145 Direct 2013-09-30 65234 Direct 2013-09-30 89642 (11 Replies)
Discussion started by: learnbash
11 Replies

4. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

5. Shell Programming and Scripting

Script that displays contents of a directory

Hello all! I am writing a script that takes in a directory name as input and if the directory exists, it shows the files inside the directory here is what I have so far (incomplete) (mostly like pseudocode) #/bin/sh echo Please enter the name of a directory read dir grep $dir... (2 Replies)
Discussion started by: subway69
2 Replies

6. Shell Programming and Scripting

Needs to delete particular date files in a directory

Hi Guys, I need help on deleting particular date files in a directory. I have to delete thousands of files with respect to particular date. Could anyone help on this to delete particular date files at a time? Thanks in Advance (2 Replies)
Discussion started by: teddy2882
2 Replies

7. Shell Programming and Scripting

Linux delete script in sub-directory

Under TEMP folder, it have many sub-folder, example"0015,0016,etc" I need to discovery those file which are 2 days ago in this sub-folder , and list out to investigate, at the end delete all file in those sub folder, only keep the emptu directory. Thanks (4 Replies)
Discussion started by: android
4 Replies

8. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

9. Shell Programming and Scripting

Looking for a perl script (windows) to delete the contents of a file

Hi All, Im having a file named logserver.txt. I want a perl script to take a backup of that file, along with the datestamp; move the file to a different location or empty the contents of the file after backup. Remember, the file gets generated when the related service starts. My condition is... (14 Replies)
Discussion started by: ntgobinath
14 Replies
Login or Register to Ask a Question