Regular file

Updated: 09/12/2023 by Computer Hope
Commands testing if a file is a regular file.

A regular file is one type of file stored in a file system. It is called "regular" primarily to distinguish it from other special types of files.

Most files used directly by a human user are regular files. For example, executable files, text files, and image files are regular files.

When data is read from or written to a regular file, the kernel performs that action according to the rules of the filesystem. For instance, writes may be delayed, journaled, or subject to other special operations.

Linux file types

In the Linux kernel, file types are declared in the header file sys/stat.h. The type name, symbolic name, and bitmask for each Linux file type are listed below.

Type name Symbolic name Bitmask
Socket S_IFSOCK 0140000
Symbolic link S_IFLNK 0120000
Regular file S_IFREG 0100000
Block special file S_IFBLK 0060000
Directory S_IFDIR 0040000
Character device S_IFCHR 0020000
FIFO (first in, first out) S_IFIFO 0010000

How can I tell if a file is regular?

In bash, the command "test -f file" returns an exit status of 0 if file is a regular file. It returns 1 if file is of another type or does not exist.

test -f /etc/passwd; echo $?  # check for regular file, echo exit status of test
0
test -f /etc; echo $?         # directories are not regular files, so test fails
1
file="/etc/passwd";   # assign filename, enclosed in "", to variable named file
if test -f "$file";   # reference its value with $. Enclose expansion in ""
then                  # this part will run if test returns 0
  echo "$file is a regular file.";
else                  # this part will run if test returns anything else
  echo "$file is not a regular file, or does not exist.";
fi
/etc/passwd is a regular file.

You can also check a file's type with stat:

stat /etc/passwd
  File: /etc/passwd
  Size: 2234        Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 132814      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-07-06 08:45:49.960000000 -0400
Modify: 2018-03-14 23:46:25.048004001 -0400
Change: 2018-03-14 23:46:25.052004001 -0400
 Birth: -

Inode, Operating system terms, Regular, Standard input (stdin), Standard output (stdout)