Can linux var log be deleted?
linux var log可以删掉,“/var/log”是Linux系统登录文件放置的地方,里面比较重要的文件有“/var/log/messages”、“/var/log/wtmp”等,其他不重要的日志就可以删除。
本教程操作环境:linux5.9.8系统、Dell G3电脑。
linux var log 可以删掉吗?
可以。
var log里就是记录点日志而已,可以删除,不过为了句柄安全,最好删除后重启xenserver。
【Shell脚本】清除/var/log下的日志文件
【脚本要求】
清除/var/log目录下/var/log/messages和/var/log/wtmp中的内容;
该脚本带一个参数用来设置保留日志的行数,无参数时时默认保留最后50行,有参数时判断参数是否是纯数字;
只有root身份才能执行此脚本,非root用户执行此脚本时,将以error形式退出并返回错误代码;
判断是否正确进入到/var/log目录,如果不能进入到该目录,将以error形式退出并返回错误代码;
【基础知识】
/var/log是Linux系统登录文件放置的地方。里面比较重要的文件有/var/log/messages,/var/log/wtmp等。
/var/log/messages存放的是系统发生错误时的信息,如果系统发生莫名其妙的错误,那么一定要查看此文件。
/var/log/wtmp则记录了正确登陆过系统的帐号信息,对于追踪登陆系统者的行为很有帮助。
【知识点总结】
1、任何shell脚本第一行都应该是以#!开头。除了脚本第一行的#其它行的#表示脚本的注释。注释可以放在命令行的结尾,也可以另起一行。
2、LOG_DIR、UID_ROOT、LINE、E_XCD、E_NOTROOT、E_WRONGARGS等常量要大写
3、$UID是当前登陆用户的UID,root用户的UID为0
4、-nq用于两个整数之间相等的比较
5、判断式中括号[]的两端和需要有空格的地方都有加空格。例如,[空$UID空-nq空$UID_ROOT空]
6、双引号""中的特殊符号保持原有含义,单引号''中的特殊字符一律看作一般字符。
7、此脚本用到if语句和case语句,注意他们的语法结构。
8、*是通配符,代表任意数量的任意字符,它也可以用来匹配给定目录下任意文件名,在算术运算中代表乘法。
9、[!0-9],[0-9]代表0~9这10个数字,!表示取反,这里代表不含有0~9这10个数字。
10、||连接两个命令时,当前面的命令执行错误时(命令返回码不为0),才执行后面的命令。与此相对的是&&,&&前面的命令执行正确时,才执行后面的命令。
11、大括号{}中间的成为代码块,其中声明的变量对于脚本其他部分代码来说还是可见的。而小括号()中生命的变量对脚本其他部分来说是不可见的,因为()中的代码将作为一个子Shell来运行。
12、tail命令是输出文件的后面若干行,语法是tail -n filename表示输出filename最后面的n行。类似的还有命令head,表示输出文件的前面若干行,语法是head -n filename表示输出filename最前面的n行。
13、>&2代表将stdout指定到stderr。其它数据流重定向符还有:>代表的数据流重定向功能,以覆盖的方法将stdout指定到文件或者设备上,>>代表以追加的方法将stdout指定到文件或者设备上,2>以覆盖的方法将stderr指定到文件或者设备上,2>>代表以追加的方法将stderr指定到文件或者设备上。&>以覆盖的方法将stdout和stderr指定到文件或者设备上,&>>代表以追加的方法将stdout和stderr指定到文件或者设备上。
14、mv命令可以将文件或者目录移动到一个指定的目录,带上参数-i可以提示目标存在时是否覆盖;也可以对文件或者目录进行重命名。
15、/dev/null是一个垃圾桶黑洞设备,有非常重要的作用,一是可以产生空白内容,另外可以吞噬任何导向这个设备的信息。此处是用来产生空白内容。
16、分号“;”用于将同一行上的多个命令分隔开来。
【脚本清单】
#!/bin/bash LOG_DIR=/var/log UID_ROOT=0 LINE=50 E_XCD=66 E_NOTROOT=67 E_WRONGARGS=65 #保证只有root用户才可以执行此脚本 if [ $UID -nq $UID_ROOT ] ; then echo "This script. must be run by root" exit $E_NOTROOT fi #测试命令行的参数 case $1 in " " )lines=$LINE;; *[!0-9]*)echo "Usage: `basename $0` file-to-cleanup";exit $E_WRONGARGS;; * )lines=$1;; esac #处理log之前,再次确认当前目录是否正确 cd &LOG_DIR || { echo "Cannot change to &LOG_DIR" >&2 exit $E_XCD } #处理log tail -$lines messages>mesg.temp mv mesg.temp messages cat /dev/null > wtmp #处理完毕 echo "Logs cleaned up " exit 0
希望大家看过后,能给提出意见和建议,相信通过交流我们提高的更快。
相关推荐:《Linux视频教程》
The above is the detailed content of Can linux var log be deleted?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.
