Introduction to Linux Files and Directories

by Wire Tech
Linux Files and Directories

Today we are going to discuss about Linux Files and Directories. In computer systems, the smallest unit of data archiving is the file. Texts are stored in text files, with different formats and types: ASCII text files, Rich Text Format (RTF), and the more advanced Word formats doc/docx.

Welcome back to Linux System Administration Series, Earlier we have discussed about

Learn to Install RedHat Enterprise Linux 9 – Step by Step Guide

What is Linux Shell?

Use Wildcard Expansion & History in Bash Shell Command

Bash Shell Command Auto-Completion & Shell Expansion

Tables of data and numeric statistics are stored in Excel files. Databases are physically stored in Database files (.dbf). Media (music and video) are also stored on computer systems as files. Executable programs, libraries, start menu and desktop shortcuts are all represented in computer as files of various types.

In UNIX and Linux, the above still apply. Actually, there is an important rule that you should know when starting to learn UNIX and Linux. The golden rule is: Everything in UNIX (and Linux as well) is a file. How?! And how to work with files of different types? This is going to be the subject of the article you are about to read.

Everything in UNIX/Linux is a File

Yes, everything… everything in Linux is represented as a file. Text files, links, directories (the equivalent of Windows folders in UNIX and Linux), executable binaries, even hardware: disks, CD-ROM/DVD, printers, tape drives, and terminals… all are represented in the Linux operating systems as files of different types.

So, it is must for you as a Linux system administrator to know how to deal with files: how to create files, list files, check permissions of files, execute a binary or script file, create a link, copy, rename, move, and remove files. You will learn all these skills and more in this article and in next articles in this series.

Types of Files

Regular Files

The most used and most common among all file types are regular files. Regular files include:

  • User’s Text files.
  • Shell Script files.
  • Binary executable files.
  • Library files.
  • System Configuration files.

Directories in Linux Files and Directories

The second most commonly-used type of files are directories. Directories are special type of files used as containers for files and other child directories as well. It is used to group together similar files and directories that are related to the same subject, project, application, etc.

Its also serve as mount points for filesystems. i.e. filesystems are represented to UNIX and Linux users as directories.

Symbolic Links in Linux Files and Directories

A symbolic link is a shortcut to a file or directory.

Block Device Files in Linux Files and Directories

Special type of files used to represent the Block Devices to the Linux operating system. Block devices are mainly storage devices like hard desk drives, logical volumes, CD-ROMs, and DVD.

Character Device Files in Linux Files and Directories

Another special type of files used to represent Character Devices to Linux. Character (stream) devices transfer data one character at a time.

Examples of character devices include keyboards, printers, and terminals. Some common character device files are /dev/null, /dev/zero, and /dev/random (We will discuss the use of these files in a later article).

How to Know a File Type in Linux Files and Directories?

Each of the above file types has its characteristics, and the way to deal with. Before processing, manipulating, or working with a file, we need first to know the file type. In this section, we are going to learn how to know the type of a given file.

Using the ls Command with Long Listing Option

One way to determine the type of a file is to use the ls command with the -l option. The ls command is used to list contents of one or more directories.

When used with -l option, its output comes in the long listing format, which gives many information about the file like: file type, permissions on the file, the owner of the file, the group it belongs to, the file size in bytes, and the date and time the file was last modified.

Consider the following output of the command

ls -l /etc :
Linux Files and Directories

Lines of output starting with hyphen “ – “ represent regular files. e.g. the files system-release-cpe , vconsole.conf , virc , and xattr.conf are all regular files, because their lines in the long listing output of ls command start with hyphen “ – “.

Lines of output starting with letter d represent directories. So, terminfo , tmpfiles.d , udev , X11 , and yum are all of type directory.

Lines starting with letter l (small L) represent symbolic links (shortcuts) to other files or directories.

For example, the file system-release is a symbolic link that points to another file called redhat-release , while yum.conf is another shortcut that points to the file dnf/dnf.conf .

Now, consider the following output of ls -l command listing part of the content of the /dev directory:

Linux Files and Directories

In the output, we can see lines starting with letter c . These lines represent Character device files. For example, /dev/console , /dev/null , /dev/random , and /dev/tty are character device files.

Other lines starting with letter b represent the other type of device files: Block device files. Some examples of this type are /dev/sda (SCSI or SATA hard drive) , /dev/sda1 (first partition under disk sda) , and /dev/sr0 (cdrom).

Using the file Command

Another way to determine the type of a given file is to use the file command. The file command can take one or more files as arguments. It returns the type of each provided (argument) file and print the results to the terminal.

Let’s see some examples. The following is the list of files under /etc directory:

Linux Files and Directories

At your shell, execute the following command:

file rsyslog.conf
Linux Files and Directories

The output shows that the rsyslog.conf is an ASCII text regular file.

Now execute the following:

file yum X11
Linux Files and Directories

Here we have passed two files (actually directories) as arguments to the file command: yum and X11. The command output shows that both yum and X11 are directories.

Now, let’s use the wildcard expansion with the file command. Execute the following at your shell:

file sys*

This should get types of all files starting with sys .

Linux Files and Directories

Six files matched the sys* pattern: sysconfig , sysctl.conf , sysctl.d , systemd , system-release , system-release-cpe. Three of the files are found to be directories, two regular ASCII text files, and one symbolic link to redhat-release file.

Finally, we need to see how the file command returns when the argument is a block device file, or character device file.

Back to the examples we mentioned above: Files /dev/console , /dev/null , /dev/random , and /dev/tty are character device files.

Let’s pass them as arguments and see the output:

Linux Files and Directories

Similarly, the block device files /dev/sda , /dev/sda1  , and /dev/sr0 :

image 43

Using the test Command

A third method to determine the type of a file is to use the test command. Th test command checks file types and compares value. It is usually used with if condition statements, and in while loops as the condition that decides:

  • whether one or commands to be executed or not (with if).
  • whether a while loop should continue and execute its body one more time or exit.

The use of test command and its conditions will be discussed in detail in our Shell Scripting series.

Conclusion

In this article, we had an introduction to Linux files and directories in Linux. We learned the different file types, and how to determine the type of a given file.

In the next article, we will continue with linux files and directories, and see how to create, copy, move, rename, and delete files and directories. There will be much to learn in next articles, so don’t miss one.

You may also like

Unlock the Power of Technology with Tech-Wire: The Ultimate Resource for Computing, Cybersecurity, and Mobile Technology Insights

Copyright @2023 All Right Reserved