Script That Can navigate to 3 differents directory & remove files under them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script That Can navigate to 3 differents directory & remove files under them
# 1  
Old 01-21-2010
Script That Can navigate to 3 differents directory & remove files under them

Hi

I am Trying to Write a script that can goto 4 different directorys on the server & remove the Files older then 30 days ??

/logs

logs1 logs2 logs3

Now I need to remove files under

logs1 logs2 logs3 which are older then 30 days whose name stat 'sit' , 'mig','bld' .

in addition

I have another directory which residing some where else i also need to remove file in that directory which is under root as /log4 are older then 30 days


I have this as of now ..

vi Archive_logscript

#!/bin/bash

cd /log/log1

find . -type f -name 'sit_*' -mtime +30 -print | xargs /bin/rm -rf

cd /log/log2

find . -type f -name 'mig_*' -mtime +30 -print | xargs /bin/rm -rf

cd /log/log3

find . -type f -name 'bld_*' -mtime +30 -print | xargs /bin/rm -rf


cd /oldfiles/log4

find . -type f -name 'pro_*' -mtime +30 -print | xargs /bin/rm -rf

exit


-----


do you think this works ??? any better suggestion ???

Last edited by Beginner123; 01-21-2010 at 05:42 PM..
# 2  
Old 01-21-2010
You do not need to cd into each directory in turn, you can specify the full path, this is also safer in that if the cd failed the find would have still run, e.g.:

Code:
find /logs/logs1

You can replace:
Quote:
-print | xargs /bin/rm -rf
with
Code:
-exec rm {} \;

The -r would do a recursive delete which should not need to apply to a file, the -f may still be necessary depending on the permissions on the files you are deleting.

If you must cd into each directory first then do:

Code:
cd /directory && find . -type f -name...

So that if the cd fails then the find does not run.
# 3  
Old 01-21-2010
Thanks Tony ..
I will implement and then see what happens ..
# 4  
Old 01-21-2010
you can first find older directories than 30 days, then move them under a spesific directory you create, lets say /old_dir_tree then remove the ones you want: modifying this code:

Code:
mkdir /old_dir_tree
find . -type d -mtime +30 | grep -i 'log1 log2 log3' >> old_dir.txt
while read line;
        do
        mv $line /old_dir_tree
done<old_dir.txt

if you like you can also grep some spesific directories in all found old directories, then remove only them.

Or you can directly remove old directories with this command but i dont recommend this way:

Code:
find . -type d -mtime +30 -exec rm -r {} \;

try to modify the first code.

regards

Last edited by EAGL€; 01-21-2010 at 06:49 PM.. Reason: i slightly modifed the finding spesific directory part
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. How to Post in the The UNIX and Linux Forums

How to Navigate in UNIX & Linux Forums..?

Hi , i am a new user to this forum can anyone please help me in navigation for this forum. also when i am trying to open any thread i am getting below error. Bad Request Your browser sent a request that this server could not understand.] Thanks. (1 Reply)
Discussion started by: nkchand
1 Replies

2. UNIX for Dummies Questions & Answers

Script to remove zip files from a directory

Hi Folks, There is a job which generates a .zip files every day at /usr/app/generated directory , now please advise for the script that will delete this zip files permanently.but while deleting it should make sure that it will not delete the last two days recently generated zip files and this... (1 Reply)
Discussion started by: punpun66
1 Replies

3. UNIX for Dummies Questions & Answers

Need to navigate to HOME directory when I log in

Hi, Currently i'm logging as a user say atgdev. When I login it takes me to directory /. I see the home directory set as /home/atgdev/ I want that when i log in it shud directly go to my home directory i.e /home/atgdev/. I thought .profile in /home/atgdev/ will be invoked first when i log... (9 Replies)
Discussion started by: Gangadhar Reddy
9 Replies

4. Shell Programming and Scripting

Script to search for a character in files in a Directory & remove it

Hi All, Am new to both Unix & this Forum - Need some help on a script that I am trying to write: In a Directory i have few text files which might or might not contain some text that I am trying to find. Once that text is found in any of the files, it needs to be removed from the file ... (6 Replies)
Discussion started by: rituparna_gupta
6 Replies

5. Programming

Script for creating a directory & move the .tif files in it.

Hi Team, I have thousands of TIF files which are converted from PDF. Below is a sample of it. LH9406_BLANCARAMOS_2012041812103210320001.tif LH9406_BLANCARAMOS_2012041812103210320002.tif LH9406_BLANCARAMOS_2012041812103210320003.tif LH9411_ANGENIAHUTCHINSON_2012041812102510250001.tif... (9 Replies)
Discussion started by: paragnehete
9 Replies

6. Shell Programming and Scripting

Need script to remove millions of tmp files in /html/cache/ directory

Hello, I just saw that on my vps (centOS) my oscommerce with a seo script has created millions of tmp files inside the /html/cache/ directory. I would need to remove all those files (millions), I tried via shell but the vps loads goes to very high and it hangs, is there some way to do a... (7 Replies)
Discussion started by: andymc1
7 Replies

7. AIX

Do you need execute permission to navigate to a directory?

i have a user 'bart' which does not belong to apps group (as shown below) and i want him to be able to navigate to TEST directory.. i gave him read access but he cannot get through. when i added execute permission he was able to navigate to TEST drwxr-xr-- 3 draco apps 4096 Apr... (2 Replies)
Discussion started by: chipahoys
2 Replies

8. Shell Programming and Scripting

Script to remove all empty files within the directory structure?

Hi I need to write a shell script which basically searches for all the empty files within the directory structure, lists them before asking the user to confirm if they would like to delete them. If the user deletes the file then a notice would appear confirming the file is deleted. I've be... (5 Replies)
Discussion started by: cat123
5 Replies

9. Shell Programming and Scripting

How Do I Navigate To A Directory???

I am trying to install some applications, on my linux OS and all the instructions are tell me that ill have to navigate to the directories and type the link that they provide, but i try typing 'dir' which shows me the directories but to access a particular one i can't remember how??? can anyone... (2 Replies)
Discussion started by: kprescod4158
2 Replies

10. UNIX for Dummies Questions & Answers

Can you navigate ABOVE the home directory?

Hello, How do I navigate to the level ABOVE the home directory? I have the following structure on my drive, bearing in my I'm using a cygwin port on Windows. C:\ C:\cygwin C:\cygwin\bin C:\cygwin\otherfolders C:\cygwin\home (the home directory) C:\cygwin\home\H (my user directory) ... (4 Replies)
Discussion started by: patwa
4 Replies
Login or Register to Ask a Question