How Are Files and Directories Organized in Linux
In a Linux or Unix operating system, all files and directories are organized into an inverted tree structure starting with a root node. The topmost level of the file system starts with the root directory, and the system uses /
to represent the root directory. Under the root directory can be both directories and files, and each directory can contain subdirectory files. So repeated can form a huge file system. All devices in Linux (including hard disks, USB drives, etc.) are on the same level and are mounted in the directory. There is no concept of drives. (Unlike in Windows, we have C
D
E
, etc.)
1 Windows v. Linux
1.1 Directory separator
Linux uses a forward slash (/) as a directory separator, for example:
/home/user/documents/
.Windows uses a backslash () as a directory separator, for example:
C:\Users\User\Documents\
.
1.2 Root dictionary
- The Linux root directory is represented as a single forward slash
/
. - Each drive of Windows has its own root directory, for example:
C:\
,D:\
.
1.3 Driver
- All devices in Linux (including hard drives, USB drives, etc.) are on the same level and are mounted in directories. There is no concept of drives.
- Windows uses different drive letters for different devices, such as
C
,D
,E
, etc.
1.4 Dictionary structures
- Linux systems usually follow a “tree” structure, with a root directory and multiple subdirectories extending downward from the root directory, such as
/bin
,/etc
,/home
, etc. - The directory structure of the Windows system is not very regular, there are some system directories (such as
Windows
andProgram Files
) located on different drives, while user files are usually under theUsers
directory.
1.5 Case sensitive
- Linux is case-sensitive for file names, for example:
file.txt
andFile.txt
are two different files. - Windows does not case-sensitive filenames by default, but the file system itself supports case-sensitivity.
1.6 Specific dictionaries
- In Linux, some special directories under the root directory have specific purposes, for example:
/bin
: store system binary executable files./etc
: store system configuration files./home
: user home directory.
- Windows also has similar special directories, but the names and purposes may be different, such as:
C:\Windows
: Windows system files.C:\Program Files
: Installed applications.C:\Users
: User personal files and settings.
In general, the file and directory organization methods of Linux and Windows are different in many ways, and these differences largely reflect the design philosophy and historical evolution of the two operating systems.
2 Structures of files in Linux
Note: All following paths are absolute paths.
$ ls /
bin boot dev etc home init lib lib64 lost+found mnt opt proc root run sbin srv sys tmp usr var
/bin
bin
is the abbreviation of Binaries (binary files), this directory stores the most frequently used commands./boot
Stored here are some core files used when starting Linux, including some connection files and mirror files.
/dev
dev
is the abbreviation of Device (device). The external devices of Linux are stored in this directory. The way of accessing devices in Linux is the same as the way of accessing files./etc
etc
is the abbreviation of Etcetera (etc.), this directory is used to store all configuration files and subdirectories required for system management./home
The user’s home directory. In Linux, each user has its own directory. Generally, the directory name is named after the user’s account, such as alice, bob, and eve in the above figure.
/lib
lib
is the abbreviation of Library (library). This directory stores the most basic dynamic link shared library of the system, and its function is similar to the DLL file in Windows. Almost all applications need to use these shared libraries./lost+found
This directory is usually empty. When the system is shut down illegally, some files are stored here.
/media
The Linux system will automatically recognize some devices, such as U disk, CD-ROM, etc. After recognition, Linux will mount the recognized devices to this directory.
/mnt
The system provides this directory to allow users to temporarily mount other file systems. We can mount the CD-ROM on /mnt/, and then enter this directory to view the contents of the CD-ROM.
/opt
opt
is the abbreviation of optional (optional), which is the directory for installing additional software on the host. For example, if you install an ORACLE database, you can put it in this directory. Default is empty./proc
proc
is the abbreviation of Processes (process), /proc is a pseudo file system (that is, virtual file system), which stores a series of special files of the current kernel running state. This directory is a virtual directory. We can get system information by directly accessing this directory.The content of this directory is not on the hard disk but in the memory. We can also directly modify some files in it. For example, the following command can be used to block the ping command of the host so that others cannot ping your machine.
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
/root
This directory is the home directory of the system administrator, also known as the super authority.
/sbin
s
is the meaning of Super User, which is the abbreviation of Superuser Binaries (binary files of super users), and the system management program used by system administrators is stored here./selinux
This directory is unique to Redhat/CentOS. Selinux is a security mechanism, similar to the Windows firewall, but this mechanism is more complicated. This directory is for storing selinux-related files.
/srv
This directory stores some data that needs to be extracted after the service is started.
/sys
This is a big change in the Linux2.6 kernel. A new file system sysfs in the 2.6 kernel is installed in this directory.
The sysfs file system integrates the information of the following three file systems the proc file system for process information, the devfs file system for devices, and the devpts file system for pseudo-terminals.
The file system is a visual reflection of the kernel device tree. When a kernel object is created, corresponding files and directories are also created in the kernel object subsystem.
/tmp
tmp
is an abbreviation for temporary (temporary). This directory is used to store some temporary files./usr
usr
is the abbreviation of unix shared resources (shared resources), which is a very important directory. Many applications and files of users are placed in this directory, similar to the program files directory under windows./usr/bin
Applications used by system users.
/usr/sbin
Advanced hypervisors and system daemons used by superusers.
/usr/src
The directory where the kernel source code is placed by default.
/var
var
is the abbreviation of variable (variable). This directory stores things that are constantly expanding. Used to store logs and so on./run
run
is a temporary file system that stores information since the system was started. When the system restarts, the files in this directory should be deleted or cleared. If you have a /var/run directory on your system, it should point torun
.The following image shows a clear structure of Linux file system.
3 File types
In Linux, the principle holds that everything is a file. Let’s take a look at the various file types in Linux. You can use the ls -l
command to list all the files within a directory. Here I used ls -l
to list all the files under /etc
and | grep passwd
to filter the output, and finally got the detailed information of the file /etc/passwd
.
bowen@DESKTOP:/etc$ ls -l | grep passwd
-rw-r--r-- 1 root root 1009 Sep 21 09:45 passwd
The output is structured as follows:
-rw-r--r-- 1 root root 1009 Sep 21 09:45 passwd
[1 ][2][3 ][4 ] [5 ] [6 ]
- Permissions
- Links
- Owner
- Group
- Last modified
- File name
3.1 Permissions
-rw-r--r--
can be broken down into -
, rw-
, r--
, and r--
, which signify:
-
denotes the file type, which falls into categories such as:Regular file
-
- ASCII
- binary
- data
Directory
d
Link
l
Device
block
b
: diskcharacter
c
: keyboard, mouse, etc.
Sockets
s
- FIFO (pipe)
p
- FIFO (pipe)
rw-
indicates the privileges of the file owner, with “read” and “write” but excluding “execute”.The first
r--
indicates the privileges of the users within the group associated with this file, limited to “read” access.The second
r--
indicates the privileges of other users , also limited to “read” access.