Automatically Split and Rotate Large Linux Log Files
This guide shows how to automatically split growing Linux log files into 60 MB chunks with date‑based naming, using the `date` and `split` commands in a Bash script, and then safely truncate the original log to free disk space.
Linux servers often generate continuous log output that can quickly fill up disk space. This tutorial explains a method to split large log files into manageable pieces, name them with the previous day's date, and safely clear the original log.
Get Yesterday's Date
First, obtain the date for the previous day, which will be used in the split file names: current_date=`date -d "-1 day" "+%Y%m%d"` The format +%Y%m%d produces a string like 20230412.
Split the Log File
Use the split command to divide the log into 60 MB chunks, adding a four‑digit numeric suffix:
split -b 65535000 -d -a 4 /home/alvin/myout.txt /home/alvin/log/log_${current_date}_Parameters: -b 65535000 – split size of 60 MB (bytes). -d -a 4 – use numeric suffixes of length 4 (e.g., 0000, 0001).
The output files will be named like log_20230411_0000, log_20230411_0001, etc.
Truncate the Original Log
After splitting, clear the original log (e.g., nohup.out) to prevent further disk consumption:
cat /dev/null > nohup.outFull Bash Script
Combine the steps into a single script that can be scheduled to run daily (e.g., via cron):
#!/bin/bash
current_date=`date -d "-1 day" "+%Y%m%d"`
split -b 65535000 -d -a 4 /home/alvin/myout.txt /home/alvin/log/log_${current_date}_
cat /dev/null > nohup.outRunning this script each day will produce date‑stamped log fragments and free up space by truncating the original log file.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
