Linux学习记录八-Shell基本命令

覆盖输出  >>追加输出  2>错误内容输出 &>和&>>

env可以输出当前用户下所有的环境变量及自定义的全局变量

bash 建立一个子shell  export 定义的变量在子shell同样有效

pstree 可以查看进程树

set查看所有的变量!

/etc/profile  包含一些用户的环境变量,可以声明全局自定义变量

/etc/bashrc  定义了PS1以及umask

‘可以把命令变成普通字符串,如果用“需要使用\来脱义。

*任意字符   ?单一字符 # 注释字符 | 将前面命令结果交给后面命令来处理

;分割 同时输出   &&与  ||或

[]内的任何单一的字符都会包含 [0-9]包含所有数字 [a-zA-Z][a-Z]所有字母

 

cut命令用来显示行中的指定部分,删除文件中指定字段。cut经常用来显示文件的内容,类似于下的type命令。

说明:该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指 明的文件,将它们的内容输出到标准输出上;其二是连接两个或多个文件,如cut fl f2 > f3将把文件fl和几的内容合并起来,然后通过输出重定向符“>”的作用,将它们放入文件f3中。

当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容。因此,一般用more等命令分屏显示。为了控制滚屏,可以按Ctrl+S键,停止滚屏;按Ctrl+Q键可以恢复滚屏。按Ctrl+C(中断)键可以终止该命令的执行,并且返回Shell提示符状态。

语法

cut(选项)(参数)

选项

-b:仅显示行中指定直接范围的内容;
-c:仅显示行中指定范围的字符;
-d:指定字段的分隔符,默认的字段分隔符为“TAB”;
-f:显示指定字段的内容;
-n:与“-b”选项连用,不分割多字节字符;
--complement:补足被选择的字节、字符或字段;
--out-delimiter=<字段分隔符>:指定输出内容是的字段分割符;
--help:显示指令的帮助信息;
--version:显示指令的版本信息。

参数

文件:指定要进行内容过滤的文件。

实例

例如有一个学生报表信息,包含No、Name、Mark、Percent:

[root@localhost text]# cat test.txt 
No Name Mark Percent
01 tom 69 91
02 jack 71 87
03 alex 68 98

使用 -f 选项提取指定字段:

[root@localhost text]# cut -f 1 test.txt 
No
01
02
03
[root@localhost text]# cut -f2,3 test.txt 
Name Mark
tom 69
jack 71
alex 68

–complement 选项提取指定字段之外的列(打印除了第二列之外的列):

[root@localhost text]# cut -f2 --complement test.txt 
No Mark Percent
01 69 91
02 71 87
03 68 98

使用 -d 选项指定字段分隔符:

[root@localhost text]# cat test2.txt 
No;Name;Mark;Percent
01;tom;69;91
02;jack;71;87
03;alex;68;98
[root@localhost text]# cut -f2 -d";" test2.txt 
Name
tom
jack
alex

指定字段的字符或者字节范围

cut命令可以将一串字符作为列来显示,字符字段的记法:

  • N-:从第N个字节、字符、字段到结尾;
  • N-M:从第N个字节、字符、字段到第M个(包括M在内)字节、字符、字段;
  • -M:从第1个字节、字符、字段到第M个(包括M在内)字节、字符、字段。

上面是记法,结合下面选项将摸个范围的字节、字符指定为字段:

  • -b 表示字节;
  • -c 表示字符;
  • -f 表示定义字段。

示例

[root@localhost text]# cat test.txt 
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

打印第1个到第3个字符:

[root@localhost text]# cut -c1-3 test.txt 
abc
abc
abc
abc
abc

打印前2个字符:

[root@localhost text]# cut -c-2 test.txt 
ab
ab
ab
ab
ab

打印从第5个字符开始到结尾:

[root@localhost text]# cut -c5- test.txt 
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz
efghijklmnopqrstuvwxyz

---------------------------------------------------------------------------------------------------------------

sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出。sort命令既可以从特定的文件,也可以从stdin中获取输入。

语法

sort(选项)(参数)

选项

-b:忽略每行前面开始出的空格字符;
-c:检查文件是否已经按照顺序排序;
-d:排序时,处理英文字母、数字及空格字符外,忽略其他的字符;
-f:排序时,将小写字母视为大写字母;
-i:排序时,除了040至176之间的ASCII字符外,忽略其他的字符;
-m:将几个排序号的文件进行合并;
-M:将前面3个字母依照月份的缩写进行排序;
-n:依照数值的大小排序;
-o<输出文件>:将排序后的结果存入制定的文件;
-r:以相反的顺序来排序;
-t<分隔字符>:指定排序时所用的栏位分隔字符;
+<起始栏位>-<结束栏位>:以指定的栏位来排序,范围由起始栏位到结束栏位的前一栏位。

参数

文件:指定待排序的文件列表。

实例

sort将文件/文本的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。

[root@mail text]# cat sort.txt
aaa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2
eee:50:5.5
eee:50:5.5

[root@mail text]# sort sort.txt
aaa:10:1.1
bbb:20:2.2
ccc:30:3.3
ddd:40:4.4
eee:50:5.5
eee:50:5.5

忽略相同行使用-u选项或者uniq

[root@mail text]# cat sort.txt
aaa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2
eee:50:5.5
eee:50:5.5

[root@mail text]# sort -u sort.txt
aaa:10:1.1
bbb:20:2.2
ccc:30:3.3
ddd:40:4.4
eee:50:5.5

或者

[root@mail text]# uniq sort.txt
aaa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2
eee:50:5.5

sort的-n、-r、-k、-t选项的使用:

[root@mail text]# cat sort.txt
AAA:BB:CC
aaa:30:1.6
ccc:50:3.3
ddd:20:4.2
bbb:10:2.5
eee:40:5.4
eee:60:5.1

#将BB列按照数字从小到大顺序排列:
[root@mail text]# sort -nk 2 -t: sort.txt
AAA:BB:CC
bbb:10:2.5
ddd:20:4.2
aaa:30:1.6
eee:40:5.4
ccc:50:3.3
eee:60:5.1

#将CC列数字从大到小顺序排列:
[root@mail text]# sort -nrk 3 -t: sort.txt
eee:40:5.4
eee:60:5.1
ddd:20:4.2
ccc:50:3.3
bbb:10:2.5
aaa:30:1.6
AAA:BB:CC

# -n是按照数字大小排序,-r是以相反顺序,-k是指定需要爱排序的栏位,-t指定栏位分隔符为冒号

-k选项的具体语法格式:

-k选项的语法格式:

FStart.CStart Modifie,FEnd.CEnd Modifier
-------Start--------,-------End--------
 FStart.CStart 选项  ,  FEnd.CEnd 选项

这个语法格式可以被其中的逗号,分为两大部分,Start部分和End部分。Start部分也由三部分组成,其中的Modifier部分就是我们之前说过的类似n和r的选项部分。我们重点说说Start部分的FStartC.StartC.Start也是可以省略的,省略的话就表示从本域的开头部分开始。FStart.CStart,其中FStart就是表示使用的域,而CStart则表示在FStart域中从第几个字符开始算“排序首字符”。同理,在End部分中,你可以设定FEnd.CEnd,如果你省略.CEnd,则表示结尾到“域尾”,即本域的最后一个字符。或者,如果你将CEnd设定为0(零),也是表示结尾到“域尾”。

从公司英文名称的第二个字母开始进行排序:

$ sort -t ' ' -k 1.2 facebook.txt
baidu 100 5000
sohu 100 4500
google 110 5000
guge 50 3000

使用了-k 1.2,表示对第一个域的第二个字符开始到本域的最后一个字符为止的字符串进行排序。你会发现baidu因为第二个字母是a而名列榜首。sohu和 google第二个字符都是o,但sohu的h在google的o前面,所以两者分别排在第二和第三。guge只能屈居第四了。

只针对公司英文名称的第二个字母进行排序,如果相同的按照员工工资进行降序排序:

$ sort -t ' ' -k 1.2,1.2 -nrk 3,3 facebook.txt
baidu 100 5000
google 110 5000
sohu 100 4500
guge 50 3000

———————————————————————————————————————————————————————————-

uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用。

语法

uniq(选项)(参数)

选项

-c或——count:在每列旁边显示该行重复出现的次数;
-d或--repeated:仅显示重复出现的行列;
-f<栏位>或--skip-fields=<栏位>:忽略比较指定的栏位;
-s<字符位置>或--skip-chars=<字符位置>:忽略比较指定的字符;
-u或——unique:仅显示出一次的行列;
-w<字符位置>或--check-chars=<字符位置>:指定要比较的字符。

