• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 756
  • Last Modified:

Delete files older than

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
shankshank
Asked:
shankshank
  • 8
  • 4
  • 3
  • +3
1 Solution
 
zcrammondCommented:
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
 
shankshankAuthor Commented:
No..

This is for UNIX not windows
0
 
thunderheadCommented:
find . -mtime 7 -exec rm '{}' \;

Should do it.

* please test this in non-production before hand *
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
shankshankAuthor Commented:
Will  that delete folders too?
0
 
thunderheadCommented:
Its not my code, so I have not tested it
0
 
jeremycrussellCommented:
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
 
woolmilkporcCommented:
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
 
jeremycrussellCommented:
To restrict find to just files add the -type f option to find.
0
 
shankshankAuthor Commented:
is it -ctime +7 or -7

i thought i had to use-
0
 
jeremycrussellCommented:
-[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
 
shankshankAuthor Commented:
jeremy your check command gives phony output
0
 
shankshankAuthor Commented:
by phony i mean it doesnt list what must be deleted. haha
0
 
woolmilkporcCommented:
Use only -ls instead of -exec ls ... ...\;
0
 
shankshankAuthor Commented:
I want to list files 5 days or older

So this?

find /BACKUPHD -ctime +5

0
 
woolmilkporcCommented:
Yep. Add -ls for an "ls -l" like output.
0
 
shankshankAuthor Commented:
/BACKUPHD>find /BACKUPHD -ctime +5 -ls
find: bad option -ls
0
 
pablomoralesCommented:
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
 
woolmilkporcCommented:
Seems that HPUX's find doesn't know -ls. Sorry.
0
 
pablomoralesCommented:
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
 
pablomoralesCommented:
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
 
shankshankAuthor Commented:
okay i wil l ry this
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now