Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does linux shell determine whether a file exists

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "linux shell how to judge the existence of files", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "linux shell how to judge the existence of files" bar!

In linux shell, you can use if statements and "- e filename" expressions to determine whether a file exists, the syntax "if [- e filename]; then echo" file exists "; else echo" file does not exist "; fi".

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Determine the basic format of the file. [operator file or directory]

-e filename true if filename exists

-d filename true if filename is a directory

-f filename true if filename is a regular file

-L filename true if filename is a symbolic link

-r filename true if filename is readable

-w filename true if filename is writable

-x filename true if filename is executable

-s filename true if the file length is not 0

-h filename true if the file is a soft link

Common examples

If a file exists, delete it

If [- f trials]; then rm ${result_path} trials; fi

If there is no folder, create

If [!-d $result_name]; then mkdir-p $result_namefi

To determine whether a file or folder exists by shell command, first look at the instance:

#! / bin/sh # determine the existence of the file, determine whether it is a folder and other testPath= "/ Volumes/MacBookProHD/Mr.Wen/08 shell command" testFile= "/ Volumes/MacBookProHD/Mr.Wen/08 shell command / fileWen" # determine whether the folder exists-dif [!-d "$testPath"] Then echo "folder does not exist" else echo folder exists "fi # determines whether the folder exists and has executable permission if [[!-x" $testFile "]; then echo" file does not exist and does not have executable permission "else echo file exists and has executable permission" fi # determines whether a file exists if [- e "$testFile"] Then echo "file does not exist" else echo "file exists" fi thank you for reading, above is the content of "linux shell how to judge whether the file exists", after the study of this article, I believe you have a deeper understanding of how to judge whether the file exists in linux shell, and the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report