Solved

Delete files older than

Posted on 2010-11-23
21
736 Views
Last Modified: 2012-06-27
Running HP UX
I have backups going to a folder, and I want to be able to delete files/folders older than 7 days.

How can I accomplish this?
0
Comment
Question by:shankshank
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
  • +3
21 Comments
 
LVL 4

Expert Comment

by:zcrammond
ID: 34197210
script found elsewhere:

Dim Fso

Dim Directory

Dim Modified

Dim Files

Set Fso = CreateObject(“Scripting.FileSystemObject”)

Set Directory = Fso.GetFolder(“BACKUP DIRECTORY”)

Set Files = Directory.Files

For Each Modified in Files

If DateDiff(“D”, Modified.DateLastModified, Now) > # Then Modified.Delete

Next


Change BACKUP FILE's with path to the folder containing the files and then change # with a number of days you want kept

save file as a .VBS and execute using this command: cscript.exe filename.vbs
0
 
LVL 5

Author Comment

by:shankshank
ID: 34197327
No..

This is for UNIX not windows
0
 
LVL 4

Expert Comment

by:thunderhead
ID: 34197358
find . -mtime 7 -exec rm '{}' \;

Should do it.

* please test this in non-production before hand *
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:shankshank
ID: 34197363
Will  that delete folders too?
0
 
LVL 4

Expert Comment

by:thunderhead
ID: 34197376
Its not my code, so I have not tested it
0
 
LVL 9

Expert Comment

by:jeremycrussell
ID: 34197380
Depending on how the files are created or accessed you can use the 'find' command.

find /path/to/files -ctime -7 -delete

would delete files created more than 7 days ago.  However, you have to be careful using ctime, other options are mtime and atime.

A file could have a ctime of more than 7 days ago, but have been modified or accessed recently and show a newer timestamp in ls.

I would suggest you "man find" and research ctime, mtime and atime before you start to delete.

One way to check things before you delete them is to run

find /path/to/files -ctime -7 -exec ls -al {} \;

And this will list the files that would be deleted with the other command, and you can see if this works as you would expect it.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34197401
To answer the question "Will  that delete folders too?"

Yes it will! To avoid this add "-type f" to the find command:

find /path/to/files -type f ... ...

wmp
0
 
LVL 9

Expert Comment

by:jeremycrussell
ID: 34197411
To restrict find to just files add the -type f option to find.
0
 
LVL 5

Author Comment

by:shankshank
ID: 34197452
is it -ctime +7 or -7

i thought i had to use-
0
 
LVL 9

Expert Comment

by:jeremycrussell
ID: 34197508
-[acm]time 7 would be files just 7 days ago.
-[acm]time +7 would be all files 7 days or older.
-[acm]time -7  would be all files in last 7 days.
0
 
LVL 5

Author Comment

by:shankshank
ID: 34197510
jeremy your check command gives phony output
0
 
LVL 5

Author Comment

by:shankshank
ID: 34197513
by phony i mean it doesnt list what must be deleted. haha
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34197523
Use only -ls instead of -exec ls ... ...\;
0
 
LVL 5

Author Comment

by:shankshank
ID: 34197531
I want to list files 5 days or older

So this?

find /BACKUPHD -ctime +5

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34197542
Yep. Add -ls for an "ls -l" like output.
0
 
LVL 5

Author Comment

by:shankshank
ID: 34197552
/BACKUPHD>find /BACKUPHD -ctime +5 -ls
find: bad option -ls
0
 
LVL 4

Expert Comment

by:pablomorales
ID: 34197586
I use mtime in my scripts with good results. Use the following to check which files will be deleted (replace /path/to/files with the path of the directory you want to clean) :

find /path/to/files -mtime -7 -print
or for only files use:
find /path/to/files -mtime -7 -type f -print

To actually delete them use:

find /path/to/files -ctime -7 -exec rm -rf {} \;
or to delete only files use:
find /path/to/files -ctime -7 -type f -exec rm -rf {} \;

cheers!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34197589
Seems that HPUX's find doesn't know -ls. Sorry.
0
 
LVL 4

Expert Comment

by:pablomorales
ID: 34197617
If you want 5 days or older use to check:

find /path/to/files -mtime +5 -print
or for only files use:
find /path/to/files -mtime +5 -type f -print

To actually delete them use:

find /path/to/files -ctime +5 -exec rm -rf {} \;
or to delete only files use:
find /path/to/files -ctime +5 -type f -exec rm -rf {} \;
0
 
LVL 4

Accepted Solution

by:
pablomorales earned 500 total points
ID: 34197644
Ooops. I meant:

find /path/to/files -mtime +5 -print
or for only files use:
find /path/to/files -mtime +5 -type f -print

To actually delete them use:

find /path/to/files -mtime +5 -exec rm -rf {} \;
or to delete only files use:
find /path/to/files -mtime +5 -type f -exec rm -f {} \;
0
 
LVL 5

Author Comment

by:shankshank
ID: 34304578
okay i wil l ry this
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Execute multiple curl cmds with sleep and send output to file 10 114
UNIX SCP 5 87
unable to put logic for reading multiple repo in a single file 4 68
lunix and unix command 21 115
Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question