Linux常用命令
names=(xxx yyy zzz)
path_pro=uuuu
echo "当前日期为:$dt"
echo "执行关闭操作:stopAll"
sh /program/$path_pro/stopAll
for (( i=0 ; i < ${#names[*]}; i ++ ))
do
filename=/program/$path_pro/${names[$i]}/logs/catalina.$dt.log
echo "当前文件名是:$filename"
rm -rf /program/$path_pro/${names[$i]}/logs/*.*
rm -rf /program/$path_pro/${names[$i]}/work/*
sh /program/$path_pro/${names[$i]}/bin/startup.sh
echo "${names[$i]}已经执行启动!"
sleep 3
done
man命令简单使用
按键 | 用处 | 按键 | 用处 |
---|---|---|---|
PaGe down | 向下翻一页 | PaGe up | 向上翻一页 |
home | 直接前往首页 | end | 直接前往尾页 |
/ | 从上至下搜索某个关键词,如“/linux” | ? | 从下至上搜索某个关键词,如“?linux” |
n | 定位到下一个搜索到的关键词 | N | 定位到上一个搜索到的关键词 |
空格键 | 向下翻一页 | q | 退出帮助文档 |
结构名称 | 代表意义 | 结构名称 | 代表意义 |
---|---|---|---|
NAME | 命令的名称 | SYNOPSIS | 参数的大致使用方法 |
DESCRIPTION | 介绍说明 | EXAMPLES | 演示(附带简单说明) |
OVERVIEW | 概述 | DEFAULTS | 默认的功能 |
OPTIONS | 具体的可用选项(带介绍) | ENVIRONMENT | 环境变量 |
FILES | 用到的文件 | SEE ALSO | 相关的资料 |
HISTORY | 维护历史与联系方式 |
echo
[root@michael ~]# echo '你好!世界!'
你好!世界!
[root@michael ~]# echo $SHELL
/bin/bash
date
参数 | 作用 | 参数 | 作用 | 参数 | 作用 |
---|---|---|---|---|---|
%Y | 年xxxx | %m | 月xx | %d | 日xx |
%H | 小时(00~23) | %M | 分钟(00~59) | %S | 秒(00~59) |
%I | 小时(00~12) | %t | 跳格[Tab键] | %j | 今年中的第几天 |
[root@michael ~]# date
Sun Jan 27 12:52:44 CST 2019
[root@michael ~]# date "+%Y-%m-%d %H:%M:%S"
2019-01-27 12:52:53
将系统的当前时间设置为2017年9月1日8点30分
[root@michael ~]# date -s "20170901 8:30:00"
Fri Sep 1 08:30:00 CST 2017
[root@michael ~]# date "+%j"
027
---------------------------------------------------------
[root@localhost ~]# hwclock
Sat 10 Aug 2019 10:26:27 PM CST -0.739522 seconds
[root@localhost ~]# clock
Sat 10 Aug 2019 10:26:32 PM CST -0.817961 seconds
将硬件时间写入到系统时间:
[root@localhost ~]# hwclock -s
将系统时间写入到硬件时间
[root@localhost ~]# hwclock -w
---------------------------------------------------------
[root@localhost ~]# date +"%Y%m%d"
20191123
[root@localhost ~]# date -d"-1 days" +"%Y%m%d"
20191122
[root@localhost ~]# date -d"+1 days" +"%Y%m%d"
20191124
[root@localhost ~]# date -d "-1 month" +%Y%m%d
20191023
[root@localhost ~]# date -d "+1 month" +%Y%m%d
20191223
[root@localhost ~]# date -d "-1 year" +%Y%m%d
20181123
[root@localhost ~]# date -d "+1 year" +%Y%m%d
20201123
重启,关机
[root@michael ~]# poweroff
[root@michael ~]# shutdown -h now
[root@michael ~]# shutdown -h 10
[root@michael ~]# shutdown -c
[root@michael ~]# reboot
ps
参数 | 作用 | 参数 | 作用 | 参数 | 作用 |
---|---|---|---|---|---|
-a | 显示所有进程(包括其他用户) | -u | 用户和其他详细信息 | -x | 显示无控制终端的进程 |
-
在Linux系统中,有5种常见的进程状态:
R(运行)
:进程正在运行或在运行队列中等待。
S(中断)
:进程处于休眠中,当某个条件形成后或者接收到信号时,则脱离该 状态。
D(不可中断)
:进程不响应系统异步信号,即便用kill命令也不能将其中断。
Z(僵死)
:进程已经终止,但进程描述符依然存在, 直到父进程调用wait4()系统函数后将进程释放。
T(停止)
:进程收到停止信号后停止运行。 -
ps aux 命令后显示的进程状态:
header | desc | header | desc |
---|---|---|---|
USER | 进程的所有者 | PID | 进程ID号 |
%CPU | 运算器占用率 | %MEM | 内存占用率 |
VSZ | 虚拟内存使用量(单位是KB) | TTY | 所在终端 |
RSS | 占用的固定内存量(单位是KB) | STAT | 进程状态 |
START | 被启动的时间 | TIME | 实际使用CPU的时间 |
COMMAND | 命令名称与参数 |
[root@localhost ~]# ps aux | grep -E 'PID|systemd'
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 53764 7604 ? Ss 21:42 0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 24
root 824 0.0 0.1 42992 3072 ? Ss 21:42 0:00 /usr/lib/systemd/systemd-journald
root 864 0.0 0.1 42688 2444 ? Ss 21:42 0:00 /usr/lib/systemd/systemd-udevd
root 1022 0.0 0.0 34684 1720 ? Ss 21:42 0:00 /usr/lib/systemd/systemd-logind
dbus 1024 0.0 0.1 37164 2168 ? Ssl 21:42 0:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root 2237 0.0 0.0 112644 968 pts/0 R+ 22:09 0:00 grep --color=auto -E PID|systemd
[root@localhost ~]# ps -ef | grep -E "mysql|PID"
UID PID PPID C STIME TTY TIME CMD
root 2219 1971 0 22:07 pts/0 00:00:00 grep --color=auto -E mysql|PID
top
[root@michael ~]# top
top - 15:33:24 up 5:39, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 435 total, 1 running, 434 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 2035648 total, 563724 used, 1471924 free, 884 buffers
KiB Swap: 2097148 total, 0 used, 2097148 free. 199780 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5309 root 20 0 123920 1916 1148 R 0.3 0.1 0:00.06 top
1 root 20 0 53044 6800 3772 S 0.0 0.3 0:02.47 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
第1行:系统时间、运行时间、登录终端数、系统负载(三个数值分别为1分钟、5分钟、15分钟内的平均值,数值越小意味着负载越低)。
第2行:进程总数、运行中的进程数、睡眠中的进程数、停止的进程数、僵死的进程数。
第3行:用户占用资源百分比、系统内核占用资源百分比、改变过优先级的进程资源百分比、空闲的资源百分比等。其中数据均为CPU数据并以百分比格式显示,例如“97.1 id”意味着有97.1%的CPU处理器资源处于空闲。
第4行:物理内存总量、内存使用量、内存空闲量、作为内核缓存的内存量。
第5行:虚拟内存总量、虚拟内存使用量、虚拟内存空闲量、已被提前加载的内存量。
pidof
[root@michael ~]# pidof sshd
2229 1680
killall
[root@michael ~]# killall httpd
执行命令时在末尾添加上一个&符号,命令将进入系统后台来执行;
uname
查看系统内核与系统版本等信息,格式 uname [-a]
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)
uptime
查看系统的负载信息
当前系统时间、系统已运行时间、启用终端数量、平均负载值等信息。
平均负载值:系统在最近1分钟、5分钟、15分钟内的压力情况;
负载值越低越好,尽量不要长期超过1,在生产环境中不要超过5。
[root@localhost ~]# uptime
22:47:15 up 1:04, 1 user, load average: 0.00, 0.03, 0.05
who
显示所有正在登录本机的用户名称以及他们正在开启的终端信息
[root@localhost ~]# who
root pts/0 2019-08-10 21:46 (192.168.168.2)
last
查看系统所有的登录记录,格式 last [参数]
[root@localhost ~]# last | tail -n 4
reboot system boot 3.10.0-123.el7.x Sun Aug 11 05:30 - 21:42 (-7:-48)
reboot system boot 3.10.0-123.el7.x Sun Aug 11 05:27 - 21:30 (-7:-56)
wtmp begins Sun Aug 11 05:27:08 2019
sosreport
命令用于收集系统配置及架构信息并输出诊断文档
cd,ls
[root@localhost ~]# cd -
[root@localhost ~]# cd ~
[root@localhost ~]# ls -ld /etc
cat more head tail
[root@localhost ~]# cat -n anaconda-ks.cfg
[root@localhost ~]# more anaconda-ks.cfg
[root@localhost ~]# head -n 20 anaconda-ks.cfg
[root@localhost ~]# tail -n 20 anaconda-ks.cfg
[root@localhost ~]# tail -f anaconda-ks.cfg
tr
替换文本文件中的字符,格式 tr [原始字符] [目标字符]
[root@localhost ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]
wc
统计指定文本的行数、字数、字节数,格式 wc [参数] 文本
[root@localhost ~]# wc -l /etc/profile
76 /etc/profile
[root@localhost ~]# wc -w /etc/profile
252 /etc/profile
[root@localhost ~]# wc -c /etc/profile
1750 /etc/profile
stat
查看文件的具体存储信息和时间等信息
[root@localhost ~]# stat anaconda-ks.cfg
File: ‘anaconda-ks.cfg’
Size: 1039 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 68900876 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:admin_home_t:s0
Access: 2019-08-10 23:19:53.191723702 +0800
Modify: 2019-08-11 05:26:21.750077374 +0800
Change: 2019-08-11 05:26:21.750077374 +0800
Birth: -
cut
按“列”提取文本字符
[root@localhost opt]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost opt]# cut -d: -f1 /etc/passwd | grep -E 'root|bin'
root
bin
diff
比较多个文本文件的差异
[root@localhost opt]# diff --brief test1.txt test2.txt
[root@localhost opt]# echo bbb >>test2.txt
[root@localhost opt]# diff --brief test1.txt test2.txt
Files test1.txt and test2.txt differ
[root@localhost opt]# diff -c test1.txt test2.txt
*** test1.txt 2019-08-11 15:53:34.578406473 +0800
--- test2.txt 2019-08-11 15:54:53.898402045 +0800
***************
*** 1 ****
--- 1,2 ----
aaa
+ bbb
[root@michael ~]# echo aa > aa.txt
[root@michael ~]# echo aa > bb.txt
[root@michael ~]# diff --brief aa.txt bb.txt
[root@michael ~]# echo $?
0
[root@michael ~]# echo bb >> bb.txt
[root@michael ~]# diff --brief aa.txt bb.txt
Files aa.txt and bb.txt differ
[root@michael ~]# echo $?
1
[root@michael ~]# echo ccC >> aa.txt
[root@michael ~]# echo bbb >> aa.txt
[root@michael ~]# echo Bbb >> bb.txt
[root@michael ~]# diff -c aa.txt bb.txt
*** aa.txt 2019-01-04 22:39:08.916997839 +0800
--- bb.txt 2019-01-04 22:39:18.244997803 +0800
***************
*** 1,3 ****
aa
! ccC
! bbb
--- 1,3 ----
aa
! bb
! Bbb
touch
创建空白文件或设置文件的时间
文件内容的修改时间(mtime
);
文件权限或属性的修改时间(ctime
);
文件的读取时间(atime
);
参数 | 作用 | 参数 | 作用 | 参数 | 作用 |
---|---|---|---|---|---|
-a | 仅修改“读取时间”(atime) | -m | 仅修改“修改时间”(mtime) | -d | 同时修改atime与mtime |
[root@localhost opt]# ll my.cnf
-rw-r--r-- 1 root root 960 8月 11 14:13 my.cnf
[root@localhost opt]# touch -d "1992-06-07 18:16:50" my.cnf
[root@localhost opt]# ll my.cnf
-rw-r--r-- 1 root root 960 6月 7 1992 my.cnf
cp
如果目标文件是目录,则会把源文件复制到该目录中;
如果目标文件也是普通文件,则会询问是否要覆盖它;
如果目标文件不存在,则执行正常的复制操作。
参数 | 作用 | 参数 | 作用 |
---|---|---|---|
-p | 保留原始文件的属性 | -d | 若对象为“链接文件”,则保留该“链接文件”的属性 |
-r | 递归持续复制(用于目录) | -i | 若目标文件存在则询问是否覆盖 |
-a | 相当于-pdr(p、d、r为上述参数) |
dd
按照指定大小和个数的数据块来复制文件或转换文件
按照指定大小和个数的数据块来复制文件的内容。还可以在复制过程中转换其中的数据。
[root@localhost opt]# dd if=/dev/zero of=/opt/test.txt count=2 bs=10M
记录了2+0 的读入
记录了2+0 的写出
20971520字节(21 MB)已复制,0.0341364 秒,614 MB/秒
[root@localhost opt]# dd if=/dev/cdrom of=test.iso
记录了7521150+0 的读入
记录了7521150+0 的写出
3813312160字节(3.8 GB)已复制,370.758 秒,15.6 MB/秒
file
查看文件类型,格式 " file 文件名 ”
[root@localhost opt]# file test.txt
test.txt: data
[root@localhost opt]# file test1.txt
test1.txt: ASCII text
tar
[root@localhost opt]# tar -czvf my.tar.gz /etc/sysconfig
[root@localhost opt]# tar -xzvf my.tar.gz -C /opt
[root@localhost opt]# tar -cjvf my.bz2 /etc/sysconfig
[root@localhost opt]# tar -xjvf my.bz2 -C /opt
grep
[root@localhost ~]# cat /var/log/mysqld.log | grep -c password
8
[root@localhost ~]# cat /var/log/mysqld.log | grep -ic password
8
[root@localhost ~]# cat /var/log/mysqld.log | grep -in password
6:2019-08-11T06:04:16.455359Z 1 [Note] A temporary password is generated for root@localhost: irsg*)DNs2up
省略...
[root@localhost ~]# cat /var/log/mysqld.log | grep -vn 2019
39:Version: '5.7.27' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
省略...
[root@localhost ~]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
find 指定条件来查找文件,格式 find [查找路径] 寻找条件 操作
[root@localhost ~]# find /etc -name "host*" -print
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
参数 | 作用 |
---|---|
-name | 匹配名称 |
-perm | 匹配权限(mode为完全匹配,-mode为包含即可) |
-user | 匹配所有者 |
-group | 匹配所有组 |
-mtime -n +n | 匹配修改内容的时间(-n指n天以内,+n指n天以前) |
-atime -n +n | 匹配访问文件的时间(-n指n天以内,+n指n天以前) |
-ctime -n +n | 匹配修改文件权限的时间(-n指n天以内,+n指n天以前) |
-nouser | 匹配无所有者的文件 |
-nogroup | 匹配无所有组的文件 |
-newer f1 !f2 | 匹配比文件f1新但比f2旧的文件 |
–type b/d/c/p/l/f | 匹配文件类型(后面的字幕字母依次表示块设备、目录、字符设备、管道、链接文件、文本文件) |
-size | 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件) |
-prune | 忽略某个目录 |
-exec …… {}; | 后面可跟用于进一步处理搜索结果的命令(下文会有演示) |
[root@localhost ~]# find /bin -perm -4000 -print
/bin/su
/bin/ping6
/bin/ping
/bin/umount
/bin/fusermount
/bin/mount
“{}” 表示find搜索出的每一个文件,“\;” 命令的结束符:
[root@localhost opt]# find /home/ -user michael -exec cp -a {} /opt/test \;
开关机
command | description |
---|---|
shutdown -h now | 立刻进行关机 |
shutdown -r now | 现在重新启动计算机 |
reboot | 现在重新启动计算机 |
logout | 用户注销 |
查看redhat版本
[root@michael ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)
rpm简单使用
description | command |
---|---|
安装软件的命令格式 | rpm -ivh filename.rpm |
升级软件的命令格式 | rpm -Uvh filename.rpm |
卸载软件的命令格式 | rpm -e filename.rpm |
查询软件描述信息的命令格式 | rpm -qpi filename.rpm |
列出软件文件信息的命令格式 | rpm -qpl filename.rpm |
查询文件属于哪个RPM的命令格式 | rpm -qf filename |
YUM仓库简单使用
command | description |
---|---|
yum repolist all | 列出所有仓库 |
yum list all | 列出仓库中所有软件包 |
yum info 软件包名称 | 查看软件包信息 |
yum install 软件包名称 | 安装软件包 |
yum reinstall 软件包名称 | 重新安装软件包 |
yum update 软件包名称 | 升级软件包 |
yum remove 软件包名称 | 移除软件包 |
yum clean all | 清除所有仓库缓存 |
yum check-update | 检查可更新的软件包 |
yum grouplist | 查看系统中已经安装的软件包组 |
yum groupinstall 软件包组 | 安装指定的软件包组 |
yum groupremove 软件包组 | 移除指定的软件包组 |
yum groupinfo 软件包组 | 查询指定的软件包组信息 |
Systemd初始化进程
System V init运行级别 | systemd目标名称 | 作用 |
---|---|---|
0 | runlevel0.target, poweroff.target | 关机 |
1 | runlevel1.target, rescue.target | 单用户模式 |
2 | runlevel2.target, multi-user.target | 等同于级别3 |
3 | runlevel3.target, multi-user.target | 多用户的文本界面 |
4 | runlevel4.target, multi-user.target | 等同于级别3 |
5 | runlevel5.target, graphical.target | 多用户的图形界面 |
6 | runlevel6.target, reboot.target | 重启 |
emergency | emergency.target | 紧急Shell |
将系统默认的运行目标修改为“多用户,无图形”模式,(直接用ln命令把多用户模式目标文件连接到/etc/systemd/system/目录):
[root@michael ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
Systemd简单使用
System V init命令(rhel6) | systemctl命令(rhel7) | 作用 |
---|---|---|
service xxx start | systemctl start xxx.service | 启动服务 |
service xxx restart | systemctl restart xxx.service | 重启服务 |
service xxx stop | systemctl stop xxx.service | 停止服务 |
service xxx reload | systemctl reload xxx.service | 重新加载配置文件(不终止服务) |
service xxx status | systemctl status xxx.service | 查看服务状态 |
chkconfig xxx on | systemctl enable xxx.service | 开机自动启动 |
chkconfig xxx off | systemctl disable xxx.service | 开机不自动启动 |
chkconfig xxx | systemctl is-enabled xxx.service | 查看特定服务是否为开机自启动 |
chkconfig --list | systemctl list-unit-files --type=service | 查看各个级别下服务的启动与禁用情况 |