Linux基础命令(三)

  • Linux基础命令(三)已关闭评论
  • 174 次浏览
  • A+
所属分类:linux技术
摘要

语法:grep [option…] ‘关键字’ file
根据模式搜索文本,并将符合模式的文本显示出来


Linux基础命令(三)

1.Linux文件命名规则

  • 长度不能超过255个字符
  • 不能使用/当文件名
  • 严格区分大小写

2.文本查找——grep

语法:grep [option...] '关键字' file
根据模式搜索文本,并将符合模式的文本显示出来

[root@zzd ~]# grep zzd greptest 		//过滤greptest文件里zzd关键字 hello zzd 

-i 忽略大小写

[root@zzd ~]# grep -i zzd greptest 		//过滤greptes文件里zzd关键字,忽略大小写 hello zzd heLLo Zzd 

--color 将匹配到的内容高亮显示(color命令默认的命令别名带有此选项)

[root@zzd ~]# which grep		//查看命令grep的绝对路径 alias grep='grep --color=auto' 	/usr/bin/grep 

-v 显示没有被模式匹配到的行

[root@zzd ~]# grep -v zzd greptest 		//过滤greptes文件里没有zzd关键字的行 Hello Word! Hello ZIC Hello zic Hello tgg heLLo Zzd 

-o 只显示被匹配到的字符串

[root@zzd ~]# grep -o zic greptest 		//只显示匹配到的字符串zic zic 

-E 使用正则表达式。相当于egrep

[root@zzd ~]# grep -E 'hello|zic' greptest 		//使用正则表达式匹配有hello和zic的行 hello zzd Hello zic  [root@zzd ~]# grep 'hello|zic' greptest 		//也是正则表达式的用法,不加-E hello zzd Hello zic 

-q 静默输出,不输出任何信息

[root@zzd ~]# grep  Hello greptest  Hello Word! Hello ZIC Hello zic Hello tgg [root@zzd ~]# grep -q Hello greptest 		//静默输出,一般用于shell脚本语言 [root@zzd ~]# 

-A 1 显示被匹配到的内容以及前一行,如果把1换成2,就表示前两行

[root@zzd ~]# grep -A 1 tgg greptest 		//过滤关键字tgg,并显示其以及其后一行 Hello tgg heLLo Zzd 

-B 1 显示被匹配的内容以及后一行,如果把1换成2,就表示后两行

[root@zzd ~]# grep -B 1 tgg greptest 		//过滤关键字tgg,并显示其以及其前一行 Hello zic Hello tgg 

-C 1 显示被匹配到的内容以及前后各一行,如果把1换成2则表示前后各两行

[root@zzd ~]# grep -C 1 tgg greptest 		//过滤关键字tgg,并显示其以及其前后各一行 Hello zic Hello tgg heLLo Zzd 

fgrep 不支持正则表达式,执行速度快,但一般很少用到

egrep 使用正则表达式,和grep -E一个效果

3.文件查找——find

实时查找,精确性强,遍历指定目录中的所有文件完成查找
语法: find [选项...] 查找路径 查找标准 查找到以后的处理动作
查找路径:默认为当前目录
查找标准:默认为指定路径下的所有文件

3.1 查找标准

-name 'filename' 对文件名做精确查找,支持通配符

[root@zzd ~]# find -name greptest		//在当前目录查找文件名为greptest的文件 ./greptest 

-iname 'filename' 文件名匹配时不区分大小写

[root@zzd ~]# find -iname abc		//在当前目录查找文件名为abc的目录,忽略大小写 ./ABC ./abc ./a/ABC ./a/Abc ./a/ABc ./a/abc [root@zzd ~]# find ./a -iname 'ab*'		//使用通配符查找/root/a目录下以ab开头的文件 ./a/ABC ./a/Abc ./a/ABc ./a/abc 

-user username 根据属主来查找

[root@zzd ~]# useradd zzd		//创建一个普通用户zzd [root@zzd ~]# tail -1 /etc/passwd zzd:x:1001:1001::/home/zzd:/bin/bash		#UID和GID都为1001 [root@zzd ~]# chown zzd:zzd greptest 		//修改文件greptest的属主和属组都为zzd [root@zzd ~]# ll greptest  -rw-r--r--. 1 zzd zzd 62 Jul  4 22:57 greptest [root@zzd ~]# find -user zzd 		//查找属主为zzd的文件 ./greptest 

-group groupname 根据属组来查找

[root@zzd ~]# find -group zzd 		//查找属组为zzd的文件 ./greptest 

