![]() ![]() Resulting in a file between 100 MB and 99 MB.īut I like simplicity so for now I'm going with the first option. But it takes more complexity you would need to first calculate the current size of the file to know how much to deallocate, or make a little loop, something like: while (file larger than 100M) fallocate 1M I understand it does it in place, instead of copying data into a temporary file. It will cut off bytes from your file starting at position 0, it deallocates 1 MB of disk space off your file. If that's your case, you could try this: fallocate -c -o 0 -l 1M file This could be a problem if you want to keep, say, the last 1 GB of your log file, and your storage is too small or too slow. It has the disadvantage that it will allocate in your file system, temporarily, 1 MB of space. tail -c 1M /tmp/myfile > /tmp/dzqxH4ZMiSQb91uMMMgPhsgmpnc & rm /tmp/myfile & mv /tmp/dzqxH4ZMiSQb91uMMMgPhsgmpnc /tmp/myfileįirst command cuts 1MB of the target file into a temp file, second command deletes the target file, and the third one renames the 1MB temp file as your target file. This command will truncate log file /tmp/myfile, it cuts the beginning and not the end of the file, it does not require you to configure services and install multi-line scripts just a one-line command you can add to your crontab. 100000 for 100 kB.īeing a fan of simplicity and high maintainability, I made this. Note: in the script above, SIZE is in bytes, e.g. But I guess there is simply no other way to find on the web. To copy the file instead: cd /somedir cp -i file1 ' (xdg-user-dir DESKTOP)' The -i stands for 'interactive.' It will cause the move and copy commands to prompt you before overwriting any existing file. The reason I am still googling this is that I am somewhat concerned about the (remote) possibility that some process writes to the log file just as I am moving it into position. Set msg="Error finding the new size of $file" If (`echo "$oldsize" | grep -c '^*$'` != 1) goto maxsize = $size + $size Set msg="Error finding the size of $file" # twice that amount, by junking the beginning of it. # Purpose: reduce the size of log file FILE to SIZE, if it exceeds Set echo_style='none' setenv LC_ALL C # set echo verbose Since this is getting a bigger thing for me now, I just now wrote a little tcsh script to do it: #!/bin/tcsh -f I have been using tail -c for many years. Try 'which tail' and 'where tail' to see what is wrong. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |