Delete the files older than 3 weeks in a particular directory.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete the files older than 3 weeks in a particular directory.
# 1  
Old 11-14-2009
Power Delete the files older than 3 weeks in a particular directory.

Hi, Friends,
I am writing a script to delete all the files which are there for more than 3 weeks.

I have tried this :
find /home/appl/backup -type f -mtime +21 -exec rm -f {} \;

But i am not sure if it deletes only the files in specified directory or all the directorinies in the provieded to it.

I tried subtracting the present week and week in which files were last modified.And thus delete all of the remaining.When i do ls -lt to get modification date, i cudnot get the week number from the old files' modification date.


Please suggest a way out ASAP.And any help in this regard will be appreciated in advance...
# 2  
Old 11-14-2009
Try testing your code. Setup some test directories and files. you can use the touch command to create files and change the date on the file. Create some files and directories that would fall within your delete window and some that don't. you can even create a script to generate these test files to make it easily repeatable.

Code:
mkdir tst
ls -lrt
drwxr-xr-x 2 user usrgrp 4096 Nov 14 18:02 tst
touch -t 200910121314 tst
ls -lrt
drwxr-xr-x 2 user usrgrp 4096 Oct 12 13:14 tst
touch -t 200910121314 tfile.tst
ls -lrt

drwxr-xr-x 2 user usrgrp 4096 Oct 12 13:14 tst
-rw-r--r-- 1 user usrgrp    0 Oct 12 13:14 tfile.tst

Whenever I am writing a script that removes files, I always have the script list the files the script has targeted for removal and verify those are the files I expect before I implement the rm command.

Good luck.
# 3  
Old 11-15-2009
You will almost certainly want to look at the prune option for find as well
# 4  
Old 11-15-2009
1. You can first execute the find command without -exec part, to view the list as,
Code:
find /home/appl/backup -type f -mtime +21

2. If you are want to double ensure you are removing only the intended file then use -ok instead of -exec which will ask confirmation from you before doing the specified operation on each file.

Code:
 find /home/appl/backup -type f -mtime +21 -ok rm -f {} \;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List 2 weeks older file on specific directory

Ive been a vocal of FIND command even before. Command below doesnt really give me the file that is older than two weeks.. Is there a script that will list me the log files that i want like for this date December 10, 2014, it shud list me the date between November 26, 2014 and below. When i run... (6 Replies)
Discussion started by: kenshinhimura
6 Replies

2. Shell Programming and Scripting

Move log files with date and delete older than 3 weeks

I have written a script which generate one logfile on every sunday and thursday I want to move the older log files into /tmp directory befor generating new one so i used mv command like mv usr/sbin/appl/logfile.txt usr/sbin/appl/tmp 2) But when i move this file to /tmp it will... (1 Reply)
Discussion started by: Nakul_sh
1 Replies

3. AIX

Want to delete directory, subdirectories and all files which are older than 7 days

how do i remove sub directories of a directory and all files which are older than 7 days by a single command in AIX. pls help me. I am using command as #find /gpfs1/home/vinod/hpc/ -depth -type d -mtime +7 -exec rm -rf {} \; so i want to delete all sub directories and all files from the... (1 Reply)
Discussion started by: vinodkmpal
1 Replies

4. Shell Programming and Scripting

[SOLVED] Pull lines older than N weeks

There is another post in the forums that is similar to what I am trying to do, however, the thread is closed. So, I am creating this new one to see if someone could help. I am trying to use the code Ahamed posted, and tweak it. With the info from the forum, I recreated the scenario the person... (8 Replies)
Discussion started by: karstenjhilton
8 Replies

5. Shell Programming and Scripting

Delete multiple folders in a directory which are two weeks old

I need help. I have to delete multiple directories inside a directory that are two weeks old. Example: Today is July 09, 2012 Folder1 > folder1 (created June 4, 2012) -- should be deleted > folder2 (created June 2, 2012) -- should be deleted > folder3 (created... (4 Replies)
Discussion started by: jasperux
4 Replies

6. Shell Programming and Scripting

Delete files older than 10 Days in a directory

Hi All I want to remove the files with name like data*.csv from the directory older than 10 days. If there is no files exists to remove older than 10 days, It should not do anything. Thanks Jo (9 Replies)
Discussion started by: rajeshjohney
9 Replies

7. Shell Programming and Scripting

Delete files older than "x" if directory size is greater than "y"

I wrote a script to delete files which are older than "x" days, if the size of the directory is greater than "y" #!/bin/bash du -hs $1 while read SIZE ENTRY do if ; then find $1 -mtime +$2 -exec rm -f {} \; echo "Files older than $2 days deleted" else echo "free Space available"... (4 Replies)
Discussion started by: JamesCarter
4 Replies

8. Emergency UNIX and Linux Support

How to delete all the files which are more than 3 weeks old in a particular directory.Thnx in advanc

(12 Replies)
Discussion started by: rajsharma
12 Replies

9. Shell Programming and Scripting

delete files older than 5 minutes in directory (recursively)

sorry guys can some please give me a hint how to achieve this in a slick oneliner? delete files older than 5 minutes in specified directory (recursively) peace (3 Replies)
Discussion started by: scarfake
3 Replies

10. Shell Programming and Scripting

how to check whether the given file is 5 weeks older than current date

HI, I need to check whether the given file is 5 weeks older than current date ?? Can anyone give me the script for this ?? (1 Reply)
Discussion started by: risshanth
1 Replies
Login or Register to Ask a Question