linux命令经典用法与配置收录
1 管道或字符处理
1.1 grep的经典用法
1.1.1 扩展查找参数
1
|
grep -E 'vmx|svm' /proc/cpuinfo
|
1.1.2. 过滤注解和空行
1
|
grep -E - v "(^#|^$)" /etc/yum .conf
|
1.1.3 搜索文本的内容
1
|
grep –r "string" .
|
1.2 awk的经典用法
1.2.1 按指定的列打印
1
|
echo './ftp1/abc/efg.txt' | awk -F '/' '{print $1"/"$2"/"$3"/"$4}'
|
1.2.2 去掉最前列打印
1
|
cat /tmp/ftplist | awk -F '/' '{$1="";OFS="/";print}'
|
1.2.3 去掉最后列打印
1
|
echo './ftp1/abc/efg.txt' | awk -F '/' '{NF-=1;OFS="/";print}'
|
1.2.4 去掉前后列打印
1
|
echo './ftp1/abc/efg.txt' | awk -F '/' '{$1="";NF-=1;OFS="/";print}'
|
1.2.5 打印最后一列
1
|
echo './ftp1/abc/efg.txt' | awk -F '/' '{print $NF}'
|
1.3 cut的经典用法
1.3.1 多区域截取
1
|
cat /etc/passwd | cut -d: -f1,2,3
|
1.3.2 按字符截取
1
2
|
cat /etc/passwd | cut -c 1-4,6
echo '测试用例' | cut -c 1-2,4
|
1.3.3 按字节截取
1
2
|
cat /etc/passwd | cut -b 1-4,6
echo '测试用例' | cut -b 1-2,4
|
注:以上中文用例可见,按字符可以保证截取的完整性,按字节不能不能保证。
1.4 sort经典用法
删除重复行
1
|
cat /proc/cpuinfo | grep 'model name' | sort -u
|
1.5 sed经典用法
1.5.1 控制字符转义
1
2
|
echo ' `~!@#$%^&*()_+{}[]|\:;",./<>?' | sed 's/[[:blank:]]\|[&' " '"' ()]/\\& /g '
echo ' `~!@#$%^&*()_+{}[]|\:;",./<>?' | sed 's/[^[:alnum:]]/\\&/g'
|
1.5.2 无歧义的方式输出字符
1
2
3
4
|
echo "test example" > test .txt
echo -e "test\texample" >> test .txt
sed l 'test.txt'
sed -n l 'test.txt'
|
1.5.1 字符串替换
1
|
echo "API_KEY = 0000000" | sed 's/ //g'
|
1.6 字符集转换
1
|
iconv -f utf8 -t gb2312 file -before.txt > result.txt
|
1.7 根据列表批量删除
1
|
cat list.txt | xargs rm -rf
|
2 目录或文件处理
2.1 find的经典用法
1
2
3
4
5
6
7
8
9
10
|
#查找最近30分钟修改的当前目录下的.php文件 find . -name '*.php' -mmin -30
#查找最近24小时修改的当前目录下的.php文件 find . -name '*.php' -mtime 0
#查找最近24小时修改的当前目录下的.php文件,并列出详细信息 find . -name '*.php' -mtime 0 - ls
#查找当前目录下,最近24-48小时修改过的文件。 find . - type f -mtime 1
#查找当前目录下,最近1天前修改过的文件。 find . - type f -mtime +1
|
2.2 tree用法
显示目录层数:
1
|
tree -L 2 |
2.3 当前目录大小
1
|
du -h --max-depth=1
|
2.4 强制卸载目录(慵懒模式)
1
|
umount -l /mnt
|
3 时间相关
3.1 修改时区域时间同步
1
2
3
4
5
|
yum install -y ntpdate
timedatectl list-timezones #时区查看
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #配置时区
timedatectl set -timezone Asia /Shanghai #配置时区
ntpdate 0.centos.pool.ntp.org #时间同步
|
3.2 NTP客户端配置
1
2
|
yum -y install chrony
vim /etc/chrony .conf
|
修改NTP服务器地址:
1
|
server 0.centos.pool.ntp.org iburst |
设置服务开机启动与重启
1
2
|
systemctl enable chronyd
systemcatl restart chronyd |
查看服务状态:
1
2
3
|
timedatectl status #时间日期状态
chronyc sources - v #同步时间源
chronyc sourcestats - v #同步时间状态
|
4 硬件相关
4.1 CPU信息
4.1.1 CPU的型号
1
|
dmidecode -t processor |
或
1
|
echo "cpu " $( cat /proc/cpuinfo | grep 'model name' | sort -u)
|
4.1.2 CPU的核数
1
|
echo "cpu core number:" $( cat /proc/cpuinfo | grep 'core id' | sort -u | wc -l)
|
4.2 内存信息
4.2.1 查询内存相关信息
1
|
dmidecode -t memory |
显示如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# dmidecode 2.12 SMBIOS 2.5 present. Handle 0x0019, DMI type 16, 15 bytes
Physical Memory Array Location: System Board Or Motherboard
Use: System Memory
Error Correction Type: Multi-bit ECC
Maximum Capacity: 8 GB
Error Information Handle: Not Provided
Number Of Devices: 4
Handle 0x001A, DMI type 17, 27 bytes
Memory Device Array Handle: 0x0019
Error Information Handle: No Error
Total Width: 42504 bits
Data Width: 42568 bits
Size: 1024 MB
Form Factor: DIMM
Set: 1
Locator: DIMM 1
Bank Locator: DIMM 1
Type: DDR2
Type Detail: Synchronous
Speed: 667 MHz
Manufacturer: Hyundai Electronics
Serial Number: 04004023
Asset Tag: 41420836
Part Number: 48594D503531325537324350382D59352020
Handle 0x001B, DMI type 17, 27 bytes
Memory Device Array Handle: 0x0019
Error Information Handle: No Error
Total Width: Unknown
Data Width: Unknown
Size: No Module Installed
Form Factor: DIMM
Set: 2
Locator: DIMM 2
Bank Locator: DIMM 2
Type: DDR2
Type Detail: Synchronous
Speed: Unknown
Manufacturer: 48spaces
Serial Number: 01234567
Asset Tag: 01234567
Part Number: 012345678901234567890123456789012345
|
注:size可知内存大小或者有无插内存
4.2.2 物理内存的大小
1
|
echo $( expr $( cat /proc/meminfo | grep MemTotal | awk -F ' ' '{print $2}' ) / 1024 / 1024) "GB"
|
4.3 硬盘
4.3.1 硬盘
1
|
fdisk -l | grep 'Disk /dev'
|
4.3.2 硬盘IO
1
|
iostat -d -k 2 |
4.4 查看ISCSI设备
1
|
lsscsi |
4.5 查看PCI设备
1
|
lspci |
4.5.1 查看HBA卡
1
|
lspci | grep Fibre
|
4.5.2 查看网卡
1
|
lspci | grep Ethernet
|
4.5.3 网卡信息
1
|
ethtool eth0
|
4.6 UUID相关的
1
2
3
|
ls -l /dev/disk/by-uuid
uuidgen | xargs tune2fs /dev/sda1 -U
blkid /dev/sda1
|
4.7 查看接口
1
|
dmidecode -t connector
|
注:可查询主板上的USB接口、网络接口、SATA接口等
4.8 文件系统
1
|
tune2fs -l /dev/sda3
|
如果显示如下,则文件系统需要Init 1模式下修复
1
|
Filesystem state: clean with errors |
5 源相关的配置
5.1 wget下载整个yum源目录
1
|
wget -c -r -np --reject=html,gif,A,D -nH https: //repos .fedorapeople.org /repos/openstack/EOL/openstack-juno/epel-7/
|
5.2 yum源下载
1
|
yum install --downloadonly --downloaddir= /tmp httpd
|
5.3 yum源制作
5.3.1 进入光盘挂载目录
1
|
cd CentOS-6.8-x86_64/
|
目录结构大致如下:
1
|
tree -L 2 |
显示如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
. ├── DVD1 │ ├── CentOS_BuildTag │ ├── EFI │ ├── EULA │ ├── GPL │ ├── images │ ├── isolinux │ ├── Packages │ ├── RELEASE-NOTES-en-US.html │ ├── repodata │ ├── RPM-GPG-KEY-CentOS-6 │ ├── RPM-GPG-KEY-CentOS-Debug-6 │ ├── RPM-GPG-KEY-CentOS-Security-6 │ ├── RPM-GPG-KEY-CentOS-Testing-6 │ └── TRANS.TBL ├── DVD2 │ ├── CentOS_BuildTag │ ├── EULA │ ├── Packages │ ├── RPM-GPG-KEY-CentOS-6 │ ├── RPM-GPG-KEY-CentOS-Debug-6 │ ├── RPM-GPG-KEY-CentOS-Security-6 │ ├── RPM-GPG-KEY-CentOS-Testing-6 │ └── TRANS.TBL [...] 9 directories, 27 files |
5.3.2 生成yum源所需的信息
1
2
|
createrepo . createrepo -g repodata /repomd .xml .
|
6 逻辑结构
6.1 if的使用
6.1.1 判断目录是否存在
1
2
3
|
if [ ! -d /backup ]; then
mkdir /backup
fi |
注:如果检测到目录“/backup”不存在则建立目录“/backup”
6.1.2 判断文件是否存在
1
2
3
|
if [ ! -f /backup/list .txt ]; then
rm -rf /backup/list .txt
fi |
注:如果检测到文件“/backup/list.txt”存在则删除文件“/backup/list.txt”
6.1.3 其他判断
1
|
man bash
|
输入如下命令:
1
|
\CONDITIONAL EXPRESSIONS |
7 测试或检查命令
7.1 IO速度测试
7.1.1 写速度测试
1
2
|
dd if = /dev/zero of= /dev/sda1 bs=8k count=800000
dd if = /dev/zero of= /data/test .iso bs=8k count=80000
|
7.1.2 读速度测试
1
2
|
dd if = /dev/sda1 of= /dev/null bs=8k count=800000
dd if = /data/test .iso of= /dev/null bs=8k count=80000
|
8 邮件相关
8.1 邮件发送
8.1.1 Shell直接处理
1
2
3
|
mail -s "test" admin@cmdschool.org
hello,this is test mail
[ctrl + d] |
8.1.2 管道命令
1
|
echo "hello,this is test mail" | mail -s "test2" admin@cmdschool.org
|
8.1.3 文件导入
1
2
|
echo "hello,this is test mail" > mail.txt
mail -s "test3" admin@cmdschool.org < mail.txt
|
8.1.4 含附件发送
1)安装相关包
1
|
yum install sharutils
|
2)测试发送
1
2
3
|
echo "hello,this is test mail" > mail.txt
tar -cf attachment. tar mail.txt
uuencode attachment. tar attachmentName | mail -s "test4" admin@cmdschool.org < mail.txt
|
9 备份相关
9.1 磁带机控制
9.1.1 安装磁带机控制软件
1
|
yum install mt-st
|
9.1.2 列出磁带机状态
1
2
|
mt -f /dev/st0 status
mt -f /dev/st0 status ; echo $?
|
注:SCSI接口磁带机显示为/dev/st[n-1];ATAPI显示为/dev/ht[n-1]
9.1.3 擦除磁带
1
|
mt -f /dev/st0 erase
|
9.1.4 倒带
1
|
mt -f /dev/st0 rewind
|
9.1.5 拉紧磁带
1
|
mt -f /dev/st0 retension
|
9.2 单机备份操作
9.2.1 列出明细操作
1
|
tar tvf /dev/st0
|
9.2.2 创建数据操作
1
|
tar cvf /dev/st0 backupFileName
|
9.2.3 追加数据操作
1
|
tar rvf /dev/st0 backupFileName
|
9.2.4 提取数据操作
1
|
tar xvf /dev/st0 backupFileName
|
9.3 网络备份
9.4 RSYNC同步备份
https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
https://wiki.archlinux.org/index.php/Rsync_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)
10 bash
10.1 bash快捷方式
10.1.1 编辑命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) Ctrl + b :按字符后移(左向) Alt + f :按单词前移(右向) Alt + b :按单词后移(左向) Ctrl + xx:在命令行首和光标之间移动 Ctrl + u :从光标处删除至命令行首 Ctrl + k :从光标处删除至命令行尾 Ctrl + w :从光标处删除至字首 Alt + d :从光标处删除至字尾 Ctrl + d :删除光标处的字符 Ctrl + h :删除光标前的字符 Ctrl + y :粘贴至光标后 Alt + c :从光标处更改为首字母大写的单词 Alt + u :从光标处更改为全部大写的单词 Alt + l :从光标处更改为全部小写的单词 Ctrl + t :交换光标处和之前的字符 Alt + t :交换光标处和之前的单词 Alt + Backspace:与 Ctrl + w 相同类似,分隔符有些差别 [感谢 rezilla 指正] |
10.1.2 重新执行命令
1
2
3
4
5
|
Ctrl + r:逆向搜索命令历史 Ctrl + g:从历史搜索模式退出 Ctrl + p:历史中的上一条命令 Ctrl + n:历史中的下一条命令 Alt + .:使用上一条命令的最后一个参数 |
10.1.3 控制命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
Ctrl + l:清屏 Ctrl + o:执行当前命令,并选择上一条命令 Ctrl + s:阻止屏幕输出 Ctrl + q:允许屏幕输出 Ctrl + c:终止命令 Ctrl + z:挂起命令 Bang (!) 命令 !!:执行上一条命令 !blah:执行最近的以 blah 开头的命令,如 ! ls
!blah:p:仅打印输出,而不执行 !$:上一条命令的最后一个参数,与 Alt + . 相同 !$:p:打印输出 !$ 的内容 !*:上一条命令的所有参数 !*:p:打印输出 !* 的内容 ^blah:删除上一条命令中的 blah ^blah^foo:将上一条命令中的 blah 替换为 foo ^blah^foo^:将上一条命令中所有的 blah 都替换为 foo |
网友评论