Linux学习笔记9

来源:互联网 发布:steam网络不稳定 编辑:程序博客网 时间:2024/04/29 01:49

Linux学习笔记9

  • Linux学习笔记9
      • history命令
      • 管道符的使用
      • 错误重定向错误追加重定向
      • 变量
      • 系统环境变量与个人环境变量的配置文件
      • Linux Shell中的特殊符号
      • cut命令
      • sort命令
      • wc命令
      • uniq命令
      • tee命令
      • tr命令
      • split命令
      • 对比两个文件区别
      • 多条命令在一行执行
      • 命令放在后台执行
      • 表示连续字符中任意一个
      • 与和或

history命令

history : 显示所有历史命令

history -c : 清空命令记录

history -w : 将当前历史写入历史记录文件中,重写历史文件内容

!! : 执行上一条命令

[root@localhost ~]# ls -lh 123/
总用量 0
-rw-r--r-- 1 root root 0 4月 2 15:00 321.txt
[root@localhost ~]# !!
ls -lh 123/
总用量 0
-rw-r--r-- 1 root root 0 4月 2 15:00 321.txt
[root@localhost ~]#

!n : 执行命令历史中的第n条命令

1013 ls -lh 123/
1014 history
[root@localhost ~]# !1013
ls -lh 123/
总用量 0
-rw-r--r-- 1 root root 0 4月 2 15:00 321.txt
[root@localhost ~]#

!字符串 : 执行历史记录中最近一次以该字符串开头的命令

1015 ls -lh 123/
1016 history
[root@localhost ~]# !ls
ls -lh 123/
总用量 0
-rw-r--r-- 1 root root 0 4月 2 15:00 321.txt
[root@localhost ~]#

!$ : 得到上一个命令的最后一个参数

[root@localhost ~]# echo "123456789" >> 123/321.txt
[root@localhost ~]# ls -lh 123/321.txt
-rw-r--r-- 1 root root 10 4月 3 13:56 123/321.txt
[root@localhost ~]# cat !$
cat 123/321.txt
123456789
[root@localhost ~]#

管道符的使用

| : 将前一个命令的输出作为后一个指令的输入

[root@localhost ~]# cat /etc/passwd | wc -l
48
[root@localhost ~]#

错误重定向,错误追加重定向

2>2>>

[root@localhost ~]# ls a 2> /tmp/1.err
[root@localhost ~]#

PS :该例子是将错误信息重定向到文件中。

变量

http://www.apelearn.com/study_v2/chapter13.html#id2

例:引号的用法

[root@localhost Desktop]# mydate="date is `date +%T`"
[root@localhost Desktop]# echo $mydate
date is 14:52:10
[root@localhost Desktop]# mydate='date is `date +%T`'
[root@localhost Desktop]# echo $mydate
date is `date +%T`
[root@localhost Desktop]#

PS:单引号中的特殊字符全部失效,双引号不会取消特殊字符的作用。反引号可以得到命令运行结果。

PS:用户变量可以加入到用户家目录下的.bashrc文件里面,全局变量可以加入到/etc/profile文件里面,然后用source 文件名来更新。

系统环境变量与个人环境变量的配置文件

/etc/profile : 这个文件预设了几个重要的变量,例如PATH, USER, LOGNAME, MAIL, INPUTRC, HOSTNAME, HISTSIZE, umask等等。

/etc/bashrc : 这个文件主要预设umask以及PS1。这个PS1就是在敲命令时,前面那串字符了。

[root@localhost ~]# echo $PS1

[\u@\h \W]\$

\u 就是用户, \h 主机名, \W 则是当前目录,\$ 就是那个 ‘#’ 了,如果是普通用户则显示为 ‘$’.

除了两个系统级别的配置文件外,每个用户的主目录下还有几个这样的隐藏文件:

.bash_profile :定义了用户的个人化路径与环境变量的文件名称。每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次。

.bashrc :该文件包含专用于你的shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。例可以将用户自定义的alias或者自定义变量写到这个文件中。

.bash_history :记录命令历史用的。

.bash_logout :当退出shell时,会执行该文件。可以把一些清理的工作放到这个文件中。

Linux Shell中的特殊符号

http://www.apelearn.com/study_v2/chapter13.html#linux-shell

cut命令

cut -d '分隔符' [-cf] n

选项:

-d : 分隔符选项

-f : 选择第几块

[root@localhost ~]# head -n3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# cat /etc/passwd | cut -d ':' -f1 | head -n3
root
bin
daemon
[root@localhost ~]#
[root@localhost ~]# cat /etc/passwd | cut -d ':' -f7 | head -n3
/bin/bash
/sbin/nologin
/sbin/nologin

-c : 选择第几个字符

[root@localhost ~]# head -n3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# cat /etc/passwd | cut -c1 | head -n3
r
b
d
[root@localhost ~]# cat /etc/passwd | cut -c1-5 | head -n3
root:
bin:x
daemo
[root@localhost ~]#
[root@localhost ~]# cat /etc/passwd | cut -c1,2,5 | head -n3
ro:
bix
dao
[root@localhost ~]#

