第二节:思维导图学习Shell脚本编程之命令之美

原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dba10g.blog.51cto.com/764602/1607658

wKiom1TDOVrR-McsAAG2tWTFcA4419.jpg





介绍一些常用的命令

cat命令一些用法

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
#cat -s命令压缩空行
  
[root@beijing  test ] # cat catinfo.txt
1
2
  
  
3
4
5
100
[root@beijing  test ] # cat -scatinfo.txt
1
2
  
3
4
5
  
#输出行号
[root@beijing  test ] # ls | cat -n
     1  1.txt
     2  2.txt
     3   cal .sh
     4  catinfo.txt
     5  cmd.sh
     6   date .sh
     7  func.sh
     8  index.sh
     9  menu.sh
    10  output.session
  
#-T: tab键^显示
[root@beijing  test ] # cat catinfo.txt
         public class A{
  
                 System.out.println( "helloshell" );
        }
[root@beijing  test ] # cat -Tcatinfo.txt
^Ipublic class A{
  
^I^ISystem.out.println( "helloshell" );
^I}


终端回放

1
2
3
4
#所谓的终端回放,就是捕捉终端命令历史
#可以使用script命令
script -t 2> timing.log -aoutput.session
#注意需要关闭终端,才会保存timing.log

文件查找

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
#区分大小写
[root@beijing  test ] # find . -name"f*.sh"
. /func .sh
#不区分大小写
[root@beijing  test ] # find . -iname"f*.sh"
. /func .sh
. /FUNC .SH
#基于路径查询(其实就是对路径进行完整匹配,不要考虑什么层级关系,把路径当成字符串处理)
[root@beijing ~] # find /etc -path"etc/*sysconfig/*network"
[root@beijing ~] # find /etc -path"*etc/*sysconfig/*network"
/etc/sysconfig/networking/profiles/default/network
/etc/sysconfig/network
  
#文件类型查找
#查找文件
  [root@beijing  test ] # find -type f
. /out .txt
. /index .sh
. /cal .sh
. /catinfo .txt
. /func .sh
#查找目录
[root@beijing  test ] # find -type d
.
. /aaa
  
  
基于时间的查找(atime:访问时间,ctime:变化时间,mtime:修改时间)。这里的变化指的是“元数据”,比如权限、用户名什么的。
#访问时间超过七天
find  -atime +7
#七天前被访问过的文件
find  -atime 7
  
最近七天访问过的文件
find  -atime -7
  
基于权限
find  -perm
基于大小,支持单位cwbkMG
find  -size
  -size n[cwbkMG]

xargs

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
#多行 换成单行
[root@beijing  test ] # cat xargs.txt
1.txt
2.txt
3.txt
4.txt
  [root@beijing  test ] # cat xargs.txt |xargs
1.txt 2.txt 3.txt 4.txt
  
#单行 换成多行
[root@beijing  test ] # cat xargs1.txt
1       2      3       4       5 6 6 8 9 10
[root@beijing  test ] # cat xargs1.txt|xargs
1 2 3 4 5 6 6 8 9 10
[root@beijing  test ] # cat xargs1.txt|xargs -n 2
1 2
3 4
5 6
6 8
9 10
#指定分界符【其中1,2,3,4之间就是用tab键分割的)
[root@beijing  test ] # cat xargs1.txt|xargs -n 2 -d "\t"
1 2
3 4
5 6 6 8 9 10
  
#测试shell, 仅仅打印参数
[root@beijing  test ] # cat sayparam.sh
#!/bin/bash
  
echo  "$*" #;
  
#模拟参数文件
[root@beijing  test ] # cat argums.txt
param1
param2
param3
  
[root@beijing  test ] # cat argums.txt|xargs -n 2  sh sayparam.sh
param1 param2 #
param3 #
[root@beijing  test ] # cat argums.txt|xargs -n 1  sh sayparam.sh
param1 #
param2 #
param3 #
[root@beijing  test ] # cat argums.txt|xargs   sh sayparam.sh
param1 param2 param3 #

排序

1
2
3
4
5
6
7
8
9
10
#-k 排序键,从1开始
#-r:反序,就是从大到小
[root@beijing  test ] # cat argums.txt| sort -r -k 1
param3
param2
param1
[root@beijing  test ] # cat argums.txt| sort  -k 1
param1
param2
param3

分割文件

1
2
split
可指定生成文件前缀,序号是按数字排列,还是字母排列,分割大小。

案例 批量修改某文件夹文件名称

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
for  file  in  *.txt;  do
          filename=${ file %\.*}   #文件名,非贪婪,从右到左匹配
          extention=${ file ##*.}#获取后缀名,贪婪从左到右匹配
              
          result=$filename\_back.$extention;
          mv $ file  $result 2> /dev/null ;
          if [[ $? - eq  0 ]];  then
                    echo "rename $file success"
          fi
done


本文出自 “简单” 博客,请务必保留此出处http://dba10g.blog.51cto.com/764602/1607658

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值