Linux files types

Recently I come across some linux command during I login to my customer site to retrieve activity log file, and also studies some of the linux file types, and simple command to know what is the file types at Linux Operating System.

Linux uses four basic file types:

linux file types

  • ordinary files
  • directories
  • symbolic links
  • block and character device files

You determine a file’s type by issuing the ls -l command and reading the first character of each row of the output.
The typical output of the ls command is as follows:

$ ls –l
total 8
-rw-r–r–     1 root   root       22 Oct  6 15:33 anormalfile
brw-rw—-     2 root   disk  41,   0 May  5 1998 blockdev
crw-rw-rw-     2 root   root   5,   0 May  5 1998 characterdev
drwxr-xr-x     2 root   root     4096 Oct  6 15:33 subdir
lrwxrwxrwx     1 root   root       11 Oct  6 15:35 symbolic -> anormalfile

Ordinary files begin with a dash (-), directories begin with d, symbolic links begin with the character l, block devices are prefaced with the character b, and character devices begin with the letter c.

Ordinary files
An ordinary file can consist of any kind of data, including executable programs. Most of the files in the Linux file system are of this type.

A directory is a file that contains other files and directories, and provides pointers to them.
It performs a similar function to a folder in a filing cabinet, in that it enables you to group related files in an organized fashion. However, whereas folders can normally contain files only, directories can contain additional directories, often referred to as subdirectories.

Symbolic links
A symbolic – or soft – link points to the name and location of a completely separate file. So when you open, copy, move or otherwise refer to the link, the operation is in fact performed on the referenced file. This distinction is usually invisible to the user. If the referenced file is removed or renamed, the link is broken and an error occurs if you try to open it.

You can also create hard links. A hard link points to the actual data in a file in exactly the same way as an ordinary file does. Therefore, other than the name, there is no difference between the original file and a hard link that points to the same data, and both can be regarded as ordinary files. You can distinguish a hard link from any other ordinary file only by the number of links that each one has. The number of links is displayed in the second field of an ls -l listing. If this number is greater than one, then you know there are additional hard links to the data.

Device files
All the physical devices that Linux uses are represented by device files.
Device files can be classified as character special or block special. Character-special files represent devices that interact with Linux on a character-by-character, or serial, basis. Printers and terminals are examples of this type of device. Block-special files represent devices such as hard or floppy disks and CD-ROMs, which interact with Linux using blocks of data.

All the device files are contained in the /dev directory – for example, the file associated with the system’s first floppy drive is /dev/fd0.

Device files are extremely powerful because they enable users to access hardware devices such as disk drives, modems, and printers as though they were data files. Therefore, you can move, copy, and transfer data between such devices easily, often without having to use special commands or syntax.

Filenames and pathnames

Every file is assigned a filename, which can be up to 256 characters long. This name can consist of a mixture of uppercase and lowercase letters, numbers, and certain punctuation marks such as the period, dash, or underscore.

Certain characters cannot be used in filenames. For example, you cannot use characters that represent a field separator – such as a comma – or other special characters that have particular meaning to the shell. The special characters that you cannot use are

! @ # $ % ^ & * ( ) [ ] { } ‘ ” \ / | ; < > ‘

You can navigate between directories on the command line using pathnames. To use pathnames, you must understand the directory structure of the Linux file system. The highest-level directory in the Linux file system is the root directory, which is represented by a forward slash (/). Located under the root directory are the top-level directories, followed by one or more subdirectory levels.

linux file type

File structure of a Linux file system
You can move between directories using relative or absolute pathnames.

A relative pathname starts with your current directory. For example, if you want to change to the expenses directory from within your home directory, you enter

cd expenses

Relative pathnames can begin with the name of a file or directory, or with symbolic references to the current directory (.) or its parent directory (..), but never with a forward slash.

linux file type

A simple example of a Linux file system
An absolute pathname shows the full pathname from the root directory (/). For example, the following command allows you to move from your current directory directly to the applic subdirectory in the usr directory that’s located under the root directory:

cd /usr/applic

