shell脚本——判断文件的属性、内容、、
来源:互联网 发布:linux查找文件内容 编辑:程序博客网 时间:2024/05/21 22:29
#!/bin/sh
#文件路径 :/usr/local/1.txt
read myfile1
read myfile2
#判断文件1是否存在
if [ -e $myfile1 ]; then
echo '文件存在';
#判断文件1是否为空
if [ -s $myfile1 ]; then
echo '这个文件不为空';
else
echo '这个文件为空';
fi
else
echo '不存在文件';
fi
#判断文件2是否存在
if [ -e $myfile2 ]; then
echo '文件存在';
#判断文件2是否为空
if [ -s $myfile2 ]; then
echo '这个文件不为空';
else
echo '这个文件为空';
fi
else
echo '不存在文件';
fi
1、操作符"-e"用于仅判断文件是否存在
2、有三个操作符用于判断对文件的权限
2.1 "-x"判断是否有执行文件的权限
2.2 "-r" 判断是否有读文件的权限
2.3 "-w"判断是否有写文件的权限
3、另外一些文件操作符可以用来判断文件类型
3.1 "-f" 判断为常规文件
3.2 "-L"或者"-h"判断是否为符号链接文件
3.3 "-S" 判断是否为socket
3.4 "-d"判断是否为目录
3.5 其他一些 -b:判断是否为块设备文件,-c:用于判断是否为字符设备文件
4、"-s"判断文件是否为非空,即文件大小大于0,区分与大写S
#文件的字节数 、字数、行数
wc $myfile1 >aa.txt
wc $myfile2 >bb.txt
#文件的大小
du -h --max-depth=2 $myfile1 >>aa.txt
du -h --max-depth=2 $myfile2 >>bb.txt
#文件的权限
stat $myfile1 |grep Access | awk '{print $2}' >>aa.txt
stat $myfile2 |grep Access | awk '{print $2}' >>bb.txt
#文件最后修改时间
stat $myfile1 | grep Modify | awk '{split($3,var,".");print var[1]}' >>aa.txt
stat $myfile2 | grep Modify | awk '{split($3,var,".");print var[1]}' >>bb.txt
#比较文件aa.txt与文件bb.txt,确定文件1.txt与文件2.txt的大小、权限、修改时间
方法一:
(diff aa.txt bb.txt)
方法二:
CurRow=1
LastRow=`cat aa.txt | wc -l`
while [ $CurRow -le $LastRow ]
do
for x in `awk 'NR=='$CurRow' {print $0}' aa.txt`
do
for y in `awk 'NR=='$CurRow' {print $0}' bb.txt`
do
if [ "$x" == "$y" ];then
echo "$x" >>result.txt
fi
done
done
((CurRow++))
done
在result.txt文件中,保存了两个文件大小,时间的相同性质。
#文件路径 :/usr/local/1.txt
read myfile1
read myfile2
#判断文件1是否存在
if [ -e $myfile1 ]; then
echo '文件存在';
#判断文件1是否为空
if [ -s $myfile1 ]; then
echo '这个文件不为空';
else
echo '这个文件为空';
fi
else
echo '不存在文件';
fi
#判断文件2是否存在
if [ -e $myfile2 ]; then
echo '文件存在';
#判断文件2是否为空
if [ -s $myfile2 ]; then
echo '这个文件不为空';
else
echo '这个文件为空';
fi
else
echo '不存在文件';
fi
1、操作符"-e"用于仅判断文件是否存在
2、有三个操作符用于判断对文件的权限
2.1 "-x"判断是否有执行文件的权限
2.2 "-r" 判断是否有读文件的权限
2.3 "-w"判断是否有写文件的权限
3、另外一些文件操作符可以用来判断文件类型
3.1 "-f" 判断为常规文件
3.2 "-L"或者"-h"判断是否为符号链接文件
3.3 "-S" 判断是否为socket
3.4 "-d"判断是否为目录
3.5 其他一些 -b:判断是否为块设备文件,-c:用于判断是否为字符设备文件
4、"-s"判断文件是否为非空,即文件大小大于0,区分与大写S
#文件的字节数 、字数、行数
wc $myfile1 >aa.txt
wc $myfile2 >bb.txt
#文件的大小
du -h --max-depth=2 $myfile1 >>aa.txt
du -h --max-depth=2 $myfile2 >>bb.txt
#文件的权限
stat $myfile1 |grep Access | awk '{print $2}' >>aa.txt
stat $myfile2 |grep Access | awk '{print $2}' >>bb.txt
#文件最后修改时间
stat $myfile1 | grep Modify | awk '{split($3,var,".");print var[1]}' >>aa.txt
stat $myfile2 | grep Modify | awk '{split($3,var,".");print var[1]}' >>bb.txt
#比较文件aa.txt与文件bb.txt,确定文件1.txt与文件2.txt的大小、权限、修改时间
方法一:
(diff aa.txt bb.txt)
方法二:
CurRow=1
LastRow=`cat aa.txt | wc -l`
while [ $CurRow -le $LastRow ]
do
for x in `awk 'NR=='$CurRow' {print $0}' aa.txt`
do
for y in `awk 'NR=='$CurRow' {print $0}' bb.txt`
do
if [ "$x" == "$y" ];then
echo "$x" >>result.txt
fi
done
done
((CurRow++))
done
在result.txt文件中,保存了两个文件大小,时间的相同性质。
阅读全文
0 0
- shell脚本——判断文件的属性、内容、、
- Shell脚本———判断文件
- shell脚本中修改文件的内容
- shell脚本if判断文件
- 【脚本化文档】——属性&元素的内容
- Linux shell脚本区别内容相同的文件.
- Linux shell脚本区别内容相同的文件
- shell脚本:修改包含特定内容的多个文件
- 批量修改文件名称或者文件内容的shell脚本
- shell脚本,用awk实现替换文件里面的内容。
- 实现两个文件内容逐行进行对比的shell脚本
- Linux系统shell脚本对字符串、数字、文件的判断
- shell脚本判断变量文件目录的写法
- 判断文件是否存在的shell脚本代码
- 判断文件是否存在的shell脚本代码
- 判断文件是否存在的shell脚本代码
- shell脚本编程中对文件的判断
- 判断文件是否存在的shell脚本代码
- 自定义ProgressBar png样式支持直角显示
- 【HDU】 1003 Max Sum(最大连续子序列和)
- 在Spring MVC controller中配切点
- 基于Zigbee温湿度监测系统
- 数组查找元素
- shell脚本——判断文件的属性、内容、、
- Swift_学习笔记_泛型
- 知道别人的端口,如何看出端口有什么漏洞
- redis配置详解
- Spring源码阅读(七)—AOP创建代理
- eclipse将Java project 修改为Dynamic Web project
- 父类的static属性和方法
- Word教程Spire.Doc 教程:在Word文档中转换文件格式
- c++小数点后几位、有效位数、格式化输出