scripts for automatically deleting outdated files under windows
Preface :
For example, log files are generated every day, long time will have a lot of rubbish. Sort out How to delete files regularly.
text :
Windows:
periodically delete tomcat logs and caches. Days can be retained
forfiles/p "E:\Program files\tomcat 7.0\logs"/s/m *.log/d -5/c "cmd/c del @path"
forfiles/p "E:\Program files\tomcat 7.0\logs"/s/m *.txt/d -5/c "cmd/c del @path"
The red is the directory.
Green is a file that is formatted with. log and . txt suffix will be deleted.
Blue is the number of days reserved, and logs and caches that remain within the number of days are not deleted.
Create a bat file, modify the batch command as required, put the modified command into the bat file, and save.
then through the task plan on the system, according to the requirements to customize the creation of tasks, and finally the system automatically perform tasks, to automatically delete the Tomcat log and cache purposes.
Additional Notes :
There is a forfiles command in the Windows Server 2003 system to find files that meet the specified criteria, and the following is the use of this command.
forfiles/p < target directory name >/d < days >/C < commands >
Example:forfiles/p "D:bigantlogs"/s/m * */d-3/C "cmd/c del @path"
Explain the relevant parameters and commands
/P: Specifies the location at which to start searching for files, or defaults to the current directory if not specified.
/M : The wildcard character used by the file lookup, such as Span style= "font-family: Arial" > is all log files, of course, you can also specify such as . Span style= "font-family: Arial" > This is manmee
/d [{+|-}][{|}] : Specifies that you want to select the last modified time of the file . "/d-7" indicates that all are based on the day, 7 days before the file. Of course, you can also specify a specific time, for example:"/d-08/18/2009" so all earlier than the year 8 months - Day of the document. Note The specified time must be in the format "mm/dd/yyyy" .
/c for all files to execute the specified command, the command body must be within the double quotation mark (") .
Add: use Windows Task Scheduler to automatically restart the tamcat program:
- Click“Start"button, and then select"program"→"Accessories"→"System Tools"→"Task Scheduler"(or a"Set"→"Control Panel"→"Task Scheduler"), start"Add a task schedule".
2. In the pop-up page, select "Create basic task", the effect is as follows, enter a name, description. Next
3. Create a trigger and select the frequency at which the task is automatically performed ( such as Daily, weekly, monthly, one-time, every start of the computer, every logon , and so on ), and then click Next " button.
4. set up scripts or programs (custom scripts) to be executed for this task schedule .
5. Click Finish to complete the task. You can view the details in this scheduled task.
Linux:
A Tomcat Log Script that is scheduled to clean up 7 days ago
#!/bin/bash
Logs_path= "/tomcat/tomcat-token/logs"
Find $logs _path-mtime +7-name "Localhost_access_log.*.txt"-exec rm-rf {} \;
Find $logs _path-mtime +7-name "Catalina.*.log"-exec rm-rf {} \;
Find $logs _path-mtime +7-name "Manager.*.log"-exec rm-rf {} \;
Find $logs _path-mtime +7-name "Host-manager.*.log"-exec rm-rf {} \;
Find $logs _path-mtime +7-name "fileservice.log.*"-exec rm-rf {} \;
Scripts for automatically deleting outdated files under Windows