Check if the file exists on the Linux Shell
Everyone may encounter such a need when they work. How do you check if a file exists in a bash environment of a Unix-like system? Since there is demand, of course there is a solution.
The test command in the shell can be used to detect the type of the file or compare the values. The command can also be used to check whether the file exists, as shown below:
Test -e filename
[ -e filename ]
Test -f filename
[ -f filename ]
A more common use is to place the above command in a conditional expression judged by the if ... else ... fi condition, and then write different branching logic in it:
#!/bin/bash
File="/etc/hosts"
If [ -f "$file" ]
Then
Echo "$file found."
Else
Echo "$file not found."
Fi
The following command uses the shell's conditional expression to determine if the /etc/hosts file exists:
[ -f /etc/hosts ] && echo "Found" || echo "Not found"
This combined command will output the following:
Found
The relevant operators for detecting file attributes:
If the file exists and has the appropriate properties, the following operators will return true :
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted
The above command was copied from man test.
The method of using the above symbols is exactly the same:
If [ operator FileName ]
Then
Echo "FileName - Found, take some action here"
Else
Echo "FileName - Not found, take some action here"
Fi
There are also three expressions that compare two files to each other:
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
Visti : https://www.spotoclub.com/product/cisco-ccie-collaboration-lab/
The test command in the shell can be used to detect the type of the file or compare the values. The command can also be used to check whether the file exists, as shown below:
Test -e filename
[ -e filename ]
Test -f filename
[ -f filename ]
A more common use is to place the above command in a conditional expression judged by the if ... else ... fi condition, and then write different branching logic in it:
#!/bin/bash
File="/etc/hosts"
If [ -f "$file" ]
Then
Echo "$file found."
Else
Echo "$file not found."
Fi
The following command uses the shell's conditional expression to determine if the /etc/hosts file exists:
[ -f /etc/hosts ] && echo "Found" || echo "Not found"
This combined command will output the following:
Found
The relevant operators for detecting file attributes:
If the file exists and has the appropriate properties, the following operators will return true :
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted
The above command was copied from man test.
The method of using the above symbols is exactly the same:
If [ operator FileName ]
Then
Echo "FileName - Found, take some action here"
Else
Echo "FileName - Not found, take some action here"
Fi
There are also three expressions that compare two files to each other:
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
Visti : https://www.spotoclub.com/product/cisco-ccie-collaboration-lab/
评论
发表评论