Script for deleting Files

Hello,

I am new to shell scripting..i need your help.

i have the delete the log files which are present in the specific path .i have the rules as below.

1)In Path1 i need to delete all the files which are present
2)in Path2 i need to keep 1 month of data and delete the rest of the data
3)in Path3 i need to keep 2 weeks of data and delete all the files.
4)in Path4 i need to keep 1 year of data and delete all the files which are more than one year.
5)in path5 i need to keep last 2 backups which are kept .

i need a generic code .i have tried using the below code but failed to execute can any one of you please help me in completing my code.

#!/bin/bash
function processpathinput2 () {
        targetFolder=$1
        input2=$2

        if [[ "$input2" == "*m" ]]
        then
                echo "Remove files under $targetFolder which are $input2 old"
		elif		
        if [[ "$input2" == "*d" ]]
		then
                echo "Remove files under $targetFolder which are $input2 old"
		elif	
         if [[ "$input2" == "*y" ]]
		then
                echo "Remove files under $targetFolder which are $input2 old"	
        else
               	echo "Invalid input"	
         fi
		
		}
path="/home/path/tmp/dele:1d"

My idea is based on the letter m,y,d it should calculate and delete the files .

Is this a homework assignment? Homework must be posted in the Homework and Coursework Questions forum and must include a completely filled out homework template.

Note that defining a shell function doesn't execute that function. Something in your shell script needs to actually invoke your function (passing it at least two operands).

1 Like

Its not an homework i am trying to delete my log files which are generated by server ..

You should use the find command. You just need to adapt as needed and log the deletion process.

find /logdir/* -name *.log -mtime +14 -type f | xargs rm -rvf 
1 Like

Thank You for the reply,Can you help me with the commands for the last 2 backups which are kept .

The +14 is the number of days to keep, /logdir/* is the path to the files that need to be purged, -name *.log is to make sure that you only delete log files, -type f specifies that this only applies to files and not directories, finally xargs rm -rvf does the remove operation.