search

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

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

相关推荐:《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
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
The 5 Core Components of the Linux Operating SystemThe 5 Core Components of the Linux Operating SystemMay 08, 2025 am 12:08 AM

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The 5 Essential Elements of Linux: ExplainedThe 5 Essential Elements of Linux: ExplainedMay 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

See all articles

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

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment