查找指定目录下包含"xml"的文件
Posted Bwz_Learning
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查找指定目录下包含"xml"的文件相关的知识,希望对你有一定的参考价值。
寻找当前母目录下包含”xml”这三个字符的文件。如果找到的胡话,就在当前的屏幕上打印出来。
获取/目录下的内容
[root@VM_156_149_centos ~]# ls -l /
total 68
lrwxrwxrwx. 1 root root 7 Apr 21 2016 bin -> usr/bin
dr-xr-xr-x. 4 root root 4096 Jul 25 15:21 boot
drwxr-xr-x 2 root root 4096 Apr 21 2016 data
drwxr-xr-x 18 root root 2860 Aug 19 13:21 dev
drwxr-xr-x. 85 root root 4096 Aug 19 13:21 etc
drwxr-xr-x. 2 root root 4096 Aug 12 2015 home
lrwxrwxrwx. 1 root root 7 Apr 21 2016 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Apr 21 2016 lib64 -> usr/lib64
drwx------. 2 root root 16384 Apr 21 2016 lost+found
drwxr-xr-x. 2 root root 4096 Aug 12 2015 media
drwxr-xr-x. 2 root root 4096 Aug 12 2015 mnt
drwxr-xr-x. 3 root root 4096 Apr 21 2016 opt
dr-xr-xr-x 92 root root 0 Aug 19 13:21 proc
dr-xr-x---. 3 root root 4096 Aug 19 17:35 root
drwxr-xr-x 21 root root 720 Aug 19 13:22 run
lrwxrwxrwx. 1 root root 8 Apr 21 2016 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Aug 19 17:40 shell
drwxr-xr-x. 2 root root 4096 Aug 12 2015 srv
dr-xr-xr-x 13 root root 0 Aug 19 13:21 sys
drwxrwxrwt. 7 root root 4096 Aug 19 17:28 tmp
drwxr-xr-x. 13 root root 4096 Apr 21 2016 usr
drwxr-xr-x. 19 root root 4096 Aug 19 13:21 var
使用awk获取到,每一行里面第9列的内容
[root@VM_156_149_centos ~]# ls -l / | awk ' printf $9 " " '
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin shell srv sys tmp usr var
在获取的第9列中,筛选出符合条件的内容,例如”包含b字符”
[root@VM_156_149_centos ~]# ls -l / | grep "b" --color | awk ' printf $9 " " '
bin boot lib lib64 sbin
Shell Script 1
#!/bin/sh
#获取符合条件的行
FILES=` ls -l / | awk 'print $9' `
#输出获取的行的内容
echo $FILES
#进行条件的过滤
for filename in $FILES
do
echo $filename | grep "b" --color
done
输出结果
[root@VM_156_149_centos shell]# sh 2.sh
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin shell srv sys tmp usr var
bin
boot
lib
lib64
sbin
Shell Script 2
#!/bin/sh
for filename in `ls /`
do
if [[ $filename = *b* ]] ; then
echo $filename
fi
done
输出结果
[root@VM_156_149_centos shell]# sh 8.sh
bin
boot
lib
lib64
sbin
以上是关于查找指定目录下包含"xml"的文件的主要内容,如果未能解决你的问题,请参考以下文章