find and delete script question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and delete script question
# 1  
Old 03-02-2009
find and delete script question

Okay, here is what I have:

Code:
#!/bin/sh
cd /opt/backup/app || exit
find . -name "*.tar" -mtime +7 -exec gzip {} \;
find . -name "*.war" -mtime +7 -exec gzip {} \;
find . -name "*.tar.back*" -mtime +7 -exec gzip {} \;
#find . -name "*.gz" -type f -mtime +91 -exec rm {} \;

Here is a little bit on what I want to do. The /opt/backup/app directory has many sub-directories with many more directories under them. Now, everything in these directories are backups of old files, in this case all the files I am looking to execute on are *.tar, *.war. and *.tar.back*

What I am doing now, is just gzipping anything older than 7 days, and deleting anything older than 91 days. Now, I ran into a situation where it would have been helpful to have one of these files after 91 days. So I was wondering how I would change this script to the following:

Run the find command(or some equivelant if it's easier that way), then a logic statement to say, if this .gz file is the only one in this directory, then do nothing, else delete all .gz files older than 91 days, but keep the most recently updated file.

lol, well I was going to write it in some kind of code-block, but then I didn't.
# 2  
Old 03-02-2009

Code:
find . -name "*.gz" -type f -mtime +91 |
 while IFS= read -r file
 do
   dir=${file%/*}
   (
      cd "$dir" || continue
      set -- *.gz
      [ $# -lt 2 ] && continue
      find *.gz -type f -mtime +91 -exec rm {} \;
   )
 done

# 3  
Old 03-04-2009
Quote:
Originally Posted by cfajohnson

Code:
find . -name "*.gz" -type f -mtime +91 |
 while IFS= read -r file
 do
   dir=${file%/*}
   (
      cd "$dir" || continue
      set -- *.gz
      [ $# -lt 2 ] && continue
      find *.gz -type f -mtime +91 -exec rm {} \;
   )
 done

Okay, so worked exactly as I had explained, unfortunately I didn't explain it correctly.

Rather than if this it he only .gz in a directory, do not delete, I meant to say, to always leave 1 .gz file in the directory. Regardless of how old it is. See, these are backup files that we may need, so we want to keep at least one file for quickly reverting changes if needed. So, let's say in one directory I have 8 files, and they are older than the 91 days, I want the script to delete the 7 oldest .gz files, and keep the one that's been modified the most recently.
# 4  
Old 03-04-2009

Code:
find . -name "*.gz" -type f -mtime +91 |
 while IFS= read -r file
 do
   dir=${file%/*}
   (
      cd "$dir" || continue
      set -- *.gz
      [ $# -lt 2 ] && continue
      rm $( ls -t $(find *.gz -type f -mtime +91) | tail -n +2 )
   )
 done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Bash script - find command with delete and exec

hi all, i have devised a script that starts in /restored/ and in there, there are a lot of sub folders called peoples names and in the sub folders are files/folders and it deletes the data in the sub folders BUT not the sub folder itself and it should then touch a file in all the sub folders... (3 Replies)
Discussion started by: robertkwild
3 Replies

2. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

3. Shell Programming and Scripting

Perl Script to find the disk usage and to delete the files which is consuming more space

Hi All, I have written a script to check the file system usage and to delete the files which is consuming more space.Please check whether the script is corrcet #Script Starts here #!/usr/local/bin/perl #Program to find the disk space and to delete the older files #Checks the type of OS... (8 Replies)
Discussion started by: arunkarthick
8 Replies

4. UNIX for Dummies Questions & Answers

Script to find the files and delete them

This is a real world problem so I think you might found this interesting. We have servers which are shared by multiple team members. Each team member has its own user id and home directory. Now with time each user starts creating files which in end caused the disk to be full. Now for creating a... (5 Replies)
Discussion started by: Rohit06
5 Replies

5. UNIX for Dummies Questions & Answers

Script Question (find, rsync)

Hello all, I'm using rsync to maintain two copies of a music library on my main workstation and a server running MPD. I'm wondering about using the script below as a basis for cron to update my MPD db if new files have been added or existing files modified: find -type f -iname 'FILENAME' -mmin... (9 Replies)
Discussion started by: pmicka
9 Replies

6. Shell Programming and Scripting

Script to find files and delete it by comparing

I have a directory where lot of "gzip" files are dropped in every 5 minutes. There is an application which will process these gzip and move it to another directory but will leave a gzip.out file with lot of output data. I need to remove all the outfiles except for the one which is being... (1 Reply)
Discussion started by: gubbu
1 Replies

7. Shell Programming and Scripting

Write a shell script to find ctrl M and delete them

Hi All, I would like to write a shell script to find Control M charactes and after getting the result i want to delete it. But in the script the following command couldn't work. find . -exec grep -l '^M' {} \; > test.txt ^M is ctrl V and M thanks in advance for any help. (4 Replies)
Discussion started by: jatanig
4 Replies

8. Shell Programming and Scripting

'unexpected token' error using FIND in script to delete old files

Hi, I am compete linux noob, but have managed to setup my Thecus N5500 to rsync to my N5200Pro to do automatic backups each night. The rsync script also moves any files deleted from the N5500 (and previously backed up to the N5200) to a _deleted folder on the N5200 so I can keep them for 30... (10 Replies)
Discussion started by: rpalmer68
10 Replies

9. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

10. UNIX for Dummies Questions & Answers

Script to change/find/delete/install a specific file

Hi Very much a newbie to UNIX & scripting, but have identified an area within work that would benefit from being automated, as its repeated manually very often, and it looks like the ideal first script! What I need to do is change directory to a users home (cd ~), and then find and remove a... (6 Replies)
Discussion started by: Great Uncle Kip
6 Replies
Login or Register to Ask a Question