参数

  • 输入文件:指定要去除的重复行文件。如果不指定此项,则从标准读取数据;
  • 输出文件:指定要去除重复行后的内容要写入的输出文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。

实例

删除重复行:

uniq file.txt
sort file.txt | uniq
sort -u file.txt

只显示单一行:

uniq -u file.txt
sort file.txt | uniq -u

统计各行在文件中出现的次数:

sort file.txt | uniq -c

在文件中找出重复的行:

--------------------------------------------------------------------------------------------------------------------------------

tee命令用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin。简单的说就是把数据重定向到给定文件和屏幕上。

存在缓存机制,每1024个字节将输出一次。若从管道接收输入数据,应该是缓冲区满,才将数据转存到指定的文件中。若文件内容不到1024个字节,则接收完从标准输入设备读入的数据后,将刷新一次缓冲区,并转存数据到指定文件。

语法

tee(选项)(参数)

选项

-a:向文件中重定向时使用追加模式;
-i:忽略中断(interrupt)信号。

参数

文件:指定输出重定向的文件。

在终端打印stdout同时重定向到文件中:

ls | tee out.txt
1.sh
1.txt
2.txt
eee.tst
EEE.tst
one
out.txt
string2
www.pdf
WWW.pdf
WWW.pef
[root@localhost text]# ls | tee out.txt | cat -n
     1  1.sh
     2  1.txt
     3  2.txt
     4  eee.tst
     5  EEE.tst
     6  one
     7  out.txt
     8  string2
     9  www.pdf
    10  WWW.pdf
    11  WWW.pef

 

—————————————————————————————————————————————————————————————————-

wc命令用来计算数字。利用wc指令我们可以计算文件的Byte数、字数或是列数,若不指定文件名称,或是所给予的文件名为“-”,则wc指令会从标准输入设备读取数据。

语法

wc(选项)(参数)

选项

-c或--bytes或——chars:只显示Bytes数;
-l或——lines:只显示列数;
-w或——words:只显示字数。

参数

文件:需要统计的文件列表。

———————————————————————————————————————————————————

 

split命令可以将一个大文件分割成很多个小文件,有时需要将文件分割成更小的片段,比如为提高可读性,生成日志等。

选项

-b:值为每一输出档案的大小,单位为 byte。
-C:每一输出档中,单行的最大 byte 数。
-d:使用数字作为后缀。
-l:值为每一输出档的列数大小。

实例

生成一个大小为100KB的测试文件:

[root@localhost split]# dd if=/dev/zero bs=100k count=1 of=date.file
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.00043 seconds, 238 MB/s

使用split命令将上面创建的date.file文件分割成大小为10KB的小文件:

[root@localhost split]# split -b 10k date.file 
[root@localhost split]# ls
date.file  xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj

文件被分割成多个带有字母的后缀文件,如果想用数字后缀可使用-d参数,同时可以使用-a length来指定后缀的长度:

[root@localhost split]# split -b 10k date.file -d -a 3
[root@localhost split]# ls
date.file  x000  x001  x002  x003  x004  x005  x006  x007  x008  x009

为分割后的文件指定文件名的前缀:

[root@localhost split]# split -b 10k date.file -d -a 3 split_file
[root@localhost split]# ls
date.file  split_file000  split_file001  split_file002  split_file003  split_file004  split_file005  split_file006  split_file007  split_file008  split_file009

使用-l选项根据文件的行数来分割文件,例如把文件分割成每个包含10行的小文件:


					
分享到:

120 条评论

