Operating System - HP-UX
1837902 Members
5337 Online
110124 Solutions
New Discussion

how to delete old files including those in subdirectory

 
Kelvin Ng
Advisor

how to delete old files including those in subdirectory

how to delete old files including those in subdirectory

Hi, i have a HP-UNIX 11.0 backup server that usr1/backuplog directory increasing in big size
1) how to check what program/process is writing logfile into backuplog dir daily?
2) how to delete old files in backuplog dir including those in subdirectory & keep only recent 7 days?

sorry, i'm new to HP-UNIX
thanks in advance
19 REPLIES 19
Hakki Aydin Ucar
Honored Contributor

Re: how to delete old files including those in subdirectory

>how to check what program/process is writing logfile

lsof /your directory


>how to delete old files in backuplog dir

cd your directory
rm
if includes subdirectory
rm -R

Steven Schweda
Honored Contributor

Re: how to delete old files including those in subdirectory

> 1) [...]

Look at what runs daily? "man cron"?

> 2) [...]

"man find", look for "-mtime".

> sorry, i'm new to HP-UNIX

Are you new to UNIX in general, or only to
HP-UX?

A Forum search for keywords like, say,
find mtime
should find many examples.
Steven Schweda
Honored Contributor

Re: how to delete old files including those in subdirectory

> >how to delete old files in backuplog dir

> rm

There's a difference between "delete old
files" and "delete log files".
Hakki Aydin Ucar
Honored Contributor

Re: how to delete old files including those in subdirectory

I missed that part:only recent 7 days?:
a simple example:

find /your directory -name -mtime +7 -exec rm {} \;
Kelvin Ng
Advisor

Re: how to delete old files including those in subdirectory

Hi Hakki,
sorry,i need to delete all old files (just keep recent 7 days) in backuplog dir & subdirectory (want to keep directory structure)

sorry, i'm new to unix in general
Hakki Aydin Ucar
Honored Contributor

Re: how to delete old files including those in subdirectory

Hi Kelvin,

OK then use this way:

first you have to go directory that your files there;

cd /directory

then if you will delete the old files they are older than 7 days just issue:

find /directory -name "*" -mtime +7 -exec rm {} \;
Dennis Handly
Acclaimed Contributor

Re: how to delete old files including those in subdirectory

>Hakki: find /directory -name "*" -mtime +7 -exec rm {} \;

If you want to do all files, no need for -name:
find /directory -mtime +7 -exec rm {} +
Steven Schweda
Honored Contributor

Re: how to delete old files including those in subdirectory

> If you want to do all files, no need for -name:

> cd /directory

No good reason for this "cd", either, with
"/directory" in the "find" command.

Also, if you're unsure about what damage this
command might do, you can replace "rm" with
"echo", which is much safer. Then, if you
see that it finds the files which you wish to
remove, run it again with the real "rm".
Kelvin Ng
Advisor

Re: how to delete old files including those in subdirectory

do i need to go into each subdirectory under backuplog dir for deletion? can i just issue command find /usr1/backuplog -mtime +7 -exec rm {} + (will this delete all old files in all subdirectory under backuplog dir?)
Din_1
Frequent Advisor

Re: how to delete old files including those in subdirectory

Please use the following command to find, what file is being in large size and you can truncate or delete that file...

find /dir_name | xargs ll -ld | sort -nrk5 | more

Thanks and Regards,
Din
Steven Schweda
Honored Contributor

Re: how to delete old files including those in subdirectory

> do i need to [...]

> "man find", [...]

Still a good idea.

What you might do is read some of these
responses. For example:

> Also, if you're unsure about what damage this
> command might do, you can replace "rm" with
> "echo", which is much safer. Then, if you
> see that it finds the files which you wish to
> remove, run it again with the real "rm".

In many cases, it's easier (and more
reliable) to _try_ something than it is to
ask how that thing will work.
Kelvin Ng
Advisor

Re: how to delete old files including those in subdirectory

Hi Dennis,

how to include deleting old files in subdirectory?

find /directory -mtime +7 -exec rm {} +
Steven Schweda
Honored Contributor

Re: how to delete old files including those in subdirectory

I give up.
Suraj K Sankari
Honored Contributor

Re: how to delete old files including those in subdirectory

Hi,
>>how to include deleting old files in subdirectory?

Then you need to apply two commands instead of one see the example

1st command will store your all old files into /tmp/oldfiles and 2nd will delete all file which are not recently updated within 7 days


#find /directory -mtime +7 -exec ls -l {} \; >/tmp/oldfiles

#find /directory -mtime +7 -exec rm {} \;

Suraj
Kelvin Ng
Advisor

Re: how to delete old files including those in subdirectory

Hi Suraj,

# find /usr1/backuplog -mtime +7 -exec ls -l {} \; >/tmp/oldfiles
# find /usr1/backuplog -mtime +7 -exec rm {} \;


# cd usr1/backuplog
# ls
friday monday saturday sunday thursday tuesday wednesday
# cd friday
# ls
924 comms conv corr1 dro ffg labels mdc reel workip xdata
# cd 924
# ls
XMIT0122.LOG XMIT0216.LOG XMIT0310.LOG XMIT0404.LOG XMIT0422.LOG


still didn't delete old *.LOG in subdirectory of backuplog dir. du -sk backuplog still show 15777040. /tmp/oldfiles 0 bytes
Dennis Handly
Acclaimed Contributor

Re: how to delete old files including those in subdirectory

>how to include deleting old files in subdirectory?

This is the default for find(1). Otherwise it is hard to not get subdirectories.
This is all documented under find(1).
Dennis Handly
Acclaimed Contributor

Re: how to delete old files including those in subdirectory

>still didn't delete old *.LOG in subdirectory of backuplog dir.

What does ll(1) show for that directory and file? Perhaps those files aren't old?
UXisCool
Advisor

Re: how to delete old files including those in subdirectory

Assigning some points to these good answers wouldn't hurt. Your record so far, isn't too good on that aspect.
Hakki Aydin Ucar
Honored Contributor

Re: how to delete old files including those in subdirectory

>Steven: Also, if you're unsure about what damage this
command might do, you can replace "rm" with
"echo", which is much safer. Then, if you
see that it finds the files

You are right when it comes to be sure before deletion. .