Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does linux shell determine whether a file exists

Shulou Source: shulou.com Published: 2022-05-31 23:06:33 10月16日 Update

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!

Tags: Files folders commands permissions learning content directories links examples examples that is commonly used general and have ideas situations operators tutorials articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker macOS NVidia Xiaomi MySQL