DEV Community

Cover image for The UNIX system
jmau111 πŸ¦„
jmau111 πŸ¦„

Posted on • Updated on

The UNIX system

The UNIX system is a major breakthrough that inspired many other operating systems. It's particularly visible in Linux and macOS.

You may even read the term "Unix-like systems".

The UNIX core philosophy has highlighted critical principles such as:

  • multi-tasking
  • multi-user
  • modularity
  • single responsibility
  • composition
  • portability
  • extensibility

Back in the 60's/70's, UNIX introduced the concept of "Making each program do one thing well", and this is still massively applied in programming.

N.B.: There are many other principles you might want to explore

Core components

The Kernel

The heart of UNIX is the kernel. It interacts with critical elements such as the hardware, the memory, or the filesystem.

It works with the highest level of permissions.

The shell

The shell intreprets the various commands and utilities to call the programs and execute the tasks.

The filesystem

The filesystem is composed of directories and files that contain data. It's a hierarchical tree.

A quick overview of the Unix Filesystem

Any dev is probably familiar with the UNIX file system. At least, you may have already seen something similar to the following:

/
β”œβ”€β”€ bin/
β”œβ”€β”€ dev/
β”œβ”€β”€ etc/
β”‚    └── passwd
β”œβ”€β”€ home/
β”œβ”€β”€ lib/
β”œβ”€β”€ mnt/
β”œβ”€β”€ root/
β”œβ”€β”€ proc/
β”œβ”€β”€ mnt/
β”œβ”€β”€ sbin/
β”œβ”€β”€ tmp/
└── usr/
     └── bin/
Enter fullscreen mode Exit fullscreen mode

Those directories contain other directories and files that are essentials for the server or the operating system.

While Linux distributions and other operating systems may have their specificities (e.g., special files and directories), the global structure will likely look like that.

It's called the directory tree and it's a typical multi-level UNIX hierarchy.

/home/ sweet home

Pretty straightforward, as it's the home folder for each user with, for example, special configurations.

/ vs. /root/

The top / is the system root directory. All folders and files are its descendants, including /home/. It's like C:\ in Windows.

/root/ is the home directory for the root user.

/usr/bin vs. /bin/ vs. /sbin/

The /usr/bin, for example, usually contains executable files that are not needed for booting or repairing the system but still used by the user. /usr/ is a read-only directory that contains applications like your favorite browser.

The /bin/ folder contains essential binaries such as cat or ls, which are also basic command lines you an use in the terminal. /sbin/ is for "system binaries" you cannot use without the necessary permissions, like maintenance utilities.

The privileged /etc/

/etc/ stores essentia configuration files and databases for the system. For example, the /etc/passwd file is one of the most critical ones. It contains very sensitive information about the users on the system (e.g., usernames, passwords, group ids, user ids, login shell, home directory, and more).

Only the root user is supposed to edit such file.

/dev/ vs. /proc/

/dev/ contains non-standard files for devices and virtual devices (also known as pseudo-devices), and /proc/ contains kernel processes.

Disks will be represented as file, for example, /dev/sda and its partitions could be /dev/sda1, /dev/sda2, /dev/sda3, and so on.

What is /dev/null?

Administrators and dev may partially or completely redirect the output to /dev/null to clear it, so they can skip unnecessary lines like "Permission denied", for example, in global scans with grep.

Mounting /mnt/

/mnt/ is for mount points. You typically use it to mount temporary file system like external storages or USB drives.

Roughly speaking, mounting means attaching another filesystem to the current filesystem.

/var/

/var/ is for writeable contents such as logs.

/tmp/

/tmp/ is for temporary files. Such files can be deleted at any moment, for example, when the system restarts.

To preserve files between reboots, you may use the /var/tmp/ directory instead.

What is vi?

Vi is the standard editor for UNIX systems. You can type vi NAME_OF_YOUR_FILE in the terminal to create/edit a file within a UNIX system and ESC followed by :wq to save your work.

What are processes?

Processes are created when you execute a program. Even a simple ls -lah creates a process. Processes are also known as "instances" of programs.

UNIX systems involve normal processes and background processes. To start a background process, you can use &:

./myexecutable &
Enter fullscreen mode Exit fullscreen mode

To list processes, you can use ps:

ps -e | less
Enter fullscreen mode Exit fullscreen mode

You'll get info such as process ids and many more. To kill a specific process, you can use kill -9 PROCESS_ID.

The concept of pipes, stdin, and stdout

Pipes allow passing or holding data between processes. In Unix-like systems, it will be represented as a temporary file.

Pipes can also be considered as redirections. The result (output or stdout) of a command is used as input (stdin) for another command for further processing.

The syntax used for piping several commands is |. This is a one way flow and a left-to-right direction:

cat data.txt | grep "bootleg"
Enter fullscreen mode Exit fullscreen mode

It's a powerful built-in mechanism to chain commands and run complex procedures.

What are UNIX sockets?

UNIX domain sockets (or IPC sockets) are connections between processes on the same host operating system (~ the same machine, no TCP overhead involved).

It uses the local filesystem to create an IPC channel (~ independent process not affected by other processes) between processes.

Unlike pipes, it's a two-way communication.

Why people say "everything is a file" in UNIX

The assertion "everything is a file" is convenient to describe advanced abstract concepts. Many internal features such as sockets will appear in the filesystem (e.g., mysql.sock) even if they don't contain any data.

It is how the system represents streams of bytes exchanged between programs.

However, in practice, not everything is actually file, for example, system processes, but maybe saying that "everything behaves as a file" would be more accurate.

Misknown commands or options you might want to use

Command Description
df –kh DIR/ disk usage in the given directory in human readable format
head -7 myfile.txt display first 7 lines of a file
diff -w file1.txt file2.txt display diff between files but ignore whitespaces
cd - go quickly to the previous directory
free -h memory usage in human readable format
uname -r display kernel info
!111 execute again the command with the number 111 in the history list

Note that some Unix-like systems may have different commands for the same purpose.

Top comments (4)

Collapse
 
phlash profile image
Phil Ashby

Nice introduction with a smattering of practical notes! As well as Eric's excellent book, there is of course the obligatory Wikipedia page (and many useful linked pages):

en.wikipedia.org/wiki/Unix_philosophy

Remember to come up for air regularly when getting lost in the past...

Collapse
 
ben profile image
Ben Halpern

Excellent choice of cover pic πŸ˜†

Collapse
 
jmau111 profile image
jmau111 πŸ¦„

πŸ¦–

Collapse
 
snelson1 profile image
Sophia Nelson

Nice post