In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
How to check if a file or directory exists in Bash? This problem may be our daily study or work often see. I hope you learned something from this question. The following is the reference content brought to you by Xiaobian. Let's take a look together!
Many times, when writing Shell scripts, you need to perform actions based on whether files exist or not. How do I check if the file exists?
test command
In Bash, you can use the test command to check whether a file exists and determine the type of file.
The test command takes one of the following syntax forms:
test EXPRESSION[ EXPRESSION ][[ EXPRESSION ]]
If you want scripts to be portable, you should prefer to use the old test [command available on all POSIX shells. On most modern systems that use bash, zsh, and ksh as default shels, a newly upgraded version of the test command [[(double parenthesis) is supported.
File operator of test command
The test command includes the following File operators, which allow testing specific types of files:
● -b FILE: True if FILE exists and is a block special file.
● -c FILE: True if FILE exists and is a special character file.
● -d FILE: True if FILE exists and is a directory.
-e FILE: True if FILE exists and is a file, regardless of type (node, directory, socket, etc.).
-f FILE: True if FILE exists and is a regular file (not a directory or device).
-G FILE: True if FILE exists and has the same group as the user running the command.
● -h FILE: True if FILE exists and is a symbolic link.
-g FILE: True if FILE exists and the set-group-id (sgid) flag is set.
● -k FILE: True if FILE exists and the sticky bit flag is set.
● -L FILE: True if FILE exists and is a symbolic link.
-O FILE: True if FILE exists and is owned by the user running the command.
-p FILE: True if FILE exists and is a pipe.
-r FILE: True if FILE exists and is readable.
-S FILE: True if FILE exists and is a socket.
-s FILE: True if FILE exists and has a non-zero size.
-u FILE: True if exists and set-user-id (suid) flags are set.
-w FILE: True if FILE exists and can be written.
-x FILE: True if FILE exists and is executable.
How do I check if a file exists?
The most common FILE operators used to check for file existence are-e and-f. The first operator checks whether the file exists, regardless of file type; the second operator returns true only if the file is a regular file (not a directory or device).
The most readable option to check for the existence of a file is to use the test command in conjunction with an if statement. Any of the following snippets will check for the existence of the/etc/resolv.conf file:
FILE=/etc/resolv.confif test -f "$FILE"; then echo "$FILE exist"fiFILE=/etc/resolv.confif [ -f "$FILE" ]; then echo "$FILE exist"fiFILE=/etc/resolv.confif [[ -f "$FILE" ]]; then echo "$FILE exist"fi
If you want to perform different actions depending on whether the file exists, simply use the if / then structure:
FILE=/etc/resolv.confif [ -f "$FILE" ]; then echo "$FILE exist"else echo "$FILE does not exist"fi
Note: Always use double quotes to avoid problems when working with files that contain spaces in their names.
You can also use the test command without an if statement. Commands following the && operator are executed only if the exit status of the test command is true,
test -f /etc/resolv.conf && echo "$FILE exist"[ -f /etc/resolv.conf ] && echo "$FILE exist"[[ -f /etc/resolv.conf ] && echo "$FILE exist"
If you want to run a series of commands after the && operator, simply enclose the commands in curly brackets separated by ";" or "&&."
[ -f /etc/resolv.conf ] && { echo "$FILE exist"; cp "$FILE" /tmp/; }
Contrary to &,|| Operator is executed only if the exit state of the test command is false.
[ -f /etc/resolv.conf ] && echo "$FILE exist" || echo "$FILE does not exist"
How do I check if a directory exists?
The-d operator allows testing whether a file is a directory.
For example, to check if the/etc/docker directory exists, you need to use:
FILE=/etc/dockerif [ -d "$FILE" ]; then echo "$FILE is a directory"fi[ -d /etc/docker ] && echo "$FILE is a directory"
Similarly, double brackets [] can be used instead of single brackets [].
How do I check if a file doesn't exist?
Similar to many other languages, it can be used! (exclamation mark) logical NOT operator to negate test expression:
FILE=/etc/dockerif [ ! -f "$FILE" ]; then echo "$FILE exist and it is a directory"fi
Same as above:
[ ! -f /etc/docker ] || echo "$FILE does not exist"
How do I check if there are multiple files?
If there are multiple files, you can use-a (or &&with[]) to test instead of complex nested if/else constructs:
FILE=/etc/dockerif [ -f /etc/resolv.conf -a -f /etc/hosts ]; then echo "$FILE is a directory"fiFILE=/etc/dockerif [ -f /etc/resolv.conf && -f /etc/hosts ]; then echo "$FILE is a directory"fi
Equivalent variants that do not use IF statements:
[ -f /etc/resolv.conf -a -f /etc/hosts ] && echo "both files exist"[[ -f /etc/resolv.conf && -f /etc/hosts ]] && echo "both files exist" Thank you for reading! After reading the above, do you have a general idea of how to check whether a file or directory exists in Bash? I hope the content of this article is helpful to everyone. If you want to know more about related articles, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.