linux shell文件比较,linuxSHELL学习之文件比较

一、test命令能够测试linux文件系统上的文件状态和路径-d file   检查file是否是存在并且是一个目录

-e file   检查file是否存在

-f file   检查file是否存在并且是一个文件

-r file   检查file是否存在并且可读

-s file   检查file是否存在并且不为空

-w file   检查file是否存在并且可写

-x file   检查file是否存在并且可执行

-O file   检查file是否存在并且被当前用户拥有

-G file   检查file是否存在并且默认用户组是否为当前用户组

file1  -nt  file2  检查file1是否比file2新

file1  -ot  file2  检查file1是否比file2旧

1、检查目录-d 检查一个文件是否作为目录存在于系统中,如果想将文件写到一个目录下,或者在视图改变到目录位置之前,通常使用此操作:

[root@t1 ~]# cat t6.sh

#!/bin/bash

if [ -d $HOME ]

then

echo "your home directory exists"

cd $HOME

ls -a

else

echo "there is a problem with your directory"

fi

[root@t1 ~]# ./t6.sh

your home directory exists

.   .bashrc   .gconf     .gtkrc-1.2-gnome2 .nautilus     t1.sh  .tcshrc

..   .chewing  .gconfd     .ICEauthority readline-5.0  t2.sh  .thumbnails

anaconda-ks.cfg  .cshrc    .gnome     install.log  .redhat       t3.sh  .Trash

.bash_history  Desktop   .gnome2     install.log.syslog rlwrap-0.37   t4.sh  .Xauthority

.bash_logout  .dmrc    .gnome2_private  l   .scim       t5.sh  .xsession-errors

.bash_profile  .eggcups  .gstreamer-0.10  .metacity  .ssh       t6.sh

2、检查对象是否存在-e 在脚本中使用文件或者目录之前,-e比较能够检查他们是否存在:

[root@t1 ~]# cat t7.sh

#!/bin/bash

#checking if a directory exists

if [ -e $HOME ]

then

echo "OK on the directory ,now let's check the file"

#checking if a file exists

if [ -e $HOME/testing ]

then

#the file exists,appand date to it

echo "Appanding date to existing file"

date >> $HOME/testing

else

#the file does't exist,create a new file

echo "creating new file"

date > $HOME/testing

fi

else

echo "sorry ,you don't have a home directory"

fi

[root@t1 ~]# ./t7.sh

OK on the directory ,now let's check the file

Appanding date to existing file

3、检查文件-e比较适用于文件也适用于目录,要确定指定的对象是一个文件,必须使用-f比较:

[root@t1 ~]# cat t8.sh

#!/bin/bash

#check if a file

if [ -e $HOME ]

then

echo "the objet exists,is it a file"

if [ -f $HOME ]

then

echo "yes ,it's a file"

else

echo "it is not a file"

if [ -f $HOME/.bash_profile ]

then

echo "yes,it's a file"

else

echo "no,it's not a file"

fi

fi

else

echo "sorry,the object doesn't exist"

fi

[root@t1 ~]# ./t8.sh

the objet exists,is it a file

it is not a file

yes,it's a file

4、是否可读-r 判断文件是否可读

[root@t1 ~]# ls -l |grep t8.sh

-rwxr--r-- 1 root   root    361 07-02 18:07 t8.sh

[root@t1 ~]# cat t9.sh

#!/bin/bash

#testing is you can read the file

if [ -f $HOME/t8.sh ]

then

#now,test is you can read the file

if [ -r $HOME/t8.sh ]

then

echo "you can read the file"

else

echo "you can't read the file"

fi

else

echo "sorry the file dasen't exist"

fi

[root@t1 ~]# ./t9.sh

you can read the file

5、检查空文件-s 用来检查文件是否不为空,当比较成功时则表明文件包含数据

[oracle@t1 ~]$ cat t10.sh

#!/bin/bash

#testing if a file is empty

if  [ -s $HOME ]

then

echo "the file is exist and not empty"

else

if [ -e $HOME ]

then

echo "the file is exist but empty"

else

echo "the file doesn't exist"

fi

fi

[oracle@t1 ~]$ ./t10.sh

the file is exist and not empty

6、检查是否能够向文件中写数据-w 用于判断是否可以像文件中写入数据

[oracle@t1 ~]$ ./t11.sh

you cat write to it

[oracle@t1 ~]$ cat t11.sh

#!/bin/bash

#test if a file can be write

if [ -r $HOME ]

then

echo "you cat write to it"

else

echo "you can't write to it"

fi

[oracle@t1 ~]$ ./t11.sh

you cat write to it

7、检查是否能够运行文件-x 用于检查是否对一个文件有运行的权限

[oracle@t1 ~]$ cat t12.sh

#!/bin/bash

#test if can run the file

if [ -x $HOME/t1.sh ]

then

echo "you can run the file"

else

echo "sorry,you can't run the file"

fi

[oracle@t1 ~]$ ./t12.sh

sorry,you can't run the file

8、检查文件所有权

-O用于检查您是否是文件的所有者   -G用于检查文件是否属于默认组(而不是用户所在的组)

[root@t1 ~]# ./t13.sh

you are the owner of the file

[root@t1 ~]# cat t13.sh

#!/bin/bash

#test if the file is yours

if [  -O $HOME/t1.sh ]

then

echo "you are the owner of the file"

else

echo "you are not the owner of the file"

fi

[root@t1 ~]# ./t13.sh

you are the owner of the file

9、检查文件日期file1 -nt file2 用于判断file1是否比file2新

[root@t1 ~]# cat t14.sh

#!/bin/bash

#test file date

if [ t2.sh -nt t1.sh ]

then

echo "file t2.sh is newer than file t1.sh"

else

echo "file t2.sh is older than file t1.sh"

fi

[root@t1 ~]# ./t14.sh

file t2.sh is newer than file t1.sh

file1 -ot file2用于判断file1是否比file2旧

[root@t1 ~]# cat t15.sh

#!/bin/bash

#test file date

if [ t1.sh -ot t2.sh ]

then

echo "file t1.sh is older than file t2.sh"

else

echo "file t1.sh is newer than file t2.sh"

fi

[root@t1 ~]# ./t15.sh

file t1.sh is older than file t2.sh

**如果参加比较的两个文件有任意一个不存在则返回的结果就是不成功状态

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值