I am looking for a command for clearing all files in a directory that has become more than 100 MB. I am using an AIX box and it is generating logs that are filling the space quickly. I do not wish to delete the files. Just want to clear the contents.
Asked
Active
Viewed 1,314 times
0
-
What is 100 MB? The files? Or is the size of the directory itself 100 MB? What do you mean with "clear the contents"? If you truncate the files to 0 bytes, would that be acceptable? Why not delete them instead? Or compress them? – fork2execve Mar 16 '15 at 21:14
-
Compressing is a good option. I shall explore that. I cannot delete as once deleted the system cannot create the file. – Sandeep Mar 17 '15 at 11:50
-
Depending upon which application is creating the log files, there may be an application specific solution such as rotatelogs or similar. – pedz Mar 17 '15 at 16:25
3 Answers
1
When the progs don't keep a file handle open, you can use
find yourLogDir -type f -size +100M -exec cp /dev/null {} \;
Edit, see comments:
When the M flag is not supported on your system, enter a long number.

Walter A
- 19,067
- 2
- 23
- 43
-
1I tested this a little bit on AIX 6.1. The 100M syntax I believe is not really working. Its probably safer to put +104857600 (or whatever size you want). It seems to be more reliable / portable. – pedz Mar 17 '15 at 16:24
-
@pedz Tx. I do not have a AIX at home, so I could not verify my solution. I thought Sandeep would understand when the M syntax wasn't supported. And he did. – Walter A Mar 17 '15 at 21:15
1
You can use the following command:
find LogDir -type f -size +104857600c -exec cp /dev/null {} \;
The 'c' after 104857600 indicates that the number is in bytes

jgruiz
- 26
- 3
0
I did some debugging on @WalterA's answer on my AIX machine. It looks like we have to change the provided command a bit:
find yourLogDir -type f -size +(multiple of 512 Bytes) -exec cp {} /dev/null \;
For example, if you want to copy a log file with a size of more than 100 MB, just execute the below command with proper directory path:
find yourLogDir -type f -size +204800 -exec cp {} <dir path to copy> \;

honk
- 9,137
- 11
- 75
- 83

Pravin Junnarkar
- 800
- 1
- 5
- 13