How to clean 3 days before files, from a directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to clean 3 days before files, from a directory?
# 1  
Old 01-20-2014
Question How to clean 3 days before files, from a directory?

I have a Solaris System. I am using bash shell.
I want to prepare a script which can do the below.
There are few directories i need to clean.
In those directories, I need to delete files which are older than 3 days. 3 days before files need to be deleted.
The directories are as follows.
CC1,CC2,Int.
The file format inside are(there are many files in the CC1 and CC2 directories)
tmp/test/CC1(dir)-A20140107.2355-0000_jak_file(there are files for every 5 min interval in this format)
A20140107.2355-0000_jak_test(there are files for every 5 min interval in this format)

/tmp/test/CC2(dir)-A20140107.2355-0000_jak_file(there are files for every 5 min interval in this format)
A20140107.2355-0000_jak_test(there are files for every 5 min interval in this format)

/tmp/file/Int(dir)-Int-CC1-20140107.csv
Int-CC2-20140107.csv

I have prepared something like this.. Tell me to what extent this will work??

Code:
#! /usr/bin/bash
find /tmp/test/CC1* -mtime +3 -exec rm {} \;
find /tmp/test/CC2* -mtime +3 -exec rm {} \;
find /tmp/file/Int* -mtime +3 -exec rm {} \;

But somehow i am not quite satisfied with this..
Need your expert opinion..

Last edited by Saidul; 01-20-2014 at 05:48 AM.. Reason: Clarity; find and remove
# 2  
Old 01-20-2014
It works!
You'll get errors if there are sub-directories. These can be suppressed by -type f or ! -type d.
Further, Unix find offers + instead of \; for -exec; if the command supports multiple arguments (rm does) then + is faster.
Then, rm -f suppresses eventual questions when deleting write-protected files.
Code:
find /tmp/test/CC1* -type f -mtime +3 -exec rm -f {} +
find /tmp/test/CC2* -type f -mtime +3 -exec rm -f {} +
find /tmp/file/Int* -type f -mtime +3 -exec rm -f {} +

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 01-29-2014
Hi MadeInGermany,

Thanks for your reply and it did help. I need to do a little modification if you can help me here too.

Actually whatever files i will be deleting from the above mentioned directories I need to do a provision of logging.
Code:
find /tmp/test/CC1* -type f -mtime +3 -exec rm -f {} + find /tmp/test/CC2* -type f -mtime +3 -exec rm -f {} + find /tmp/file/Int* -type f -mtime +3 -exec rm -f {} +

How do i achieve the below.?
1. Which dates files have been removed and when?
Simply, provision of logging.

Last edited by Franklin52; 01-29-2014 at 04:48 AM.. Reason: Please use code tags
# 4  
Old 01-29-2014
Quote:
Originally Posted by Saidul
Hi MadeInGermany,

Thanks for your reply and it did help. I need to do a little modification if you can help me here too.

Actually whatever files i will be deleting from the above mentioned directories I need to do a provision of logging.
Code:
find /tmp/test/CC1* -type f -mtime +3 -exec rm -f {} + find /tmp/test/CC2* -type f -mtime +3 -exec rm -f {} + find /tmp/file/Int* -type f -mtime +3 -exec rm -f {} +

How do i achieve the below.?
1. Which dates files have been removed and when?
Simply, provision of logging.
First, note that putting these three commands on one line with no command separator will NOT do what you want.
Second, you don't need to invoke find three times to do this (unless you run into ARG_MAX limitations).
If the rm utility on your system supports the -v option, try:
Code:
(date +"Following files removed at %c:";find /tmp/test/CC[12]* /tmp/file/Int* -type f -mtime +3 -exec rm -fv {} + ) > rm.log

otherwise, try something like:
Code:
date + "Following files removed at %c:" > rm.log
find /tmp/test/CC[12]* /tmp/file/Int* -type f -mtime +3 -exec rm -f {} + -exec printf "%s\n" {} + >> rm.log


Last edited by Don Cragun; 01-29-2014 at 05:17 AM.. Reason: Remove extraneous space in middle of log file name.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-29-2014
Or simply -print
Code:
{
date +"Following files removed at %c:"
find /tmp/test/CC[12]* /tmp/file/Int* -type f -mtime +3 -exec rm -f {} + -print
} > rm.log

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 6  
Old 01-29-2014
Quote:
Originally Posted by MadeInGermany
Or simply -print
Code:
{
date +"Following files removed at %c:"
find /tmp/test/CC[12]* /tmp/file/Int* -type f -mtime +3 -exec rm -f {} + -print
} > rm.log

Yes, obviously. It must be past my bed time...
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-29-2014
Thnaks a lot Don Cragun and MadeInGermany.
Will utilize this and update.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Finding files older than x days within directory with spaces

Hi, I am trying to run a command that finds all files over x amount of days, issue is one of the directories has spaces within it. find /files/target directory/*/* -type f -mtime +60 When running the above the usual error message is thrown back + find '/files/target\' 'directory/*/*' -type... (1 Reply)
Discussion started by: Ads89
1 Replies

2. Shell Programming and Scripting

How to move the files older than x days with similar directory structure?

Hello, I need to move all the files inside /XYZ (has multi-depth sub directories) that are older than 14 days to/ABC directory but with retaining the SAME directory structure. for example: /XYZ/1/2/3/A/b.txt should be moved as /ABC/1/2/3/A/b.txt I know about find /XYZ -type f -mtime +14... (3 Replies)
Discussion started by: prvnrk
3 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

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

5. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

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

7. Shell Programming and Scripting

Searching for files over 30 days old in current directory

May be a simple question for experts here.... I need to get the list of files older than 30 days in the current folder. I tried "find", but it searches recursively in all the sub directories. Can I restrict the recursive search and extract the files only from current directory ? (18 Replies)
Discussion started by: cxredd4
18 Replies

8. Shell Programming and Scripting

How to zip a modified file 15 days before but not scanning my sub directory files

I am using zip -m option to zip my files, but i dont want my sub directories files to be zipped (1 Reply)
Discussion started by: skrish70
1 Replies

9. Shell Programming and Scripting

Removing files automatically from a directory after 30 days.

Hello; I have a directory that is collecting log and act files. I need to write a script that will remove these files once they are 30 days old. I have read through a number of threads on this site that have given me a great deal of information. However I have what seems to be a unique... (7 Replies)
Discussion started by: justinb_155
7 Replies

10. AIX

Directory clean up

I want to write a script in ksh that will delete file that are more than 2 weeks old. (4 Replies)
Discussion started by: jango
4 Replies
Login or Register to Ask a Question