-uid 根据UID进行查找,当用户被删除后文件的属主会变成此用户的UID

[root@zzd ~]# find -uid 1001 		//查找属主uid为1001的文件 ./greptest 

-gid 根据GIDj进行查找,当用户或组被删除后文件的属组会变成此用户或组的GID

[root@zzd ~]# find -gid 1001 		//查找属组gid为1001的文件 ./greptest 

-nouser 查找没有属主的文件,用户被删除的情况下产生的文件,只有uid没有属主

[root@zzd ~]# userdel -r zzd		//删除用户zzd [root@zzd ~]# find -nouser			//查找只有uid没有属主的文件 ./greptest 

-nogroup 查找没有属组的文件,组被删除的情况下产生的文件,只有gid没有属组

[root@zzd ~]# find -nogroup			//查找只有gid没有属组的文件 ./greptest 

-type 根据文件类型来查找(f , d , c , l , p , s)

[root@zzd ~]# find -type f		//查找当前目录下所有文件 ./.bash_logout ./.bash_profile ./.cshrc ./.tcshrc ./.bash_history ./002 ./qwer ./ABC ./.viminfo ./greptest ./.bashrc ./anaconda-ks.cfg ./passwd ./abc ./.lesshst ./a/ABC ./a/Abc ./a/ABc ./a/abc 

-size 根据文件大小来查找,如1k,1M,+10k,+10M,-1k,-1M

[root@zzd ~]# find / -size 15M 			//查找根目录下大小为15M的文件 find: ‘/proc/1465/task/1465/fd/6’: No such file or directory find: ‘/proc/1465/task/1465/fdinfo/6’: No such file or directory find: ‘/proc/1465/fd/5’: No such file or directory find: ‘/proc/1465/fdinfo/5’: No such file or directory /mnt/AppStream/Packages/bcc-0.16.0-1.el8.x86_64.rpm /mnt/AppStream/Packages/gimp-2.8.22-15.module_el8.0.0+36+bb6a76a2.x86_64.rpm /mnt/AppStream/Packages/golang-race-1.15.2-1.module_el8.4.0+546+c69f460d.x86_64.rpm /mnt/AppStream/Packages/libkkc-data-0.2.7-12.el8.x86_64.rpm /mnt/AppStream/Packages/python2-scipy-1.0.0-20.module_el8.4.0+543+a968257c.x86_64.rpm /mnt/AppStream/Packages/python3-scipy-1.0.0-20.module_el8.3.0+389+6a62c88d.x86_64.rpm /mnt/AppStream/Packages/texlive-lm-20180414-19.el8.noarch.rpm /mnt/AppStream/Packages/texlive-oberdiek-20180414-19.el8.noarch.rpm /mnt/AppStream/Packages/wireshark-cli-2.6.2-12.el8.i686.rpm   [root@zzd ~]# find /etc/ -size -1M		//查找/etc目录下大小1M及以内的文件,+1M则是大于1M的文件 /etc/crypttab /etc/security/opasswd /etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem /etc/environment /etc/exports /etc/motd /etc/sysconfig/run-parts /etc/cron.deny /etc/kernel/install.d/20-grubby.install /etc/kernel/install.d/90-loaderentry.install /etc/selinux/targeted/contexts/files/file_contexts.local /etc/selinux/targeted/contexts/files/file_contexts.subs /etc/.pwd.lock /etc/tuned/post_loaded_profile 

-mtime 根据修改时间来进行查找(单位:天)
ctime 根据改变时间来进行查找(单位:天)
atime 根据访问时间进行查找单(位:天)
+5 五天前
-5 五天以内

[root@zzd ~]# find -mtime -5		//查找当前目录下修改时间在五天以内的文件或目录 . ./.bash_history ./002 ./qwer ./ABC ./.viminfo ./greptest ./.bashrc ./anaconda-ks.cfg ./passwd ./abc ./.lesshst ./test ./test/test1 ./test/test2 ./a ./a/ABC ./a/Abc ./a/ABc ./a/abc  [root@zzd ~]# find -mtime +5		//查找当前目录下修改时间在五天前的文件或目录 ./.bash_logout ./.bash_profile ./.cshrc ./.tcshrc 

-mmin 根据修改时间来进行查找(单位:分钟)
cmin 根据改变时间来进行查找(单位:分钟)
amin 根据访问时间进行查找单(位:分钟)
+5 五分钟前
-5 五分钟以内

[root@zzd ~]# touch passwd  [root@zzd ~]# touch anaconda-ks.cfg  [root@zzd ~]# touch greptest		 #对以上三个文件进行更新  [root@zzd ~]# find -mmin -5		//查找当前目录下修改时间在5分钟以内的文件货目录 ./greptest ./anaconda-ks.cfg ./passwd 

-perm mode 根据权限精确查找
-perm -mode 文件权限能完全包含此mode是才符合条件
-perm /mode 9位权限中有任何一位权限匹配都视为符合查找条件

[root@zzd ~]# find -perm 777		//查找当前目录下权限为777的文件或目录 ./date.sh  [root@zzd ~]# find -perm -755		//查找当前目录下权限能够完全包含755的文件或目录 ./test		 ./test/test1 ./test/test2 ./a ./date.sh  [root@zzd ~]# find /root/a -perm /777	//查找/root/a目录下9为权限权限中有一位符合777九位权限中一位的文件或目录 /root/a /root/a/ABC /root/a/Abc /root/a/ABc /root/a/abc 

3.2 处理动作

-print 显示

[root@zzd ~]# find -name greptest -print		//在当前目录查找文件名为greptest的文件,并显示 ./greptest 

-ls 类似于ls -l的形式显示每一个查找到的文件的详细信息

[root@zzd ~]# find -name greptest -ls		//在当前目录查找文件名为greptest的文件,并以长格式显示文件详细信息  33820697      4 -rw-r--r--   1  1001     1001           62 Jul  4 23:55 ./greptest 

-delete 删除查找到的文件

[root@zzd ~]# ls 002  a  abc  ABC  anaconda-ks.cfg  date.sh  greptest  passwd  qwer  test [root@zzd ~]# find -name 002 -delete		//在当前目录查找文件名为002的文件并删除 [root@zzd ~]# ls a  abc  ABC  anaconda-ks.cfg  date.sh  greptest  passwd  qwer  test  

-fls 路径 查找到的文件以长格式保存至指定文件

[root@zzd ~]# find -name greptest -fls /root/findfls	//在当前目录查找文件名为greptest的文件,并以长格式保存至/root/findfls文件 [root@zzd ~]# cat findfls   33820697      4 -rw-r--r--   1  1001     1001           62 Jul  4 23:55 ./greptest 

-ok 命令 {} ; 对查找到的文件执行命令,每次操作需要用户确认*

[root@zzd ~]# find -name greptest -ok rm -rf {} ; 		//在当前目录查找文件名为greptest的文件并执行删除命令 < rm ... ./greptest > ? n		//需要用户确认,y为是,n为否 

-exec 命令 {} ; 对查找到的文件执行命令。不需要用户确认

[root@zzd ~]# find -name greptest -exec rm -rf {} ; 		//在当前目录查找文件名为greptest的文件并执行删除命令 [root@zzd ~]# ls a  abc  ABC  anaconda-ks.cfg  date.sh  findfls  passwd  qwer  test 

xargs 通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可。因为有些命令不能接收过多的参数,所以需要用到xargs

[root@zzd ~]# ls /opt/ abc  Abc  ABc  ABC  anaconda-ks.cfg  date.sh  findfls  passwd  qwer [root@zzd ~]#  [root@zzd ~]# find /opt -type f | xargs rm -rf 		//查找/opt目录下的所有文件,然后删除 [root@zzd ~]# ls /opt/ [root@zzd ~]# 

3.3 组合条件

-a 必须同时满足所有条件,才符合查找标准

//拥有多个条件的时候,默认就是同时满足 [root@zzd ~]# tree /opt/ /opt/ ├── a │   └── abc └── abc  2 directories, 1 file  [root@zzd ~]# find /opt -name abc -type f		//查找/opt目录下类型为文件且文件名为abc的文件 /opt/abc  [root@zzd ~]# find /opt -name abc -a -type f /opt/abc 

-o 只要满足其中一个条件就符合查找标准

[root@zzd ~]# find /opt -name abc -o -type f		//查找/opt目录下名为abc或类型为f的文件或目录 /opt/.bash_logout /opt/.bash_profile /opt/.cshrc /opt/.tcshrc /opt/.bash_history /opt/.viminfo /opt/.bashrc /opt/.lesshst /opt/abc /opt/a/abc 

4. 文件层级系统