昵称
  1. Wow that was odd. I just wrote an really long comment but after I
    clicked submit my comment didn’t appear. Grrrr… well I’m not writing
    all that over again. Anyhow, just wanted to say wonderful blog!

  2. I savor, result in I found exactly what I used to be looking for.
    You’ve ended my four day long hunt! God Bless you man. Have a nice day.
    Bye

  3. May I just say what a relief to discover someone that really understands what they’re discussing on the net.

    You actually realize how to bring an issue to light and make it important.
    A lot more people need to check this out and understand this side of
    your story. I was surprised that you are not more popular because
    you definitely have the gift.

  4. This is a topic that’s near to my heart… Best wishes!
    Where are your contact details though?

  5. Wow, that’s what I was seeking for, what a information! existing
    here at this blog, thanks admin of this site.

  6. Hello! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m undoubtedly enjoying your blog and look forward to new posts.

  7. That is a great tip especially to those fresh to the blogosphere.
    Short but very precise info… Many thanks for sharing this
    one. A must read post!

  8. minecraft games

    With havin so much content and articles do you ever run into any problems of plagorism or copyright
    infringement? My blog has a lot of exclusive content I’ve either created myself or outsourced
    but it looks like a lot of it is popping it up all over the internet without my permission. Do you know any solutions
    to help reduce content from being stolen? I’d really appreciate it.

  9. minecraft games

    Ahaa, its nice discussion about this piece of writing at this place
    at this website, I have read all that, so now me also commenting here.

  10. minecraft games

    I know this web site gives quality depending articles and other data,
    is there any other site which presents these stuff in quality?

  11. minecraft games

    I have read so many posts about the blogger lovers but this post is really
    a good post, keep it up.

  12. minecraft games

    This is a topic that’s close to my heart… Many thanks!

    Exactly where are your contact details though?

  13. minecraft games

    Hello! This is kind of off topic but I need some help from an established blog.
    Is it hard to set up your own blog? I’m not very techincal
    but I can figure things out pretty fast. I’m thinking about setting up my own but I’m not sure where to begin. Do you have any points or suggestions?
    With thanks

  14. minecraft games

    Hey! Quick question that’s completely off topic.
    Do you know how to make your site mobile friendly?

    My site looks weird when viewing from my apple iphone.
    I’m trying to find a template or plugin that might
    be able to correct this problem. If you have any suggestions, please share.
    Appreciate it!

  15. minecraft games

    Hey there just wanted to give you a quick heads up.
    The text in your article seem to be running off the screen in Ie.

    I’m not sure if this is a formatting issue or something to do
    with web browser compatibility but I figured I’d post to let you know.
    The design and style look great though! Hope you get the issue solved soon. Thanks

  16. minecraft games

    What’s up it’s me, I am also visiting this web page regularly, this web site is
    actually pleasant and the users are in fact sharing nice thoughts.

  17. minecraft games

    Wonderful beat ! I would like to apprentice even as you amend your
    site, how could i subscribe for a weblog site? The account
    helped me a acceptable deal. I were a little bit familiar
    of this your broadcast offered vibrant clear concept

  18. This is the perfect web site for anybody who wants
    to find out about this topic. You realize a whole lot its almost tough to argue with you (not that I really would want
    to…HaHa). You certainly put a brand new spin on a topic which has been written about for a long time.
    Great stuff, just great!

  19. Amazing! Its really remarkable piece of writing, I have got much clear idea concerning from this article.

  20. Good answers in return of this difficulty with firm arguments and telling everything concerning that.

  21. WOW just what I was looking for. Came here by searching for
    quest bars cheap

  22. I’m really inspired along with your writing talents as neatly as with
    the structure to your blog. Is this a paid subject matter or did you modify it yourself?

    Anyway stay up the excellent high quality writing,
    it’s rare to look a great weblog like this one nowadays..

  23. I am really impressed together with your writing skills as neatly as with the
    structure in your blog. Is that this a paid subject matter or
    did you modify it yourself? Either way stay up the excellent quality writing, it’s uncommon to
    see a great weblog like this one nowadays..

  24. Hi there, You have done an incredible job.
    I will definitely digg it and personally suggest to my friends.

    I’m confident they’ll be benefited from this site.

  25. Spot on with this write-up, I actually think this amazing site needs much more attention.
    I’ll probably be returning to read through more, thanks for the information!

  26. Someone essentially lend a hand to make significantly posts
    I would state. This is the very first time I frequented your web page and up to now?
    I surprised with the research you made to create this actual publish extraordinary.
    Great job!

  27. Nice post. I learn something new and challenging on websites I stumbleupon everyday.
    It will always be exciting to read through articles
    from other writers and practice something from their
    websites.

  28. you are truly a just right webmaster. The web site loading speed is amazing.
    It kind of feels that you’re doing any unique trick. Moreover,
    The contents are masterpiece. you’ve done a fantastic activity on this topic!

  29. minecraft games

    Having read this I thought it was rather informative.
    I appreciate you taking the time and effort to put
    this content together. I once again find myself personally spending a lot
    of time both reading and posting comments.
    But so what, it was still worthwhile!

  30. plenty of fish dating site

    you’re in point of fact a just right webmaster.
    The web site loading velocity is incredible.
    It sort of feels that you’re doing any distinctive trick.
    Moreover, The contents are masterpiece. you’ve done a
    excellent task on this matter!