Script for deleting Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for deleting Files
# 1  
Old 10-14-2015
Linux 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.



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 .

Last edited by Don Cragun; 10-14-2015 at 04:27 PM.. Reason: Add CODE tags.
# 2  
Old 10-14-2015
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).
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-14-2015
Its not an homework i am trying to delete my log files which are generated by server ..
# 4  
Old 10-14-2015
You should use the find command. You just need to adapt as needed and log the deletion process.

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

This User Gave Thanks to gandolf989 For This Post:
# 5  
Old 10-14-2015
Thank You for the reply,Can you help me with the commands for the last 2 backups which are kept .
# 6  
Old 10-14-2015
Quote:
Originally Posted by rajesh25
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script for Deleting files older than 14 days automatically

we need to have periodic clean up implemented in our sap directory \\sapds\PR1\int\scm\snp\outbound\snapshot. which needs unix script for Deleting files older than 14 days automatically in sap system unix os. (1 Reply)
Discussion started by: kvkreddy_b2w
1 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. Shell Programming and Scripting

Script for deleting files and directories when the file system reaches the threshold

Hi Can someone assist in writing a script. I have a filesystem named /sybase in my aix lpar. When this filesystem becomes 94% full all the files and directories under /sybase/logs should be deleted immediately. :confused: (7 Replies)
Discussion started by: newtoaixos
7 Replies

4. Shell Programming and Scripting

Need help with shell script for moving/deleting/renaming files

Hi. I need help with a little script that will be used to move some files to their parent directory, delete the directory, rename one file in the parent directory and delete another, then continue to the next. Here's an example: /var/media/Music/Genesis/1970 album - Trespass (2008 Box -... (4 Replies)
Discussion started by: aflower
4 Replies

5. Shell Programming and Scripting

Script for searching a pattern in 5 files and deleting the patterns given

Hi All, I have written the below script that searches for the pattern in a file and delete them if present. please can some one have a look and suggest the changes in the script. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read... (4 Replies)
Discussion started by: Shazin
4 Replies

6. Shell Programming and Scripting

deleting files inside shell script - ( using find)

Hi, I am using the command find /apps/qualdb/gpcn/scripts/cab_outbound/archive -name 'z*' -mtime +28 -exec rm {} \; in unix command prompt for deleting the files in my unix system under the specfied folder. It was succesfull. But when i running this command inside a shell script name... (2 Replies)
Discussion started by: Jayaram.Nambura
2 Replies

7. Shell Programming and Scripting

Script for Deleting files

Hi All, I need a script which compares the sysdate and the file date(Last updated time stamp) and if the time difference is greater than 10 mins i need to delete that file. Can anyone help me pls?????? Ashok. (5 Replies)
Discussion started by: Ashok_oct22
5 Replies

8. Shell Programming and Scripting

shell script reqd - deleting files

I have written a script that deletes files: Requirement: i need to delete the files and to know how many files are deleted i.e the count of files and even i need to display when the started time of deletion and the ending time of deletion. I need to display these two times. script: ... (2 Replies)
Discussion started by: venkatesht
2 Replies

9. Shell Programming and Scripting

Script for Deleting Core files on root filesystem

ok i am setting up a script to run daily using crontab. This script will search the root filesystem and delete any and all core files. I have set up this script The only problem i get with this script is it searches for directories and attempts to delete them. Since i have probably... (7 Replies)
Discussion started by: rgfirefly24
7 Replies

10. Shell Programming and Scripting

shell script: deleting files from a directory

i have the following problem: use shell script: Two directories have to be searched for files havin the same name. then delete these files from one of these directories. the directory names have to be accepted as command line arguments. what i have done till now is: 1. run a loop... (1 Reply)
Discussion started by: onlyc
1 Replies
Login or Register to Ask a Question