A Beginner’s Guide to the Linux File System

0
13416

98-100_Linux Perspective June 2015

This is an article targeted at Linux beginners. It helps readers get an understanding of the Linux file system through a few experiments that they, too, could try out.

In Linux, everything is treated as a file. Besides data or program files, the hardware devices are also treated as files. Hard disks (and their partitions), CD-ROM drives, keyboards, mice, terminals, etc, are all files. In the Linux file system, every hardware device is represented by a unique file name. These files are often called device files and are stored under the directory /dev.
The following experiments demonstrate the concept of device files in Linux. I have tried these on my Ubuntu 14.04 desktop.

Experimenting with the device file representing the mouse
The device file that represents the mouse is /dev/psaux (i.e., the file psaux in the /dev directory).
Step 1: Open a terminal window.
Step 2: Type the following command at the prompt:

sudo cat /dev/psaux

Step 3: Give the administrator password and the system waits for the input (in this case the cat command expects input from the file /dev/psaux, which is actually the mouse itself).
Step 4: Now move the mouse, and watch the garbage characters on the screen.

Experimenting with the device file representing the terminal
The terminal window, which we open using the gnome-terminal program, is actually a software emulation of a hardware device called Terminal. That is, this terminal also has an associated device file name in the /dev directory.
Step 1: Open two terminal windows.
Step 2: Find the file name associated with each terminal by typing the following command in each terminal:

tty

Let the file names be /dev/pts/2 and /dev/pts/3 for the first and second terminals, respectively.
Step 3: From the first terminal (in this example, the file name of which is /dev/pts/2), type the following command:

sudo cat > /dev/pts/3

Give the password and type anything (/dev/pts/3 is the file name associated with the second terminal).

Step 4: Watch the characters that were typed in the first terminal displayed in the second terminal.
Step 5: Try Step 3 from the second terminal cating to the file name of the first terminal.

Figure 1
Figure 1: Concatenating the file /dev/psaux which represents the mouse
Figure 2
Figure 2: Concatenating to the file /dev/pts/3 which represents the second terminal

Identifying the type of a file
As the Linux file system has many files that are of different types, it’s important to identify the type of a particular file. The file type describes what kind of information it contains or its file format. For example, the file created using gedit is an ASCII Text file. The file created using LibreOffice Writer is an Open Document Text file. The one created using LibreOffice Impress is an Open Document Presentation file. Normally, the files of these applications have a three letter extension to the file name, and this extension reveals the type of that file. We know the extensions odt (Open Document Text), odp (Open Document Presentation), ods (Open Document Spreadsheet), etc. We are also quite familiar with pdf files (pdf stands for Portable Document File). Even a directory or folder is a special file.
Here are two methods for identifying the type of a file.
Method 1: The first character in the long listing of a file denotes its file type. To get the long listing, use the command ls with the -l option, i.e., ls -l
The first character in the long list and the associated file type:
– Ordinary file
d Directory file
c Character special device files (e.g., keyboard, terminal, etc)
b Block special device files (e.g., hard disk and its partitions)
l Symbolic link files
p Named pipes
s Semaphore
The following are a few commands and their outputs, which I tried in my system. Replace the file names/folder names in these examples with those in your system:

Ordinary/regular file:

ls -l ivn.odt
-rw-rw-r-- 1 ctstaff ctstaff 61110 Nov 27 14:17 ivn.odt

Directory file:

ls -ld CFRD
drwx------ 16 ctstaff ctstaff 4096 Nov 10 13:55 CFRD

Symbolic link file:

ls -l lnfile
lrwxrwxrwx 1 ctstaff ctstaff 5 Nov 10 15:40 lnfile -> sqr.c

Block special device file:

ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 Nov 30 13:21 /dev/sda

Character special device file:

ls -l /dev/tty1
crw-rw---- 1 root tty 4, 1 Nov 30 13:21 /dev/tty1

Character special device file:

ls -l /dev/psaux
crw------- 1 root root 10, 1 Nov 30 13:21 /dev/psaux

Character special device file:

ls -l /dev/pts/0
crw--w---- 1 root tty 136, 0 Nov 30 13:26 /dev/pts/0

Here, the file type Ordinary file or Regular file denotes all the data files (those created by applications such as LibreOffice Writer, Impress, gedit, etc) as well as executables (programs). Thus, by long listing these files, we cannot distinguish between them. The best way to identify the exact file type is to use the command file. Method 2 illustrates this command.

Figure 3
Figure 3: Displaying the concatenated lines of text on the second terminal

Method 2: The file utility determines the file type. Linux does not use extensions to determine the file type. Hence, this command is the best option to determine the file type. The following are a few examples I tried in my Linux system. Replace the file names in these examples with those in your system:

file j.c

j.c: C source, ASCII text

file index.jpeg

index.jpeg: JPEG image data, JFIF standard 1.01

file SysADM.pdf

SysADM.pdf: PDF document, version 1.4

file SysADM.odp

SysADM.odp: OpenDocument Presentation

file lnfile
lnfile: symbolic link to `sqr.c’

file /dev/sda
/dev/sda: block special

file /dev/tty1

/dev/tty1: character special

file /bin/grep

/bin/grep: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24
BuildID[sha1]=c51ad343b8f16ec239e1a490d06fa9a276f0aecc, stripped

Use the -s option with the file command to read the special files that are block or character special files. This is useful for determining the file system types of partitions that are block special files. Here are a few examples I worked out in my Linux system. Replace the file names in these examples with those in your system.

file -s /dev/sda
/dev/sda: x86 boot sector

file -s /dev/sda5

/dev/sda5: Linux rev 1.0 ext4 filesystem data, UUID=e15aa1d4-8676-49c7-bea2-f001c7390f30 (needs journal recovery) (extents) (huge files)

file -s /dev/psaux

(Now move the mouse)
/dev/psaux: ISO-8859 text, with no line terminators

Note: /dev/sda is the file name representing the first SATA hard disk and /dev/sda5 is a partition on this hard disk.

LEAVE A REPLY

Please enter your comment!
Please enter your name here