Can linux var log be deleted?

Mar 13, 2023 am 10:11 AM
linux

linux var log可以删掉,“/var/log”是Linux系统登录文件放置的地方,里面比较重要的文件有“/var/log/messages”、“/var/log/wtmp”等,其他不重要的日志就可以删除。

Can linux var log be deleted?

本教程操作环境: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
Copy after login

希望大家看过后,能给提出意见和建议,相信通过交流我们提高的更快。

相关推荐:《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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

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.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

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.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

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.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles