Linux check file and compare two files (very verbose)

How to check the file in linux to see if the file exists?

The command we use today is the test command. The most important function of this command is to check/compare files

Table of contents

Overview of test parameters:

Detailed demo:

1. Check if the file exists and is a directory

2. Check whether the file exists (you can judge whether the file and directory exist #Existence is zero, non-existence is non-zero)

 3. Check if the file exists and is a file  

4. Check if the file exists and is readable

5. Check whether the file exists and is writable

6. Check whether the file exists and is executable#You can judge the command

7. Check if the file exists and is not empty #Key

 8. Check if the file exists and is owned by the current user

 9. Check if the file exists and the default group is the current user group

10. Check whether file 1 is newer than file 2 #The two dates compared here

11. Check whether file 1 is older than file 2#The date of comparison is new and old

 12. Check whether file 1 is the same as file 2, judge by i node

13. Check whether file 1 is the same as file 2, it is judged by i node

expand:


Overview of test parameters:

-d #检查文件是否存在且为目录

-e #检查文件是否存在

-f #检查文件是否存在且为文件

-r #检查文件是否存在且为可读

-w #检查文件是否可写

-x #检查文件是否存在且可执行

-s #检查文件是否存且不为空

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

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

file1 -nt file2  #检查 文件1 是否比 文件2 新 

file1 -ot file2 #检查 文件1  是否比 文件2 旧

file1 -ef file2 #检查 文件1 是否与 文件2 相同  是按i节点判断

Detailed demo:

## echo $? #Return the result after the last execution is successful

If the return value is zero, the execution is successful; non-zero failure

";" semicolon usage: Command 1 ; Command 2 Use ";" to separate each command, and each command will be executed sequentially from left to right, and all commands will be executed regardless of whether they fail or not. .

1. Check if the file exists and is a directory

Writing: 

#判断一个文件是否存在并且为目录
test -d /shell ; echo $?   #这里我shell那个目录已经建立好了,如果没有返回的值就是非零的数

operation result:

 The result is 0, indicating that the file exists and is a directory. If it is not a directory, the returned value is non-zero.

2. Check whether the file exists (you can judge whether the file and directory exist #Existence is zero, non-existence is non-zero)

Writing: 

test -e /shell/ ;echo $?  #判断目录是否存在
test -e /shell/1.txt  ;echo $? #判断文件是否存在

operation result:

 The returned result is 0 to indicate existence

 3. Check if the file exists and is a file  

Writing:

test -f /shell/1.txt ; echo $? #判断文件是否存在 并且为文件

operation result:

 zero success, non-zero failure

4. Check if the file exists and is readable

## ls -l and ll can check file permissions, so how can we identify whether a file has readable permissions without these two commands? and the file exists?

At present, all my operations are performed under root authority, so let's switch to a normal user to type commands

Writing:

test -r /shell ; echo $? #判断文件是否为当前用户 可读 

 operation result:

see result is non-zero, not readable by current user

5. Check whether the file exists and is writable

Writing:

test -w /shell ; echo $? #查看文件或目录 在当前用户下是否可写

 Running result: #Currently, I do not have permission to write under normal user

6. Check whether the file exists and is executable#You can judge the command

Writing:

test -x /shell ; echo $? #判断文件或目录是否存在并且为可执行

 operation result:

 Judgment command:

7. Check if the file exists and is not empty #Key

Writing:

test -s /shell/1.txt ;echo $? #判断1.txt文件内是否有内容

operation result:

As a result, you can see that there is no content in the 1.txt file. There is content under the /shell directory ##Here, note that if you directly echo > /shell/1.txt, the returned result is 0 because echo entered a null value into 1. In the txt file, it is judged that there is content in the file

 8. Check if the file exists and is owned by the current user

Writing:

test -O /shell ; ehco $?  #检查文件/目录是否存在并且被当前用户拥有

 operation result

I created it with root authority, so I can judge whether it was created by the current user. If there is no authority, the owner and group of the culture or directory cannot be modified. If it is changed to the current user, the returned result is 0.

 9. Check if the file exists and the default group is the current user group

 Writing:

test -G /shell ; echo $? #检查当前文件或者目录是否为当前用的组所拥有

operation result:

Consistent with the meaning of the 8th

10. Check whether file 1 is newer than file 2 #The two dates compared here

Writing:

test 1.txt -nt 2.txt ; echo $? #判断1.txt是否比2.txt新

operation result:

 

11. Check whether file 1 is older than file 2#The date of comparison is new and old

Writing:

test 1.txt -ot 2.txt ; echo $? #判断1.txt是否比2.txt旧

 operation result:

 12. Check whether file 1 is the same as file 2, judge by i node

Writing:

test 1.txt -ef 2.txt ; echo $? #判断两个文件是否相同 查看i节点 ls -i + file

operation result:

13. Check whether file 1 is the same as file 2, it is judged by i node

wording

test 1.txt -ef 2.txt ; echo $? #判断两个文件是否相同 是以i节点作为判断的

operation result:

expand:

How to view files, permissions, recent access, recent changes, and time of recent changes, you can use the stat command

Guess you like

Origin blog.csdn.net/weixin_58279299/article/details/123494619