shell初步了解

第一部分.正则表达式说明

 文件的时间信息 (修改时间)
 atime   访问时间
 mtime   修改时间
 ctime   改变时间(文件属性)
 ​
 date
      find -mtime 按天查找数据
           -mmin  按分钟查找

实例说明:

 [root@localhost opt]# find /opt  -type f -mtime +1  ###从现在开始算,1天以前的所有文件
 [root@localhost opt]# find /opt  -type f -mtime -1  ###昨天到现在已经创建的文件
 /opt/world.sql
 /opt/haha.cnf
 /opt/my.cnf.bak
 /opt/123.txt
 [root@localhost opt]# find /opt/ -type f -mtime 1   ### 昨天一天创建的所有文件
 /opt/world.sql
 /opt/haha.cnf
 /opt/my.cnf.bak
 [root@localhost opt]# find /opt/ -type f -mtime 0   ###昨天现在到今天现在已经创建的文件
 /opt/123.txt

   有疑问???????????????????????????????????????????

查找并批量删除

 
 利用find找出文件之后, 进行批量处理:
    1) 批量删除
    find /oldboy_dir/ -type f -mtime -1|xargs rm -f

批量删除

 [root@localhost opt]# find /opt/ -type f -mtime -1|xargs   ###通过xargs重新排列成一行
 /opt/test/asd.txt /opt/123.txt
 [root@localhost opt]# find /opt/ -type f -mtime -1|xargs rm -rf
 [root@localhost opt]# find /opt/ -type f -mtime -1

标准输入输出

 [root@localhost test]# xargs -n5 <test01.txt
 01 02 03 04 05
 06 07 08 09 10
 [root@localhost test]# xargs -n10 <test01.txt
 01 02 03 04 05 06 07 08 09 10

系统符号信息

  $ 
        a  调取变量信息
        b  区分用户类型  $ 区分普通用户和root用户
        c  结合awk对文件进行取列

$变量

 [root@localhost test]# nsh=123
 [root@localhost test]# echo $nsh
 123

对awk去列

 [root@localhost test]# xargs -n 2 <test01.txt
 01 02
 03 04
 05 06
 07 08
 09 10
 [root@localhost test]# xargs -n 2 <test01.txt  |awk '{print $2}'
 02
 04
 06
 08
 10
 [root@localhost test]# xargs -n 2 <test01.txt  |awk '{print $1}'
 01
 03
 05
 07
 09
 [root@localhost test]# xargs -n 2 <test01.txt  |awk '{print $0}'
 01 02
 03 04
 05 06
 07 08
 09 10

!的作用

  a 强制的作用  wq!
  b 可以实现取反 

[root@localhost test]# cat test02.txt
 aaaaaa
 aaaaaa
 aaaaaa
 bbbbbb
 [root@localhost test]# grep "bbbbbb" test02.txt
 bbbbbb
 [root@localhost test]# grep -v "bbbbbb" test02.txt
 aaaaaa
 aaaaaa
 aaaaaa
 [root@localhost test]# awk '/bbbbbb/' test02.txt
 bbbbbb
 [root@localhost test]# awk '!/bbbbbb/' test02.txt
 aaaaaa
 aaaaaa
 aaaaaa

通过取反来看文件和非文件

 [root@localhost opt]# cd test/
 [root@localhost test]# mkdir page
 [root@localhost test]# ll
 total 16
 drwxr-xr-x. 2 root root  6 Apr 24 05:58 page
 -rw-r--r--. 1 root root 30 Apr 24 05:45 test01.txt
 -rw-r--r--. 1 root root 46 Apr 24 05:52 test02.txt
 -rw-r--r--. 1 root root 10 Apr 24 05:12 test03.txt
 -rw-r--r--. 1 root root 10 Apr 24 05:12 test04.txt
 [root@localhost test]# cd ..
 [root@localhost opt]# find test/ -type f ###查询当前test目录下的文件
 test/test03.txt
 test/test04.txt
 test/test01.txt
 test/test02.txt
 [root@localhost opt]# find test/ ! -type f ###查询test目录下的非文件
 test/
 test/page

!信息  可以快速调取执行历史命令(慎用)

 [root@localhost opt]# find test/ ! -type f
 test/
 test/page
 [root@localhost opt]# !find  ###执行上述命令
 find test/ ! -type f
 test/
 test/page

实现管道功能
     将前一个命令执行的结果交给管道后面的命令进行处理一般管道符号 会经常和xargs命令配合使用批量删除操作

 find /test -type f -name "test*.txt"|xargs rm
 find /test/ -type f -delete
 find /test/ -type f -exec rm -f {} ;

         批量复制

 find /test -type f -name "test*.txt" |xargs -i cp {} /oldgirl/
 find /test -type f -name "test*.txt" |xargs cp -t /oldgirl/
 find /test -type f -name "test*.txt" -exec cp -a {} /oldgirl ;

        批量移动

 find zhangsan/ -name "test*.txt" |xargs mv -t lisi/
 find lisi/ -name "test*.txt" |xargs -i mv {} zhangsan/
 find zhangsan/ -name "test*.txt" -exec  mv {} lisi/ ;

 

