• Uncategorized

About linux : What-is-the-regular-file-on-CentOS-and-Ubuntu

Question Detail

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)?

Question Answer

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

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.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.