My environment is:
The GNU coreutils 8.4 has the test command to check the file using -f
option.
man test
shows
-f FILE FILE exists and is a regular file
The definition of the "regular file" is ambiguous for me.
On the terminal, I did
$ touch afile
$ ln -fs afile LN-FILE
Then, I executed the following script (check_file_exist_180320_exec)
#!/usr/bin/env bash
if [ -e LN-file ]
then
echo "file exists [-e]"
fi
if [ -f LN-file ]
then
echo "file exists [-f]"
fi
For CentOS and Ubuntu, both show -e and -f for symbolic linked file (LN-FILE).
However, ls -l returns l:symbolik link
(not -:regular file
) identifiers for the LN-FILE file.
( see. https://linuxconfig.org/identifying-file-types-in-linux)
On the other hand, I found following, Difference between if -e and if -f
A regular file is something that isn't a directory / symlink / socket / device, etc.
answered Apr 18 '12 at 7:10
jman
What is the reference I should check for the "regular file" (for CentOS and Ubuntu)?
Note the documentation further down in man test
Except for -h and -L, all FILE-related tests dereference symbolic links.
Basically when you do -f LN-file
, and LN-file is a symbolic link, the -f
test will follow that symlink, and give you the result of what the symlink points to.
If you want to check if a file is a symlink or a regular file, you need to do e.g.
if [ -h LN-file ]
then
echo "file is a symlink [-h]"
elif [ -f LN-file ]
then
echo "file is a regular file [-f]"
fi