FHS     //文件层级系统 /       //可以单独分区,LVM分区 /boot       //系统启动相关的文件,如内核(vmlinuz)、initrd(initramfs),             //以及grub(bootloader)。建议单独分区,基本分区 /dev        //设备文件。不能单独分区     设备文件    //关联至一个设备驱动程序,进而能够与之对应硬件设备进行通信         块设备     //随机访问,数据块(比如硬盘)         字符设备    //也叫线性设备,线性访问,按字符为单位(比如鼠标、显示器)         设备号     //主设备号(major)和次设备号(minor)             主设备号标识设备类型             次设备号标识同一类型下的不同设备     设备文件只有元数据,没有数据 /etc        //配置文件 /home       //普通用户的家目录,每一个用户的家目录通常默认为/home/USERNAME。              //建议单独分区 /root       //管理员的家目录。不该单独分区 /lib        //库文件     静态库     //.a     动态库     //.dll,.so(shared object)     /lib/modules    //内核模块文件 /media      //挂载点目录,通常用来挂载移动设备 /mnt        //挂载点目录,通常用来挂载额外的临时文件系统,比如另一块硬盘 /opt        //可选目录,早期通常用来安装第三方程序 /proc       //伪文件系统,内核映射文件(伪文件系统实际上里面是没有任何内容的,              //开机之后才映射上去的)。不能单独分区 /sys        //伪文件系统,跟硬件设备相关的属性映射文件(伪文件系统实际上里面是没有              //任何内容的,开机之后才映射上去的)。不能单独分区 /tmp        //临时文件,/var/tmp /var        //可变化的文件,比如log、cache。存放日志信息、pid文件、lock文件,             //建议单独分区 /bin        //可执行文件,用户命令 /sbin       //管理命令 /usr        //shared,read-only,全局共享只读文件。提供操作系统核心功能,可以单独分区     /usr/bin     /usr/sbin     /usr/lib /usr/local      //第三方软件安装路径     /usr/local/bin     /usr/local/sbin     /usr/local/lib     /usr/local/etc     /usr/local/man  /etc,/bin,/sbin,/lib内是系统启动就需要用到的程序,这些目录不能挂载额外的分区, 必须在根文件系统的分区上  /usr/bin,/usr/sbin,/usr/lib提供操作系统核心功能,/usr可以单独分区  /usr/local/bin,/usr/local/sbin,/usr/local/lib,/usr/local/etc,  /usr/local/man等等在/usr/local目录下的内容都是第三方软件,建议单独分区 

5.重定向与管道服

5.1 系统设定

默认输入设备:标准输入,stdin, 0 (键盘)
默认输出设备:标准输出,stdout, 1 (显示器)
标准错误输出:stderr, 2 (显示器)

5.2 I/O重定向

> 覆盖

[root@zzd ~]# ls > new [root@zzd ~]# cat new a abc ABC anaconda-ks.cfg date.sh findfls new passwd qwer test [root@zzd ~]# ls abc > new		//覆盖了原本的内容 [root@zzd ~]# cat new  abc 

>> 追加

[root@zzd ~]# cat findfls >> new    //将findfls文件里的内容追加到文件new里 [root@zzd ~]# cat new  abc  33820697      4 -rw-r--r--   1  1001     1001           62 Jul  4 23:55 ./greptest 

2> 重定向错误输出

[root@zzd ~]# asdf -bash: asdf: command not found [root@zzd ~]# asdf 2> 1.txt		//重定向错误输出,将报错写入1.txt文件 [root@zzd ~]# cat 1.txt  -bash: asdf: command not found 

2>> 追加重定向错误输出

[root@zzd ~]# ls sadgga 2>> 1.txt 		//重定向错误输出,将报错追加到1.txt文件 [root@zzd ~]# cat 1.txt  -bash: asdf: command not found ls: cannot access 'sadgga': No such file or directory 

&> 覆盖重定向标准输出或错误输出至同一个文件

[root@zzd ~]# ls passwd asdf &> 1.txt  [root@zzd ~]# cat 1.txt  ls: cannot access 'asdf': No such file or directory passwd 

&>> 追加重定向标准输出或错误输出至同一个文件

[root@zzd ~]# cat findfls asfagh &>> 1.txt [root@zzd ~]# cat 1.txt  ls: cannot access 'asdf': No such file or directory passwd  33820697      4 -rw-r--r--   1  1001     1001           62 Jul  4 23:55 ./greptest cat: asfagh: No such file or directory 

< 输入重定向

[root@zzd ~]# cat 1.txt  [root@zzd ~]# cat qwer  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@zzd ~]# cat > 1.txt < qwer  [root@zzd ~]# cat 1.txt  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@zzd ~]# cat > dame << quit		//指向文件dame,输入,当遇到quit的时候退出 > 123 > 4 > qw > as > quit [root@zzd ~]# cat dame  123 4 qw as 

