* 通配符,任意个任意字符 ? 任意一个字符 # 注释字符,在命令或脚本前面写入加#号,就表示这一行不会生效 \ 脱义字符, | 管道符
[root@hf-01 ~]# cat /etc/passwd |head -2 //查看文件的前两行 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@hf-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1 //截取文件前两行中以冒号做为分割符的第一段 root bin [root@hf-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2 root:x bin:x [root@hf-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3 root:x:0 bin:x:1 [root@hf-01 ~]# cat /etc/passwd |head -2 |cut -c 4 //截取两段文件前两行中显示第4个字符 t :
[root@hf-01 ~]# sort /etc/passwd //sort命令,默认按照ASCII码排序 adm:x:3:4:adm:/var/adm:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin [root@hf-01 ~]# head /etc/passwd >> 1.txt //head命令,默认显示前十行 [root@hf-01 ~]# vim 1.txt //并在文件中添加一些字符,特殊符号 [root@hf-01 ~]# sort 1.txt //sort命令,默认按照ASCII码排序 < { 1.txt 222111 22222222222aaaaaa 223333 22aaa 2.txt 47888888gdkgljsd adm:x:3:4:adm:/var/adm:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin *dffadg halt:x:7:0:halt:/sbin:/sbin/halt lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sync:x:5:0:sync:/sbin:/bin/sync [root@hf-01 ~]#
[root@hf-01 ~]# sort -n 1.txt < { adm:x:3:4:adm:/var/adm:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin *dffadg halt:x:7:0:halt:/sbin:/sbin/halt lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 1.txt 2.txt 22aaa 222111 223333 47888888gdkgljsd 22222222222aaaaaa [root@hf-01 ~]#
[root@hf-01 ~]# sort -nr 1.txt 22222222222aaaaaa 47888888gdkgljsd 223333 222111 22aaa 2.txt 1.txt sync:x:5:0:sync:/sbin:/bin/sync operator:x:11:0:operator:/root:/sbin/nologin halt:x:7:0:halt:/sbin:/sbin/halt *dffadg daemon:x:2:2:daemon:/sbin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin { < [root@hf-01 ~]#
[root@hf-01 ~]# wc -l 1.txt //显示文件的行数 22 1.txt [root@hf-01 ~]# wc -m 1.txt //显示文件中的字符数 468 1.txt [root@hf-01 ~]# vim 2.txt //在文件中编写两行,6个字符 [root@hf-01 ~]# wc -m 2.txt //在查看的时候,会显示出8个字符 8 2.txt [root@hf-01 ~]# cat -A 2.txt //会统计全部的字符,包括隐藏字符 123$ avd$ [root@hf-01 ~]# wc -w 2.txt //它是以空格、空白字符进行区分的 2 2.txt
uniq命令去重条件:须要先排序,再去重正则表达式
[root@hf-01 ~]# vim 2.txt [root@hf-01 ~]# cat 2.txt 123 avd 112,21a 123 avd 1 2 1 [root@hf-01 ~]# uniq 2.txt 123 avd 112,21a 123 avd 1 2 1 [root@hf-01 ~]# vim 2.txt [root@hf-01 ~]# cat 2.txt //查看文件内容 123 avd 112,21a 123 avd 1 1 2 [root@hf-01 ~]# uniq 2.txt //会看到在更改排序后,去重了 123 avd 112,21a 123 avd 1 2 [root@hf-01 ~]#
[root@hf-01 ~]# sort 2.txt 1 1 123 123 2 avd avd 112,21a [root@hf-01 ~]# sort 2.txt |uniq 1 123 2 avd avd 112,21a [root@hf-01 ~]#
[root@hf-01 ~]# sort 2.txt |uniq -c 2 1 2 123 1 2 1 avd 1 avd 112,21a [root@hf-01 ~]#
[root@hf-01 ~]# > 2.txt [root@hf-01 ~]# cat 2.txt [root@hf-01 ~]#
[root@hf-01 ~]# cat 3.txt asda 123 fgdg,45 1 abc cda abc 1 [root@hf-01 ~]# sort 3.txt |uniq -c |tee a.txt 2 1 1 123 fgdg,45 2 abc 1 asda 1 cda [root@hf-01 ~]# cat a.txt 2 1 1 123 fgdg,45 2 abc 1 asda 1 cda [root@hf-01 ~]#
[root@hf-01 ~]# sort 3.txt |uniq -c |tee -a a.txt 2 1 1 123 fgdg,45 2 abc 1 asda 1 cda [root@hf-01 ~]# cat a.txt 2 1 1 123 fgdg,45 2 abc 1 asda 1 cda 2 1 1 123 fgdg,45 2 abc 1 asda 1 cda [root@hf-01 ~]#
[root@hf-01 ~]# echo "hanfeng" |tr '[hf]' '[HF]' HanFeng [root@hf-01 ~]# echo "hanfeng" |tr 'h' 'H' Hanfeng [root@hf-01 ~]# echo "hanfeng" |tr '[a-z]' '[A-Z]' HANFENG [root@hf-01 ~]#
[root@hf-01 ~]# echo "hanfeng" |tr '[a-z]' '[1]' //错误示范,这样写会出错 ]1]]]]] [root@hf-01 ~]# echo "hanfeng" |tr '[a-z]' '1' //在替换成数字的时候,须要去除方括号 1111111 [root@hf-01 ~]#
[root@hf-01 ~]# find /etc/ -type f -name "*conf" -exec cat {} >>a.txt \; //将etc目录下全部文件以conf结尾的文件全都输出重定向到a.txt文件中 [root@hf-01 ~]# du -sh a.txt 252K a.txt [root@hf-01 ~]# mv a.txt 111/ //把a.txt文件移动到111目录下 [root@hf-01 ~]# cd 111/ //切换到111目录下 [root@hf-01 111]# ls a.txt [root@hf-01 111]# split -b 1000 a.txt //单位是字节(1000byte=1k) [root@hf-01 111]# ls a.txt xbe xcj xdo xet xfy xhd xii xjn xks xlx xnc xoh xpm xqr xrw xtb xaa xbf xck xdp xeu xfz xhe xij xjo xkt xly xnd xoi xpn xqs xrx xtc xab xbg xcl xdq xev xga xhf xik xjp xku xlz xne xoj xpo xqt xry xtd xac xbh xcm xdr xew xgb xhg xil xjq xkv xma xnf xok xpp xqu xrz xte 等等等,只截取了一小部分 [root@hf-01 111]# du -sh //查看目录下文件大小 2.3M . [root@hf-01 111]# du -sh * //会看到分割出来的都占据了一个块 252K a.txt 4.0K xaa 4.0K xab 4.0K xac 4.0K xad 等等等,只截取了一小部分 [root@hf-01 111]# rm -f x*
在切割一个文件,在不指定任何的文件名,最后显示的切割完的文件,会是已xab,xac这样一直延续下去,若再切割一次,则会zxaaa,zxaab等依次下去shell
[root@hf-01 111]# [root@hf-01 111]# split -b 100k a.txt [root@hf-01 111]# ls a.txt xaa xab xac [root@hf-01 111]# du -sh * 252K a.txt 100K xaa 100K xab 52K xac [root@hf-01 111]# rm -f x* [root@hf-01 111]#
[root@hf-01 111]# split -b 100k a.txt abc [root@hf-01 111]# ls abcaa abcab abcac a.txt [root@hf-01 111]# split -b 100k a.txt abc. [root@hf-01 111]# ls abcaa abc.aa abcab abc.ab abcac abc.ac a.txt [root@hf-01 111]# rm -f abc*
切割的文件默认是以x开头!!!vim
[root@hf-01 111]# split -l 1000 a.txt [root@hf-01 111]# ls -l 总用量 512 -rw-r--r--. 1 root root 256144 11月 18 06:41 a.txt -rw-r--r--. 1 root root 44741 11月 18 06:59 xaa -rw-r--r--. 1 root root 44239 11月 18 06:59 xab -rw-r--r--. 1 root root 44320 11月 18 06:59 xac -rw-r--r--. 1 root root 34153 11月 18 06:59 xad -rw-r--r--. 1 root root 38618 11月 18 06:59 xae -rw-r--r--. 1 root root 34693 11月 18 06:59 xaf -rw-r--r--. 1 root root 15380 11月 18 06:59 xag [root@hf-01 111]# wc -l * 6548 a.txt 1000 xaa 1000 xab 1000 xac 1000 xad 1000 xae 1000 xaf 548 xag 13096 总用量 [root@hf-01 111]#
$ 变量前缀,!$组合,正则里面表示行尾 ;多条命令写到一行,用分号分割 ~ 用户家目录,后面正则表达式表示匹配符 & 放到命令后面,会把命令丢到后台 > 正确重定向 >> 追加剧定向 2> 错误重定向 2>> 错误追加剧定向 &> 正确和错误输出重定向 [ ] 指定字符中的一个,[0-9],[a-zA-Z],[abc] || 和 && ,用于命令之间
[root@hf-01 ~]# [ -d haha ] || mkdir haha [root@hf-01 ~]# ls 111 1_heard.txt 1.txt 2.txt 3.txt haha [root@hf-01 ~]# [ -d haha ] && mkdir haha mkdir: 没法建立目录"haha": 文件已存在 [root@hf-01 ~]#
source exec 区别bash
sort并未按ASCII排序.net