[root@ming home]# cp /etc/passwd{,.bak} 备份/etc/passwd
----------------------------------------------------------------------------------------------------------
[root@ming home]# ll /etc/passwd* 查看
----------------------------------------------------------------------------------------------------------
-rw-r--r--. 1 root root 1670 May 1 06:43 /etc/passwd
-rw-r--r--. 1 root root 1615 May 1 03:43 /etc/passwd-
-rw-r--r--. 1 root root 1670 May 3 03:07 /etc/passwd.bak
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# ls /etc/ | head -4 查看/etc/ 目录的前4行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# tail anaconda-ks.cfg 读取anaconda文件的后十行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# tail -f anaconda 实时查看anaconda的动向,比如文件的内容增加减少
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# echo abc >> anaconda 将字母abc追加到anaconda文件中,
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# tail -n0 -f anaconda 实时查看anaconda文件的最新动向 动态 只显示最新状态
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# tail -n0 -f anaconda-ks.cfg & 放入后台 在后太进行执行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# cat -n install.log |head -1000 查看install.log 文件的前1000行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# cat -n install.log |head -1000 |tail -n1 只显示第1000行的内容
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# ifconfig |cut -c21-34 |head -2 |tail -n1 取出第二行21-34个字节
----------------------------------------------------------------------------------------------------------
172.16.250.154
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# netstat -nt 查看有多少人在连接
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# cd /var/log/httpd/
----------------------------------------------------------------------------------------------------------
[root@localhost httpd]# ll access_log
----------------------------------------------------------------------------------------------------------
-rw-r--r--. 1 root root 0 5月 3 22:00 access_log 外围服务器的访问日志
----------------------------------------------------------------------------------------------------------
[root@localhost httpd]# iptables -F 清空防火墙
----------------------------------------------------------------------------------------------------------
[root@localhost httpd]# cd /var/log/httpd/
----------------------------------------------------------------------------------------------------------
[root@localhost httpd]# cat access_log > /var/www/html/index.html
----------------------------------------------------------------------------------------------------------
制作一个网站 内容显示为access_log文件里的内容
----------------------------------------------------------------------------------------------------------
[root@localhost httpd]# cut -d" " -f1 access_log 取出第1例
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
[root@localhost httpd]# sed -n '2p' access_log 取出access_log文件的第二行
----------------------------------------------------------------------------------------------------------
[root@localhost httpd]# cut -d" " -f 1 /var/log/httpd/access_log |sort 排序sort
----------------------------------------------------------------------------------------------------------
[root@localhost httpd]# cut -d" " -f 1 /var/log/httpd/access_log |sort |uniq -c|sort -nr
----------------------------------------------------------------------------------------------------------
86 172.16.253.141
23 172.16.253.109
2 172.16.250.44
[root@localhost Packages]# rpm -ivh nmap* *代表任意字符
-rw-r--r--. 1 root root 57 5月 4 00:29 q2
-rw-r--r--. 1 root root 51 5月 4 00:10 q3
-rw-r--r--. 1 root root 45 5月 4 00:35 q5 比较重要
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# uniq -d q2 只显示重复的行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# uniq -u q2 显示不重复的行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# last 显示曾经有多少人登陆过你的机器
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# last|cut -d" " -f1 |sort |uniq -c 查看谁登陆的最多
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# diff q2 q3 比较两个文件的不同
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# diff -u q2 q3 更详细的查看两个文件的不同
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# diff q2 q3 > q5 将q2 q3重定向到q5文件里
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# rm -r q3 删除q3
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# patch -b q2 q5 通过q2 q5 的相互比较来恢复被删除的q3
----------------------------------------------------------------------------------------------------------
patching file q2
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# ll
----------------------------------------------------------------------------------------------------------
-rw-r--r--. 1 root root 51 5月 4 00:37 q2 原来的q3变成q2
-rw-r--r--. 1 root root 57 5月 4 00:29 q2.orig 原来的q2变成q3
-rw-r--r--. 1 root root 45 5月 4 00:35 q5
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep dfdf grep是一个搜索效率比较高的命令 在一些大的文件里
----------------------------------------------------------------------------------------------------------
sdfdfg 此处dfdf字母会高亮
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep com qw2 搜索qw2文件中含有com的那一行,在下面显示并高亮 |在6.6镜像里不会高亮
-------------------------------------------------------------------------------------- 但会搜索
@compat-libraries com会高亮 |
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -v root qw2 搜索qw2文件但不显示root字符
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -e root -e ming qw2 搜索包含root 或者ming的字母
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -n root qw2 搜索包含root并加行号
----------------------------------------------------------------------------------------------------------
27:rootpw --iscrypted
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -i ROOT qw2 搜索包含ROOT的行,并忽略字符的大小写
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -c root qw2 统计匹配的行号
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -o root qw2 仅显示匹配的字符串
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -q root qw2 不输出任何信息
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# echo $? 查看上一条命令是否执行成功
----------------------------------------------------------------------------------------------------------
0 成功显示0不成功显示1
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -A3n root qw2 显示root后续的三行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -b3 roo qw2 显示root前三行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# grep -nC3 root qw2 显示前三行和后三行
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# nmap -sP -v 172.16.250.209/24 扫描 很有意思000000000000000000000000000000000000
----------------------------------------------------------------------------------------------------------
[root@ming ~]# nmap -v -A 172.16.0.1
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# m=ming
[root@localhost ~]# echo $mqw
[root@localhost ~]# echo "$m"qw
mingqw
[root@localhost ~]#
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# pstree -p 查看进程树
----------------------------------------------------------------------------------------------------------
[root@localhost ~]# echo $$ 查看在那个进程里
----------------------------------------------------------------------------------------------------------
#!/bin/bash
name="par"
echo "current pid is $$" 当前我的pid是$$
echo "par.sh:name=$name" 在par.sh脚本中name的值等于$name
sleep 1 睡眠1秒不那么快执行结束
----------------------------------------------------------------------------------------------------------
[root@ming mmm]# vim sh.sh
#!/bin/bash
echo "The script name is $0" 到了0是脚本的名字
echo "1st arg is $1"
echo "2st arg is $2"
echo "3st arg is $3"
set -- 把后面的清空 3后面的清空不包括3
echo "4st arg is $4"
echo "5st arg is $5"
----------------------------------------------------------------------------------------------------------
[root@ming mmm]# bash sh.sh 1 2 3 4 5 6 7 8 9 10
----------------------------------------------------------------------------------------------------------
The script name is sh.sh
1st arg is 1
2st arg is 2j
3st arg is 3
4st arg is
5st arg is
----------------------------------------------------------------------------------------------------------
[root@ming mmm]# vim ew.sh |[root@ming mmm]# bash ew.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/bin/bash |
echo 1all arg are $* |1all arg are 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $*是匹配所有{1..20}
echo 2the number is $# |2the number is 20 $#只显示一个数字
shift | shift减去一个
echo 3all arg are $* |3all arg are 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $*是匹配所有{2..20}
echo 4the number is $# |4the number is 19 $#只显示一个数字 上方减一个数字所以是19
shift | shift减去一个
echo 5all arg are $* |5all arg are 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $*是匹配所有{3..20}
echo 6the number is $# |6the number is 18 $#只显示一个数字 19-1=18
shift 2 | shift减去两个
echo 7all arg are $* |7all arg are 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $*是匹配所有{4..20}
echo 8the number is $# |8the number is 16 $#只显示一个数字18-2=16
shift 20 | shift减去20个
echo 9result is $? |9result is 1 输出的结果 减完了没了所以显示为1 1为错误的0为对的
上方减去了24 个数字 而我只有20个所以显示为错误1而非是0
----------------------------------------------------------------------------------------------------------
[root@ming mmm]# vim sh.sh [root@ming mmm]# vim ww.sh
#!/bin/bash #!/bin/bash
echo haode "$*"
bash ew.sh "$@"
查看只读变量:在脚本代码调用中,通过命令行传递给脚本的参数
$1 , $2 .....:对应第1,第2等参数,shift[n]换位置
$0: 命令本身
$*:传递给脚本的所有参数,全部参数合为一个字符串, 1 2 3 把1 2 3 当成一个数字了
$@:传递给脚本的所有参数,每个参数为独立的字符串, 1 2 3 是三个数字
$@:传递给脚本的参数的个数
$@ $* 只有被双引号包起来的时候才会有差异,不同
set -- 清空所有位置变量
----------------------------------------------------------------------------------------------------------
[root@ming ~]# scp mmm/sh.sh 172.16.251.62:/ 向另一个虚拟机发送文件
----------------------------------------------------------------------------------------------------------
[root@ming mmm]# vim sh.sh
----------------------------------------------------------------------------------------------------------
#!/bin/bash
----------------------------------------------------------------------------------------------------------
#Description/lremote scp
----------------------------------------------------------------------------------------------------------
scp $* ming@172.16.253.244:/home/ming/bin 可以将root下的文件复制到ming用户里
----------------------------------------------------------------------------------------------------------
echo copy is finished.
----------------------------------------------------------------------------------------------------------
[root@ming mmm]# ./sh.sh /etc/* 复制必须是./sh.sh 后面可以随便
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
[root@ming mmm]# vim jiao.bei.sheng.cheng
#!/bin/bash
echo "#!/bin/bash
#description: Display information about this system!
#version:1.0.0
#Author:ming
#Date: `date "+%F %T"` 通过echo将内容输入新创建的文件中
" > $1
vim $1
[root@ming mmm]# chmod +x jiao.bei.sheng.cheng 给脚本增加权限
----------------------------------------------------------------------------------------------------------
[root@ming mmm]# ./jiao.bei.sheng.cheng 123 脚本前面要加./ 123是需要创建的文件 然后按回车
----------------------------------------------------------------------------------------------------------
#!/bin/bash
#description: Display information about this system!
#version:1.0.0
#Author:ming
#Date: 2017-05-08 04:04:04
----------------------------------------------------------------------------------------------------------
[root@ming]# x=100
----------------------------------------------------------------------------------------------------------
[root@ming]# y=200
----------------------------------------------------------------------------------------------------------
[root@ming]# z=$x+$y
----------------------------------------------------------------------------------------------------------
[root@ming]# echo $z echo不会算出它的结果
----------------------------------------------------------------------------------------------------------
100+200
----------------------------------------------------------------------------------------------------------
[root@ming]# let z=$x+$y
----------------------------------------------------------------------------------------------------------
[root@ming]# ehco $z
----------------------------------------------------------------------------------------------------------
300
----------------------------------------------------------------------------------------------------------
[root@ming]# let z=x+y
----------------------------------------------------------------------------------------------------------
[root@ming]# echo $z
----------------------------------------------------------------------------------------------------------
300
----------------------------------------------------------------------------------------------------------
[root@ming]# z=$[x*y] [root@ming]# z=$[$z*$y] [root@ming]# z=$[z*y]
----------------------------------------------------------------------------------------------------------
[root@ming]# echo $z [root@ming]# echo $z [root@ming]# echo $z
----------------------------------------------------------------------------------------------------------
20000 2000000 200000000
----------------------------------------------------------------------------------------------------------
[root@ming]# expr
----------------------------------------------------------------------------------------------------------