The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete files older than 30 days William1482 SUN Solaris 2 05-08-2009 02:35 PM
how to delete the files which are the 30 min older...? psiva_arul Shell Programming and Scripting 2 06-13-2008 04:36 PM
delete files older than 5 minutes in directory (recursively) scarfake Shell Programming and Scripting 3 06-13-2008 01:10 AM
Reg: delete older files from ftp sam99 UNIX for Dummies Questions & Answers 3 01-09-2008 10:56 AM
how to check whether the given file is 5 weeks older than current date risshanth Shell Programming and Scripting 1 10-29-2007 04:53 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 6 Days Ago
rajsharma rajsharma is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 5
Red face 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 (permalink)  
Old 6 Days Ago
jsmithstl jsmithstl is offline
Registered User
  
 

Join Date: Oct 2009
Location: St. Louis, MO
Posts: 34
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 (permalink)  
Old 5 Days Ago
steadyonabix steadyonabix is offline
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 154
You will almost certainly want to look at the prune option for find as well
  #4 (permalink)  
Old 5 Days Ago
thegeek thegeek is online now
Registered User
  
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 434
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 {} \;
Sponsored Links
Reply

Bookmarks

Tags
delete files older than 3 weeks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0