In shell scripts, '-f' and '-d' are conditional expressions used to test file types.
1. '-f' expression:
- Expression: '[ -f file ]'
- Description: Determines whether a given path is a regular file. A regular file refers to a file that is not a directory or device file.
- Example:
if [ -f /path/to/file ]; then
echo "This is a file."
fi
2. '-d' expression:
- Expression: '[ -d directory ]'
- Description: Determines whether a given path is a directory.
- Example:
if [ -d /path/to/dirctory ]; then
echo "This is a directory"
fi
The above two expressions are usually used in combination with conditional statements, such as 'if' statements, to perform different operations based on the type of a file or directory.
Sample script:
!/bin/bash
file_path="/path/to/somefile" directory_path="/path/to/somedirectory"
Check if the file exists and is a regular file
if [ -f "$file_path" ]; then echo "The file exists and is a regular file." else echo "The file either does not exist or is not a regular file." fi
Check if the directory exists
if [ -d "$directory_path" ]; then echo "The directory exists." else echo "The directory does not exist." fi
This script demonstrates how to use the '-f' and '-d' conditional expressions to test files and directories. In actual scripts, such conditional judgments are often used to perform different operations according to different situations.
The meaning of the "-e -d -f -eq -ne -gt -ge" operator in common Linux shell scripts
The meaning of the "-e -d -f -eq -ne -gt -ge" operator in common shell scripts:
File expressions
-e filename: If filename exists, it is true.
-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: If filename is writable, it is true.
-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.
filename1 -nt filename2: True if filename1 is newer than filename2.
filename1 -ot filename2: If filename1 is older than filename2, then true.
Integer variable expression
-eq: equal to
-ne: Not equal to
-gt: greater than
-ge: greater than or equal to
-lt : less than
-le: less than or equal to
String variable expression
If [ $a = $b ] Strings allow for equal signs using assignment numbers.
if [ $string1 != $string2 ]: True if string1 is not equal to string2.
if [ -n $string ] : If string is not empty, it is true.
if [ -z $string ] : If string is empty, it is true.
if [ $sting ] �
Logical non-logical!
if [ ! Expression] : The logical non-conventional expression
if [ ! -d $num ] : If the directory $num does not exist
Logic and –a
if [Expression 1 –a Expression 2]: The conditional expression and
Logical or -o
if [Expression 1 –o Expression 2]: or
This is the end of this article about what '-f' and '-d' mean in shell scripts. For more related shell '-f' and '-d' content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!