双引号,单引号,$符号的区别

 [root@localhost test]# echo '`123`'
 `123`
 [root@localhost test]# echo '`echo 123`'
 `echo 123`
 [root@localhost test]# echo "`echo 123`"
 123
 [root@localhost test]# echo "$(echo 123)"
 123
 ``等价于$()

重定向


  >/1>   标准输出重定向符号
    >>/1>>   标准输出追加重定向符号
    2>     错误输出重定向符号
    2>>    错误输出追加重定向符号 

示例一

 [root@localhost test]# cat >> wangwu<<eof
 > zhangsan
 > zhangsan
 > eof
 [root@localhost test]# cat wangwu
 zhangsan
 zhangsan
 [root@localhost test]# ls
 lisi  wangwu  zhangsan
 [root@localhost test]# cat >>wangwu<<lisi
 > askjjsfs
 > rgjfhgfh
 > cbvfgh
 > lisi
 [root@localhost test]# cat wangwu
 zhangsan
 zhangsan
 askjjsfs
 rgjfhgfh
 cbvfgh

  

示例二

 [root@localhost test]# echo 123>haha
 [root@localhost test]# echo 123 >haha
 [root@localhost test]# cat haha
 123
 [root@localhost test]# ll
 total 8
 -rw-r--r--. 1 root root  4 Apr 24 23:59 haha
 [root@localhost test]# echo sdfjs>>haha
 [root@localhost test]# cat haha
 123
 sdfjs

逻辑符号系列

  &&  前一个命令操作执行成功了,再操作执行后面的命令
  ||  前一个命令操作执行失败了,再操作执行后面的命令
 [root@localhost test]# mkdir /old_dir && echo "create dir sucess" || echo "create dir failed"
        create dir sucess
 [root@localhost test]# mkdi /old_dir && echo "create dir sucess" || echo "create dir failed"
   -bash: mkdi: command not found
        create dir failed

系统通配符号

用于匹配文件名称信息, 便于快速查找文件信息 find

 *  匹配所有信息
 find /oldboy -type f -name "zhangsan*"   ---以zhangsan开头的信息都查询出来
 find /oldboy -type f -name "*zhangsan"   ---以zhangsan开头的信息都查询出来
 find /oldboy -type f -name "ozhangsan*lisi"   
                                             ---以zhangsan 开头的信息
                            以lisi结尾的信息都查询出来 

{} 产生序列信息
   连续序列:

 [root@localhost test]# echo {01..05}
  01 02 03 04 05
 [root@localhost test]# echo {a..z}
  a b c d e f g h i j k l m n o p q r s t u v w x y z
 [root@localhost test]# echo {A..Z}
 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

生成不连续序列
   a 有规律的不连续序列

 [root@localhost test]# echo {01..05..2}
        01 03 05
 [root@localhost test]# echo {a..z..2}
        a c e g i k m o q s u w y

        b 没规律的不连续序列

[root@localhost test]# echo {www,bbs,blog}
    www bbs blog

   生成组合序列

 [root@localhost test]# echo {1,2}{a,b}
    1a 1b 2a 2b
 [root@localhost test]# echo {1,2}{a,b}{A,B}
    1aA 1aB 1bA 1bB 2aA 2aB 2bA 2bB
 ​
    echo A{a,b}
    Aa Ab     
 ​
    A=oldboy.txt 
 ​
    echo oldboy.txt{a,b}   
    oldboy.txta oldboy.txtb     
 ​
 [root@localhost test]# echo A{,b}
    A Ab
    A=oldboy.txt 
    b=.bak
 ​
    cp oldboy.txt{,.bak} == cp oldboy.txt oldboy.txt.bak
  可以快速备份文件命令
 [root@localhost test]# cp oldboy.txt{,.bak}
 [root@localhost test]# ll
    -rw-r--r-- 1 root root 30 Jan 16 11:12 oldboy.txt
    -rw-r--r-- 1 root root 30 Jan 16 11:56 oldboy.txt.bak
  快速还原数据方法
 [root@localhost test]# cp oldboy.txt{.bak,}
    -rw-r--r-- 1 root root 30 Jan 16 12:02 oldboy.txt
    -rw-r--r-- 1 root root 30 Jan 16 11:56 oldboy.txt.bak
 ​
 # echo A{B,}
 ​
    AB A
    oldboy.txt{.bak,} == cp oldboy.txt.bak oldboy.txt

 未完待续..........................

原文地址:https://www.cnblogs.com/nshgo/p/10769131.html