Different types of files

The seven types of files are as follows, denoted with the character used by Linux to represent them:

Type

Explanation

-: Normal file

A regular file, containing text or bytes

d: Directory

A directory, which can contain other directories and regular files

l: Symlink

Symbolic link, used as a shortcut

s: Socket

A channel used for communication

c: Special file

Mostly used for device handlers

b: Block device

The type that represents storage hardware, such as disk partitions

p: Named pipe

Used between processes to talk to each other


Out of these seven file types, you will first encounter just the regular files (-) and the directories (d). Next, you will probably interact some more with symlinks (l), block devices (b), and special files (c). Very rarely will you use the last two: sockets (s) and named pipes (p).

A good place to encounter the most common file types is in /dev/. Let's use ls to see what it contains:

reader@ubuntu:/dev$ ls -l /dev/
total 0
crw-r--r-- 1 root root 10, 235 Jul 29 15:04 autofs
drwxr-xr-x 2 root root 280 Jul 29 15:04 block
drwxr-xr-x 2 root root 80 Jul 29 15:04 bsg
crw-rw---- 1 root disk 10, 234 Jul 29 15:04 btrfs-control
drwxr-xr-x 3 root root 60 Jul 29 15:04 bus
lrwxrwxrwx 1 root root 3 Jul 29 15:04 cdrom -> sr0
drwxr-xr-x 2 root root 3500 Jul 29 15:04 char
crw------- 1 root root 5, 1 Jul 29 15:04 console
lrwxrwxrwx 1 root root 11 Jul 29 15:04 core -> /proc/kcore
...<SNIPPED>:
brw-rw---- 1 root disk 8, 0 Jul 29 15:04 sda
brw-rw---- 1 root disk 8, 1 Jul 29 15:04 sda1
brw-rw---- 1 root disk 8, 2 Jul 29 15:04 sda2
crw-rw---- 1 root cdrom 21, 0 Jul 29 15:04 sg0
crw-rw---- 1 root disk 21, 1 Jul 29 15:04 sg1
drwxrwxrwt 2 root root 40 Jul 29 15:04 shm
crw------- 1 root root 10, 231 Jul 29 15:04 snapshot
drwxr-xr-x 3 root root 180 Jul 29 15:04 snd
brw-rw---- 1 root cdrom 11, 0 Jul 29 15:04 sr0
lrwxrwxrwx 1 root root 15 Jul 29 15:04 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Jul 29 15:04 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Jul 29 15:04 stdout -> /proc/self/fd/1
crw-rw-rw- 1 root tty 5, 0 Jul 29 17:58 tty
crw--w---- 1 root tty 4, 0 Jul 29 15:04 tty0
crw--w---- 1 root tty 4, 1 Jul 29 15:04 tty1
...<SNIPPED>:
reader@ubuntu:/dev$

As you saw from your output, /dev/ contains a lot of files, with most of the types as outlined above. Ironically, it does not contain the most common file type: regular files. However, because we have been interacting with regular files until now, you should have an idea about what they are (and otherwise the rest of the book will definitely give you an idea).

So, let's look at anything other than a regular file. Let's start with the most familiar: directories. Any line that starts with a d is a directory, and, if you're using SSH, will most probably be represented in a different color as well. Do not underestimate how important this visual aid is, as it will save you a lot of time when you're navigating a Linux machine. Remember, you can move into a directory by using either cd with a relative path or a fully qualified path, which always starts from the root of the filesystem.

Next, you will see files starting with the b. These files are used to represent block devices, the most common usage being a disk device or partition. Under most Linux distributions, disks are often called /dev/sda, /dev/sdb, and so on. Partitions on those disks are referred to with a number: /dev/sda1, /dev/sda2, and further. As you can see in the preceding output, our system has a single disk (only /dev/sda). That disk does, however, have two partitions: /dev/sda1 and /dev/sda2. Try using df -hT again, and you will notice /dev/sda2 mounted as the root filesystem (unless your virtual machine was configured differently, in which case it might be /dev/sda1 or even /dev/sda3).

Symlinks are often used on Linux. Look in the preceding output for the entry cdrom, which you will see starts with an l. The term cdrom has contextual meaning: it refers to the CD (or more probably, in a newer system, the DVD) drive. However, it is linked to the actual block device that handles the interaction, /dev/sr0, which starts with the b for block device. Using a symlink makes it easy to find the item you need (the disk drive) while still preserving the Linux configuration which calls the device handler sr0.

Finally, you should see a long list of files called tty. These are denoted by a c at the beginning of the line, indicating a special file. To keep it simple, you should consider a tty as a Terminal you use to connect to your Linux server. These are a kind of virtual device that Linux uses to allow interaction from the user with the system. Many virtual and physical devices use the special file handlers when they appear on your Linux filesystem.

This chapter introduced you to many commands. Perhaps you have gotten sick of typing everything already, perhaps not. In any case, we have some good news: Bash has something called autocomplete. It is something we did not want to introduce to early as to avoid confusion, but it is something that is used so extensively when working with a Linux system that we would be cheating you if we had not explained it.

It's actually pretty simple: if you hit the Tab key after the first part of a command (such as cd or ls), it will complete your command if it has a single choice, or if you hit Tab again, it will present you a list of options. Go to /, type cd, and press Tab twice to see this in action. Moving into the /home/ directory and pressing Tab once (after entering cd) will make it autocomplete with the only directory there is, saving you time!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.108.22