Delete a directory after X number of days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete a directory after X number of days
# 1  
Old 07-28-2010
Delete a directory after X number of days

Hi
How do I remove a directory which are some days old .

For Ex :
Code:
$ cd /WMID/data/temp/TxTest/Dev/txStore
$ ls -ltr
total 0
drwxr-xr-x    3 wmethods wmapp            80 Jul 21 03:36 0AE445C4
drwxr-xr-x    3 wmethods wmapp            80 Jul 21 04:00 0AE44664
drwxr-xr-x    3 wmethods wmapp            80 Jul 22 04:50 0AE4478B
drwxr-xr-x    3 wmethods wmapp            80 Jul 22 04:51 0AE047C2
drwxr-xr-x    3 wmethods wmapp            80 Jul 22 23:47 0AE44811
drwxr-xr-x    3 wmethods wmapp            80 Jul 27 02:15 0AE44DDA
$ cd 0AE445C4
$ ls -ltr
total 16
drwxr-xr-x    2 wmethods wmapp          1024 Jul 21 03:37 A192CFCC
$ cd ../0AE44664
$ ls -ltr
total 16
drwxr-xr-x    2 wmethods wmapp          1024 Jul 21 04:00 A191C022

I tried the below command and getting an error
Code:
$  find /WMID/data/temp/TxTest/Dev/txStore -type d -mtime +1 -exec rm -rf {} \;
find: 0652-081 cannot change directory to </WMID/data/temp/TxTest/Dev/txStore/0AE047C2>:
  : A file or directory in the path name does not exist.
find: 0652-081 cannot change directory to </WMID/data/temp/TxTest/Dev/txStore/0AE445C4>:
  : A file or directory in the path name does not exist.
find: 0652-081 cannot change directory to </WMID/data/temp/TxTest/Dev/txStore/0AE44664>:
  : A file or directory in the path name does not exist.
find: 0652-081 cannot change directory to </WMID/data/temp/TxTest/Dev/txStore/0AE4478B>:
  : A file or directory in the path name does not exist.
find: 0652-081 cannot change directory to </WMID/data/temp/TxTest/Dev/txStore/0AE44811>:
  : A file or directory in the path name does not exist.
find: 0652-081 cannot change directory to </WMID/data/temp/TxTest/Dev/txStore/0AE44DDA>:
  : A file or directory in the path name does not exist.

Any help would be great

Thanks
Leo

Last edited by leo76; 07-28-2010 at 11:55 AM.. Reason: Please use code tags
# 2  
Old 07-28-2010
Might be a bit late for that. This is not a syntax error.

Quote:
find /WMID/data/temp/TxTest/Dev/txStore -type d -mtime +1
The above statement finds the parent directory first:
/WMID/data/temp/TxTest/Dev/txStore
Then the command part deleted the entire tree recursively.
The remaining deletions all fail because the directories have already been deleted.
# 3  
Old 09-08-2010
This should work exactly as you require

Code:
find /WMID/data/temp/TxTest/Dev/txStore -type d -mtime +1 | xargs rm -rf

# 4  
Old 09-08-2010
Moving - nothing AIX related in it.
# 5  
Old 09-08-2010
@jkhan69
Quote:
find /WMID/data/temp/TxTest/Dev/txStore -type d -mtime +1 | xargs rm -rf
This has exactly the same problem where the first directory found is the parent directory "/WMID/data/temp/TxTest/Dev/txStore" which we can tell from what actually happened had a timestamp older than one day.

The find command would have been better as:
Code:
find /WMID/data/temp/TxTest/Dev/txStore/* -prune -type d -mtime +1

The "*" ensures that we only look at the subdirectories. The "-prune" stops us descending the tree. Thus we only make the decision whether to delete the directory based on the criteria in the original post and only issue one "rm" per deletable tree.

The moral of this story is to test the "find" statement thoroughly before spawning powerful commands like "rm -rf".
# 6  
Old 09-08-2010
Hehe you original statement is fine except your missing a very simple switch...
Code:
find /WMID/data/temp/TxTest/Dev/txStore -maxdepth 1 -type d -mtime +1 -exec rm -rf {} \;

# 7  
Old 09-09-2010
Quote:
Originally Posted by methyl
The moral of this story is to test the "find" statement thoroughly before spawning powerful commands like "rm -rf".
Nice one mate.. Like it Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete files 30 days old

Will this work to delete files 30 days old in $backupDir or is there a better way to do it? find $backupDir -type f -mtime +30 -exec rm {} \; (2 Replies)
Discussion started by: wyclef
2 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Delete directory after 3 days

Dear all, I have a directory /homes/zak in which I have a number of directories which are created on a daily basis thus: 02-MAY-10 03-MAY-10 04-MAY-10 05-MAY-10 I want to script the clean up of these directories, so I only keep two days worth, so for example anything over 2 days... (10 Replies)
Discussion started by: Zak
10 Replies

5. Shell Programming and Scripting

Delete a file after 3 days

Hi.. Am using diff to compare 2 directories(A & B) and as ouput i get a list of files which are found only in directory B ( I used grep & sed to create the list). Now I want to delete the files which are found only in dir B after 3 days. Please help me with this. Thanks CoolAtt (7 Replies)
Discussion started by: coolatt
7 Replies

6. Shell Programming and Scripting

delete three days from date

i want to delete three days from system date ... date -3 (4 Replies)
Discussion started by: r_t_1601
4 Replies

7. UNIX for Dummies Questions & Answers

Delete last 10 days logs

Hi Can u please tell me how to delete last 10 days logs .. (9 Replies)
Discussion started by: pb18798
9 Replies

8. Shell Programming and Scripting

ls latest 4 days or specify days of files in the directory

Hi, I would like to list latest 2 days, 3 days or 4 days,etc of files in the directory... how? is it using ls? (3 Replies)
Discussion started by: happyv
3 Replies

9. UNIX for Dummies Questions & Answers

How to delete files over 30 days

I have a directory that contains files. I would like the command that deletes all files that are over 30 days old. Delete files based on creation date and not modified. (2 Replies)
Discussion started by: GEBRAUN
2 Replies

10. UNIX for Dummies Questions & Answers

How to delete files which are 7 days old

Hi all, how to write a script that will indentify the files in a directory which are 7 days old and delete those files. Thanks in advance Cheers Arunava (8 Replies)
Discussion started by: arunava_maity
8 Replies
Login or Register to Ask a Question