简体   繁体   中英

What is “the regular file” on CentOS and Ubuntu?

My environment is:

  • CentOS 6.9
  • Ubuntu 16.04 LTS

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 eg

if [ -h LN-file ]
then
    echo "file is a symlink [-h]"
elif [ -f LN-file ]
then
    echo "file is a regular file [-f]"
fi

A regular file is a file that isn't a binary file neither a symbolic link. It use to be associated to a plain text file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM