Script to delete files older than x days and also taking an input for multiple paths


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to delete files older than x days and also taking an input for multiple paths
# 8  
Old 10-08-2012
read -r reads raw. If backslash escape sequences occur in your input, they will not be interpreted. E.g., if the input has \n, it will be treated as \n and not as a new-line. You'll get the input as is with no such interpretation by read.
This User Gave Thanks to elixir_sinari For This Post:
# 9  
Old 10-31-2012
Hi ,

For the same code, I have an additional requirement. How can we have 2 parameters passed from the input file dirlist.txt? The two parameters would be 1. Teh directory path 2. The retention days

In short , now I wanna write a script taht would delete files older than the retention period. This period should be different for different directory paths. Till now I have written this piece of code.

Code:
 
while IFS= read -r d; do
(
        cd "$d"
        files2del=`ls *.dat `
        if [ ${files2del:-NO} = "NO" ]
        then 
                echo " No files to delete" | mailx -s " ATTENTION: NO FIES TO DELETE " $EMAILLIST
        else
                find "$d" -type d ! -name . -prune -o -mtime +$NUM_DAYS -name '*.dat' -print
                echo " Files to delete : $files2del " | mailx -s " Files deleted are from path $d " $EMAILLIST
        fi
 
)
done < dirlist.txt

The dirlist.txt can have sample input as
Code:
 
/path1 7
/path2 4
/path3 9

wherein 7,4 and 9 are retention days for which the files have to be kept and the files older than these should be deleted.
# 10  
Old 10-31-2012
Change while IFS= read -r d; do in your script to while IFS= read -r d NUM_DAYS; do
# 11  
Old 01-10-2013
Hi,
One more requirement for the same code. Along with the two parameters in the input file, I want to add one more parameter i.e. file extension. This parameter will be different for different paths.
Th input file will now look like this:

Code:
 
/path1 7 *.dat
/path2 4 *.csv
/path3 9 *.txt

For this, my command goes like this:

Code:
 
while IFS=' ' read -r dir NUM_DAYS file_extn; do
(
files2del=$(find . -type d ! -name . -prune -o -mtime +$NUM_DAYS -name '$file_extn' )                                                                                                      
        echo "$files2del" >> $LOGFILE 2>&1
)
 done < dirlist.txt

The problem is , it is not listing any file in the output. Can anyone please let me know where am I going wrong?
# 12  
Old 01-10-2013
Try changing '$file_extn' in your find command to "$file_extn".

Note that if there are any spaces, tabs, or newlines in any of the matched filenames, this may produce ambiguous output in $LOGFILE.

With single quotes, $file_extn won't be expanded and find will literally look for $file_extn instead of the string stored in the variable file_extn.

Last edited by Don Cragun; 01-10-2013 at 06:45 AM.. Reason: Explain why you need double quotes in this case.
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 01-10-2013
Thats working. thanks!!! :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find files not matching multiple patterns and then delete anything older than 10 days

Hi, I have multiple files in my log folder. e.g: a_m1.log b_1.log c_1.log d_1.log b_2.log c_2.log d_2.log e_m1.log a_m2.log e_m2.log I need to keep latest 10 instances of each file. I can write multiple find commands but looking if it is possible in one line. m file are monthly... (4 Replies)
Discussion started by: wahi80
4 Replies

2. Shell Programming and Scripting

Script to delete files in a folder older than 2 days

hi i need a script to delete the files older than 2 days... if my input is say in a folder versions A_14122012.txt A_15122012.txt A_16122012.txt A_17122012.txt i want my output to be A_16122012.txt A_17122012.txt thanks in advance hemanth saikumar. (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

Delete files older than X days.

Hi All, I am using below code to delete files older than 2 days. In case if there are no files, I should log an error saying no files to delete. Please let me know, How I can achive this. find /path/*.xml -mtime +2 Thanks and Regards Nagaraja. (3 Replies)
Discussion started by: Nagaraja Akkiva
3 Replies

4. Solaris

Delete files older than 30 days

Hi all, I want to delete log files with extension .log which are older than 30 days. How to delete those files? Operating system -- Sun solaris 10 Your input is highly appreciated. Thanks in advance. Regards, Williams (2 Replies)
Discussion started by: William1482
2 Replies

5. Shell Programming and Scripting

Delete files older than 2 days using shell script in Unix

I'm new to shell script.... can any one help... What is the shell script to delete the files older than 2 days ? (3 Replies)
Discussion started by: satishpabba
3 Replies

6. Shell Programming and Scripting

delete files more than 15 days older

i have to delete files which are older than 15 days or more except the ones in the directory Current and also *.sh files i have found the command for files 15 days or more older find . -type f -mtime +15 -exec ls -ltr {} \; but how to implement the logic to avoid directory Current and also... (3 Replies)
Discussion started by: ali560045
3 Replies

7. UNIX for Dummies Questions & Answers

Delete files older than 30 days

This is driving me crazy. How can I delete files in a specifc directory that are over 30 days old? Thanks in advance. (3 Replies)
Discussion started by: tlphillips
3 Replies

8. UNIX for Dummies Questions & Answers

How can I delete files older than 7 days?

I will like to write a script that delete all files that are older than 7 days in a directory and it's subdirectories. Can any one help me out witht the magic command or script? Thanks in advance, Odogboly98:confused: (3 Replies)
Discussion started by: odogbolu98
3 Replies

9. UNIX for Dummies Questions & Answers

delete files older than 7 days

can anyone tell me how I would write a script in ksh on AIX that will delete files in a directory older than 7 days? (1 Reply)
Discussion started by: lesstjm
1 Replies
Login or Register to Ask a Question