PS:-c 后面可以是1个数字n,也可以是一个区间n1-n2,还可以是多个数字n1,n2,n3。

sort命令

sort [-t 分隔符] [-kn1,n2] [-nru] 这里的n1 < n2

选项:

-t 分隔符 :作用跟cut的-d一个意思

-n :使用纯数字排序

-r:反向排序

-u :去重复

-kn1,n2 :由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序

例:以:做分隔符的第三个块,字典序升序排序

[root@localhost ~]# head -n5 /etc/passwd | sort -t : -k3
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost ~]#

PS:如果选项加上-n,则是按数字大小排序

例:以:做分隔符的第三到五个块,字典序降序排序

[root@localhost ~]# head -n5 /etc/passwd | sort -t : -k3,5 -r
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]#

wc命令

用于统计文档的行数、字符数、词数,常用的选项为:
>

-l :统计行数

-m :统计字符数

-w :统计词数

[root@localhost ~]# wc -l test/123.txt
337864 test/123.txt
[root@localhost ~]# wc -m test/123.txt
8090358 test/123.txt
[root@localhost ~]# wc -w test/123.txt
903417 test/123.txt
[root@localhost ~]# wc test/123.txt
337864 903417 10849826 test/123.txt
[root@localhost ~]#

PS:wc 不跟任何选项,直接跟文档,则会把行数、词数、byte数依次输出。

uniq命令

去重复的行

-c :统计重复的行数,并把行数写在前面

例:统计重复行数

[root@localhost ~]# cat test.txt
123
123
321
321
111
111
111
2222
333
456
[root@localhost ~]# sort -n test.txt | uniq -c
3 111
2 123
2 321
1 333
1 456
1 2222
[root@localhost ~]#

PS:使用uniq 的前提是需要先给文件排序,否则不管用。

tee命令

后跟文件名,类似与重定向 “>”, 但是比重定向多了一个功能,在把文件写入后面所跟的文件中的同时,还显示在屏幕上。

[root@localhost ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaaa" |tee testb.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaa
[root@localhost ~]# cat testb.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaa

PS:tee 常用语管道符 “|” 后。

tr命令

替换字符

选项:

-d :删除某个字符,-d 后面跟要删除的字符

-s :把重复的字符去掉

例:小写变大写

[root@localhost ~]# head -n2 /etc/passwd |tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
[root@localhost ~]#

PS:其局限性是只针对单个字符,一一对应替换。

split命令

切割文档

选项:

-b :依据大小来分割文档,单位为byte

[root@localhost ~]# ls -lh test/123/
总用量 11M
-rw-r--r-- 1 root root 11M 4月 3 17:37 123.txt
drwxr-xr-x 2 root root 6 3月 31 17:03 321
[root@localhost ~]# split -b300000 test/123/123.txt
[root@localhost ~]# split -b3000000 test/123/123.txt test/123/new123
[root@localhost ~]# ls -lh test/123/
总用量 21M
-rw-r--r-- 1 root root 11M 4月 3 17:37 123.txt
drwxr-xr-x 2 root root 6 3月 31 17:03 321
-rw-r--r-- 1 root root 2.9M 4月 3 17:44 new123aa
-rw-r--r-- 1 root root 2.9M 4月 3 17:44 new123ab
-rw-r--r-- 1 root root 2.9M 4月 3 17:44 new123ac
-rw-r--r-- 1 root root 1.8M 4月 3 17:44 new123ad
[root@localhost ~]#

PS:如果split不指定目标文件名,则会以xaa xab… 这样的文件名来存取切割后的文件。当然我们也可以指定目标文件名:

-l :依据行数来分割文档

对比两个文件区别

diff test/123.txt test/123.txt

多条命令在一行执行

;

[root@localhost ~]# ls -d test*; touch test111; ls -d test*
test test1 test2 test3 testa testb.txt
test test1 test111 test2 test3 testa testb.txt

命令放在后台执行

& : 如果想把一条命令放到后台执行的话,则需要加上这个符号。通常用于命令运行时间非常长的情况。

[root@localhost ~]# sleep 30 &
[1] 3260
[root@localhost ~]# jobs
[1]+ Running sleep 30 &

表示连续字符中任意一个

[]

[root@localhost ~]# ls -d test*
test test1 test111 test2 test3 testa testb.txt
[root@localhost ~]# ls -d test[1-3]
test1 test2 test3
[root@localhost ~]# ls -d test[1a3]
test1 test3 testa
[root@localhost ~]# ls -d test[0-9]
test1 test2 test3
[root@localhost ~]# ls -d test[0-9a-z]
test1 test2 test3 testa

与和或

command1 ; command2

command1 && command2

command1 || command2

使用 “;” 时,不管command1是否执行成功都会执行command2;

使用 “&&” 时,只有command1执行成功后,command2才会执行,否则command2不执行;

使用 “||” 时,command1执行成功后command2 不执行,否则去执行command2,总之command1和command2总有一条命令会执行。

参考《跟阿铭学Linux》


0 0
原创粉丝点击