less than 1 minute read

There’s a Linux server that hasn’t been properly managed. I’ve put together, in the order I use them, the commands for deleting that server’s log files.


1. Check disk usage

df

2. Check usage per subdirectory

du -h --max-depth=1 # Check directory usage only
du -sh *            # Check usage for both directories and files

3. Check files

Search for and sort log files older than 30 days

find ./ -name '*.log.*' -mtime +29 | sort

4. Delete files

Delete log files older than 30 days

find ./ -name '*.log.*' -mtime +29 -delete

5. Compress files, then delete

Compress log files older than 2 days, then delete the originals

find ./ -name '*.log.*' -mtime +1 -exec sh -c "tar cjvf {}.bz2 {}; rm -f {};" \;

Tags:

Categories:

Updated:

Leave a comment