Inodes, blocks, and special files

Every file is assigned a unique inode number. An inode is a structure that defines the file’s location and attributes. You can check for a file’s inode number using the -i option with the ls command. You can view the information that a file’s inode contains using the stat filename syntax. This command output (the stat results) displays the information related to the “results” file’s inode.

$ stat results
File: “results”
Size: 8
Filetype: Regular file
Mode: (0644/-rw-r–r–)
Uid: (     0/     root)
Gid: (     0/     root)
Device: 3,7    Inode: 123256    Links: 1
Access: Tue Jul 25 16:45:00 2000 (00072.18:31:07)
Modify: Thu Jul 20 12:35:20 2000 (00077.22:40:47)
Change: Thu Jul 20 12:35:20 2000 (00077.22:40:47)

In this example, some of the attributes that are displayed include the file type, file size, the owner’s User ID (UID), the number of hard links associated with it, and the file’s creation, access, and modification times.

An inode does not store a file’s name. Filenames are stored in directories with their associated inode numbers. In the example of the stat command, the name of the file is obtained from the filename parameter that you entered.

In the Linux file system, files are stored in blocks, which are identically sized segments of disk space. Generally, the size of a block varies from 512 bytes to 32 KB, depending on the Linux installation. The maximum size of a file depends on the block size used in the file system. For example, the maximum file size for an ext2 file system is 2 GB if it uses 512-byte blocks.

Disk systems retrieve data in block-sized chunks, so the larger the block size the more efficient the access. The problem with creating large blocks is that it can waste disk space. For example, if the block size is 4 KB and most files contain only a few bytes of data, most of the 4096 bytes of disk space is wasted for each file. But if you make the block sizes small, disk access will be relatively less efficient.

Some commands, such as df, output disk information in 1 KB blocks, even if the installation stores files in a different block size. A simple way to check your system’s block size is to use the du command to display the disk usage. In this example, the disk usage for all files beginning with “m” is displayed.

$ du -h m*
36k mail
4.0k mail.rc
12k mailcap
12k mailcap.vga
4.0k man.config
148k midi
40k mime-magic
104k mime-magic.dat
8.0k mime.types
4.0k minicom.users
0 motd
4.0k mtab

In this example, the du command output displays the file sizes on the disk in increments of the block size, which is 4.0 KB in this case. One exception to this rule is when zero is displayed, which indicates that the file is completely empty.

Special files
In addition to other file types, Linux makes use of special files, which are system-defined files that perform unique functions when accessed.

Special files and their functions

linux file types

For example, if you need to get rid of unwanted output from a command, you can redirect the output to the /dev/null file. In this example, any errors generated by the find command are redirected from stderr to /dev/null.

$ find / -n myfile 2> /dev/null

7 Responses to “Linux files types”

  1. Ah Hong June 20, 2009 at 10:49 am #

    I love the du -h command, but it just doesn’t applied in the HP-UX platform. So we need to use du -k with the output of size in Kb

  2. Dave June 20, 2009 at 7:50 pm #

    I always confuse with soft link and hard link. What I know is that soft link can link to files and directories. But hard links only for files.

    Other than this, am not sure how will it behave if the source files are move, delete or rename…

  3. ChampDog June 21, 2009 at 12:05 pm #

    I usually use soft link only? Any occasion we want to use hard link?

  4. Sean June 22, 2009 at 11:28 am #

    A soft link is like a redirect on the Internet. A hard link is a a duplicate entry in the directory for the same file. Soft links can break – the original file can disappear. Hard links can’t break.

  5. Jayce June 23, 2009 at 2:51 pm #

    Hehe… The penguin is so cute. 😀

  6. MonsterBuzz June 25, 2009 at 3:36 am #

    I’m always confuse when it’s come to changing file types. Thanks for the info. It helps me understanding Linux a lil’ bit.

  7. Miss46 October 23, 2009 at 1:21 am #

    What we hope to stress, therefore, is that talents and ideas are not a matter of age, but a matter of motivation and opportunity. ,

Leave a Reply