linux tools cheatsheet

本文全面介绍了Linux系统中常用的命令,包括文件操作、权限管理、进程查看、系统监控、日志处理及软件安装等方面,旨在帮助用户提升Linux操作技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


内置


数组

# 声明一个数组
declare -a pass
# 从文件读取一个数组,以换行符分割
readarray -t pass </etc/passwd
# 输出数组与输出普通变量不一样
echo ${testarr[@]}

权限

查看用户属于哪个组?

groups root

添加一个用户组

groupadd groupone
# 将root用户添加到groupone
usermod -g groupone root

文件

rsync

两个文件夹内容完全相同

rsync在复制大文件夹的场景比cp命令要适用,因为可以断点续传

rsync -avu --delete /releaseA/ /releaseB/
  • -a Do the sync preserving all filesystem attributes
  • -v run verbosely
  • -u only copy files with a newer modification time (or size difference if the times are equal)
  • --delete delete the files in target folder that do not exist in the source

Manpage: https://download.samba.org/pub/rsync/rsync.html

mkdir

  1. 如果目录已经存在,不再新建;如果不存在,就新建
mkdir -p /root/mysql5.7_HA

tar

  1. 解压到指定目录
tar xzf /root/mysql5.7_HA.tar.gz -C /root/mysql5.7_HA
  1. 创建压缩包
tar czvf youname.tar.gz mydir

I/O流


日志功能------tee

有的时候你需要同时输出内容到日志文件和STDOUT(屏幕上),可以使用

# 覆盖
echo "hello"|tee temp.log
# 追加
echo "hello"|tee -a temp.log

系统类


sync

手动去刷新系统的磁盘写缓冲区。写缓冲区是为了批量写入增加I/O效率。sync是强制刷盘操作。详情查看这篇博客

df 查看文件占用情况

在这里插入图片描述

parted -l 查看磁盘剩余空间

在这里插入图片描述

chgrp 改变权限组

如何在linux对硬盘(如/dev/sdb)进行分盘?

参考:linux下磁盘分区详解

# 查看系统硬盘
fdisk -l
# 对硬盘分区
fdisk /dev/sdb
输入n创建新分区
输入p选择分区类型为主分区
输入分区号
输入+20G选择分区大小为20G
# 分区完成,文件系统安装
fdisk -l查看设备号
mkfs -t ext3  /dev/hdb1
mount /dev/hdb1 /path/to/dest_dir

wget

  1. 如果已经下载了同名文件,不下载,并且指定下载目录
# -N指定如果服务器上的文件版本不比已下载的版本高,就不下载。如果文件还没下载,和正常的下载就一样
# -P 指定下载的前缀目录,这里下载到/root路径下
wget -N http://xxxx/xxx/dbaas/mysql/5.7/mysql5.7_HA.tar.gz -P /root

rpm

  1. 查看一个包(rpm package)是否安装
#检测gcc是否安装
rpm -qa|grep gcc

$

# 当前shell脚本
$0 
# shell进程
$$
#上一条命令的执行结果
$?
# 传入脚本的参数个数
$#
# 传入脚本的所有参数
$@ $*

监控类


uname

查看系统信息

Print certain system information.  With no OPTION, same as -s.

  -a, --all                print all information, in the following order,
                             except omit -p and -i if unknown:
  -s, --kernel-name        print the kernel name
  -n, --nodename           print the network node hostname
  -r, --kernel-release     print the kernel release
  -v, --kernel-version     print the kernel version
  -m, --machine            print the machine hardware name
  -p, --processor          print the processor type or "unknown"
  -i, --hardware-platform  print the hardware platform or "unknown"
  -o, --operating-system   print the operating system

top

vmstat

参考:https://gitee.com/oneslideicywater/Note/blob/master/liunx/vmstat.md


进程类


pstree

查看进程树

yum -y install psmisc
pstree -pl

tree

查看文件的目录层级

yum -y install tree
tree path_to_target

文本处理


envsubst

运行env会输出一堆环境变量,你可以使用环境变量去替代文本。

比如你有一个文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ${APP_NAME}
  namespace: ${NAMESPACE}
  labels:
    app: ${APP_NAME}
spec:
  replicas: ${REPLICA}
  selector:
    matchLabels:
      app: ${APP_NAME}
  template:
    metadata:
      labels:
        app: ${APP_NAME}
    spec:
      containers:
        - name: ${APP_NAME}
          image: ${CURRENT_DOCKER_IMAGE_TAG}
          imagePullPolicy: Always
          ports:
            - containerPort: ${CONTAINER_PORT}

可以使用envsubst去填充:

cat deployment.yaml | envsubst > .dump.yaml

crudini

操作INI格式配置文件

# 增加一个section 
crudini --set test.ini server2 id 1
# 获取一个section的全部key
crudini --get test.ini server
# 获取某个key的值
crudini --get test.ini server sid

server2是section,id是key,1是value

logratate

logrotate工具处理日志轮转,配置文件在/etc/logrotate.conf

很多情况下,logrotate由在 /etc/cron.daily/目录下的脚本唤醒。

#!/bin/sh
logrotate /etc/logrotate.conf

样例:/etc/logrotate.conf
日志滞留52周,单个日志尺寸100M,每周轮转一次

 /var/log/repmgr/repmgrd.log {
        missingok
        compress
        rotate 52    
        maxsize 100M
        weekly
        create 0600 postgres postgres
        postrotate
            /usr/bin/killall -HUP repmgrd
        endscript
    }

tr

  • https://www.thegeekstuff.com/2012/12/linux-tr-command/

script

# 记录
script --timing=time_log playscript.log
# 这个时候你可以键入命令,然后执行完键入exit
exit
# 重放,会输出到屏幕上
scriptreplay --timing=time_log playscript.log

seq

  • https://www.geeksforgeeks.org/seq-command-in-linux-with-examples/
seq [OPTION]... LAST
seq [OPTION]... FIRST LAST
seq [OPTION]... FIRST INCREMENT LAST

# 生成节点id等信息
[root@node1 ~]# seq -f "node%3g" 2 4
node  2
node  3
node  4
[root@node1 ~]# seq -f "node%4g" 2 4
node   2
node   3
node   4
# 替换默认分隔符"\n"
[root@node1 ~]# seq -w -s " "  2  2 40
02 04 06 08 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

# 但是 -w 和 -f不能联用
Reference
  1. https://www.linode.com/docs/uptime/logs/use-logrotate-to-manage-log-files/

CentOS 安装图形界面

yum -y groupinstall "X Window System" 
yum grouplist
yum -y groupinstall "GNOME Desktop" "Graphical Administration Tools"
ln -sf /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target

yum安装慢的问题

参考:解决Centos7使用yum 下载速度慢的问题

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

ulimit

其中比较重要且常见的:

# 最大打开文件数 
ulimit -n  
# 硬限制
ulimit -Hn
# 软限制
ulimit -Sn 

调整系统参数

查看系统参数,比如内存,cpu,打开文件数量

/proc/meminfo
/proc/cpuinfo
/proc/sys/fs/file-max  #系统总限制
/proc/sys/fs/file-nr #整个系统目前使用的文件句柄数量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值