To nullify multiple log files older than X days.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To nullify multiple log files older than X days.
# 1  
Old 02-07-2017
To nullify multiple log files older than X days.

Hi,
I am writing the script for tomcat logs clean up ending with *.txt and *.log.
cleanup should be applied to logs older than 7 days logs same will be scheduled in cron.

after googling found below but it only help partially.
Code:
find . -type f -name '*.log' -exec truncate --size 0 "{}" \;

I can list out the files older than 7 days.

Code:
find . -mtime +7 -exec ls -lrt {} \;

getting issue while combining the commands ,any best pointer can i have to the task?
# 2  
Old 02-07-2017
If you are getting the list of files you want with one of the above, what would you prefer to happen next?

The second find command is selecting file in the current directory and below that are older than seven days, more correctly more than 7*24*60*60 seconds since last modified. The -exec ls -lrt {} \; section then attempts to give you the long (reversed time ordered) listing of each file. This is a one-at-a-time list, so the output will still be unsorted. Does this really matter?

You might do better with one of these:-
Code:
find . -mtime +7 -exec ls -lrt {} +
find . -mtime +7 | xargs ls -lrt

.... however if there are too many files to list in one go, this will break into multiple ls commands and the output will be sorted in chunks but still not entirely.


What is the eventual plan for the selected files?



Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 02-07-2017
What if you add the -mtime test to the first command?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-07-2017
Eventual plan is to nullify the *.log and *.txt logs older than 7 days and to schedule the script .

---------- Post updated at 08:32 AM ---------- Previous update was at 07:44 AM ----------

Quote:
Originally Posted by RudiC
What if you add the -mtime test to the first command?
Code:
find . -type f -name '*.log' -mtime +7 -exec truncate --size 0 "{}" \;

How to add *.txt in the above command means multiple extension.
# 5  
Old 02-07-2017
How about ORing in a second -name test using the -o operator (incl. parentheses)? Or, extend the ".log" pattern like [lt][ox][gt] (with the danger of false positives)?
# 6  
Old 02-07-2017
Below worked fine. Thanks.
Code:
find . -type f -name '*.log' -o -name '*.txt' -mtime +7 -exec truncate --size 0 "{}" \;


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-07-2017 at 10:05 AM.. Reason: Added CODE tags.
# 7  
Old 02-07-2017
You may have found ".log" files younger than 7 days as without grouping the -name tests using parentheses the -mtime will be valid for the ".txt" test only.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Listing files older than 30 days

Hi, Could someone help me that what the problem is in this code? #!/bin/sh FOLDER=/abc/datasource/checkstatus TIMESTAMP=$(date +%s) for filename in $(find $FOLDER -maxdepth 1 -type f -name "CHECK_STATUS*"); do f1=$($filename -Eo "{4}+") f2=$(date -d "$f1" +%s) if... (11 Replies)
Discussion started by: Home
11 Replies

2. Shell Programming and Scripting

Find files not matching multiple patterns and then delete anything older than 10 days

Hi, I have multiple files in my log folder. e.g: a_m1.log b_1.log c_1.log d_1.log b_2.log c_2.log d_2.log e_m1.log a_m2.log e_m2.log I need to keep latest 10 instances of each file. I can write multiple find commands but looking if it is possible in one line. m file are monthly... (4 Replies)
Discussion started by: wahi80
4 Replies

3. Shell Programming and Scripting

List files older than 10 days.

Hello all, I want to list the files older than 10 days. Currently am using find ./ -mtime +10 -exec ls -ltr {} \; command. But I want to execute the same command in 16 directories at a time and want an output asking to remove those file? Please help me to design the script. regards, Ajay (3 Replies)
Discussion started by: 02Ajay
3 Replies

4. UNIX for Dummies Questions & Answers

Find all log files under all file systems older than 2 days and zip them

Hi All, Problem Statement:Find all log files under all file systems older than 2 days and zip them. Find all zip files older than 3days and remove them. Also this has to be set under cron. I have a concerns here find . -mtime +2 -iname "*.log" -exec gzip {} Not sure if this will work as... (4 Replies)
Discussion started by: saurabh.mishra
4 Replies

5. Shell Programming and Scripting

Script to delete files older than x days and also taking an input for multiple paths

Hi , I am a newbie!!! I want to develop a script for deleting files older than x days from multiple paths. Now I could reach upto this piece of code which deletes files older than x days from a particular path. How do I enhance it to have an input from a .txt file or a .dat file? For eg:... (12 Replies)
Discussion started by: jhilmil
12 Replies

6. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

7. UNIX for Dummies Questions & Answers

Size of files which are older than x days

Hi I want to find the total space used by the files which are older than x days find ./ -type f -mtime +x-days -name "G00*" -exec du {} \; | awk '{total+=$1}END{print "TOTAL" total}' Total prints as 17.20 MB ( total / 1024*2 ) But actual size of it will be around 18.5 GB... (1 Reply)
Discussion started by: rakeshkumar
1 Replies

8. UNIX for Dummies Questions & Answers

Files older than 50 days

Hi All, OS :- HP-UX wm5qa B.11.23 U ia64 1119805695 unlimited-user license I need to search files older than 50 days. I've used following command in order to search desired files, I also discoverd, it's showing today's files as well. Do you have any clue with this ? wmqa1> find .... (4 Replies)
Discussion started by: alok.behria
4 Replies

9. UNIX Desktop Questions & Answers

Find files older than 10 days

What command arguments I can use in unix to list files older than 10 days in my current directory, but I don't want to list the hidden files. find . -type f -mtime +15 -print will work but, it is listing all the hidden files., which I don't want. (4 Replies)
Discussion started by: Pouchie1
4 Replies

10. UNIX for Dummies Questions & Answers

Removing files older than 7 days

Script help, I need to delete files that are older than 7 days. I do that automatically but I know that a cron job can do the job for me. Any help is greatly appreciated, as you can see, I am a DOS or WINDOWS guy. Little on UNIX. Thanks (3 Replies)
Discussion started by: texasoeb
3 Replies
Login or Register to Ask a Question