preface
The increasing number of log files on the server causes disk overflow, but not only log files, resulting in abnormal service crash.
Manually Performing deletion
Of course, before we delete a file, we need to find out what’s causing it.
Disk space usage
Df [options] [file]Copy the code
-
The first column is the pathname of the device file that represents the file system
-
Column 2 gives the number of data blocks (1024 bytes) that the partition contains
-
Columns 3 and 4 indicate the number of used and available data blocks, respectively
-
Column 5 shows the percentage of space used
-
Column 6 represents the mount point of the file system.
For normal use, add a -h option after df.
df -h
Copy the code
Output is displayed digitally in the form of ‘G’ (gigabytes), “M” (megabytes) and “K” (kilobytes), adding to the reading sensation.
Then see which directory has the highest percentage of usage and go to that directory to find large files.
Displays the size of a directory or file
Next, use a new command, du, to display the size of a directory or file.
[root@root /]# cd usr/software/
[root@root software]# du -hs
267M .
Copy the code
-
-h or –human-readable will improve the readability of the information in units of K, M, or G.
-
-s or –summarize just displays the sum.
delete
If you find a file that is too large, you can execute the delete command.
- Delete file command:
Find directory -path Ignore a directory -prune -name "filename" -mtime + days -exec rm -rf {} \;Copy the code
- The instance
find /opt/app/log -path /opt/app/log/sal -prune -o -mtime +3 -name "*log*" -print > /opt/chimes/lastlog.log -exec rm -rf {} \;
Copy the code
Select * from /opt/app/log; select * from /opt/app/log; select * from /opt/app/log.
- Path Search path
- Prune -o: ignores a directory, and the order of -prune -o cannot be reversed
- Mtime -n/+n Searches for files by file change time. -n indicates within N days, and +n indicates n days ago
- Name fileName: Searches for a file named fileName
- The print: find command outputs the matched files to standard output
- Exec: a fixed expression
- Rm -rf: forcibly deletes files, including directories
- {}; : fixed writing,
Note: {} and \; There's a space between
Timing task
If it’s too much trouble to manually execute a delete statement every few days, you can write the statement to an executable shell script, then create a Cron scheduling task and let the system do it for you.
Creating a script File
[root@root /]# cd /usr/local/bin/
[root@root bin]# vim clear.sh
Copy the code
Sh content
#! /bin/sh
find /opt/app/log -path /opt/app/log/sal -prune -o -mtime +3 -name "*log*" -print > /opt/chimes/lastlog.log -exec rm {} \;
Copy the code
Save and exit :wq!
Test whether the script is available
[root@root bin]# ./clear.sh
Copy the code
Nothing out of the ordinary is good.
Write crON tasks
[root@root bin]# crontab -e 0 0 * * * /usr/local/bin/clear.sh >> /usr/local/bin/clear.logCopy the code
‘0 0 * * * /usr/local/bin/clear
Run the clear-. sh file at 00:00 every day to clear data.
Viewing script Content
[root@root bin]# crontab -l
0 0 * * * /usr/local/bin/clear.sh >> /usr/local/bin/clear.log
Copy the code
- If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.