5.3 管道符

前一个命令的输出,作为后一个命令的输入。最后一个命令会在当前shell进程子shell进程中执行
命令1 | 命令2 | 命令3 | ...

[root@zzd ~]# ls 1.txt  a  abc  ABC  anaconda-ks.cfg  dame  date.sh  findfls  haha  new  passwd  qwer  test [root@zzd ~]# ls | grep ABC		//列出当前目录下的所有文件或目录,将结果交给grep过滤ABC ABC  

5.4 tee

从标准输入读取数据,输出一份到屏幕上,一份保存到文件

[root@zzd ~]# echo 'Hello ZIC' | tee 1.txt  Hello ZIC [root@zzd ~]# cat 1.txt  Hello ZIC 

6.bash字符处理

6.1 截取变量的字符串

FILE=/usr/local/hadoop/etc/hadoop
echo ${FILE#*/}

[root@zzd ~]# FILE=/usr/local/hadoop/etc/hadoop		//创建一个变量FILE,值为/usr/local/hadoop/etc/hadoop [root@zzd ~]# echo $FILE		//打印变量FILE	 /usr/local/hadoop/etc/hadoop  [root@zzd ~]# echo ${FILE#*/}		 usr/local/hadoop/etc/hadoop 

echo ${FILE##*/}

[root@zzd ~]# echo ${FILE##*/} hadoop 

echo ${FILE%/*}

[root@zzd ~]# echo ${FILE%/*} /usr/local/hadoop/etc 

6.2 字符串切片

${var:offset:number}

[root@zzd ~]# echo $FILE		 /usr/local/hadoop/etc/hadoop [root@zzd ~]# echo ${FILE:4:5}		//从变量FILE的第4个索引开始截取5个字符 /loca 

6.3 取字符串最右侧几个字符(自右向左)

${var: -lengh} 注意:冒号后面必须有一空白字符

[root@zzd ~]# echo ${FILE: -6}		//截取变量FILE最右侧的6个字符 hadoop 

6.4 查找替换

${var/pattern/substi} 查找var所表示的字符串中,第一次被pattern所匹配到字符串,以substi替换之

[root@zzd ~]# echo ${FILE/hadoop/had}	 /usr/local/had/etc/hadoop 

${var//pattern/substi} 查找var所表示的字符串中,所有能被pattern所匹配到字符串,以substi替换之

[root@zzd ~]# echo ${FILE//hadoop/had} /usr/local/had/etc/had 

${var/#pattern/substi} 查找var所表示的字符串中,行首被pattern所匹配到字符串,以substi替换之

[root@zzd ~]# file='hello world' [root@zzd ~]# echo $file hello world [root@zzd ~]# echo ${file/#hello/hi} hi world 

${var/%pattern/substi} 查找var所表示的字符串中,行尾被pattern所匹配到字符串,以substi替换之

[root@zzd ~]# echo ${FILE/%hadoop/had} /usr/local/hadoop/etc/had 

6.5 查找并删除

${var/pattern} 查找var所表示的字符串中,第一次被pattern所匹配到字符串,删除之

[root@zzd ~]# echo $FILE		 /usr/local/hadoop/etc/hadoop [root@zzd ~]# echo ${FILE/hadoop} /usr/local//etc/hadoop 

${var//pattern} 查找var所表示的字符串中,所有被pattern所匹配到字符串,删除之

[root@zzd ~]# echo ${FILE//hadoop} /usr/local//etc/ 

${var/#pattern} 查找var所表示的字符串中,行首被pattern所匹配到字符串,删除之

[root@zzd ~]# echo $file hello world [root@zzd ~]# echo ${file/#hello} world 

${var/%pattern} 查找var所表示的字符串中,行尾被pattern所匹配到字符串,删除之

[root@zzd ~]# echo ${FILE/%hadoop} /usr/local/hadoop/etc/ 

6.6 字符大小写转换

${var^^} 所有小写转换为大写

[root@zzd ~]# echo ${FILE^^}		//将变量FILE里的小写转换为大写		 /USR/LOCAL/HADOOP/ETC/HADOOP 

${var,,} 所有大写转换为小写

[root@zzd ~]# FILE=${FILE^^}	//将变量FILE里的小写转换为大写并赋予变量FILE [root@zzd ~]# echo $FILE /USR/LOCAL/HADOOP/ETC/HADOOP [root@zzd ~]# echo ${FILE,,}		//将FILE变量里的大写都转换为小写 /usr/local/hadoop/etc/hadoop