Linux basics: Definition and Directory Structure

Long Nguyen
7 min readFeb 7, 2020

--

https://imgur.com/gallery/cJN9YJ9

My notes while learning about Linux and its directory structure

I. About Linux

What is Linux

  • Linux is the kernel (core) of an operating system (OS).

What is GNU/Linux OS

  • It is usually called just Linux OS. It is a very popular, free & open-source family of UNIX-like OS with a Linux kernel and GNU software packages, which were developed in the GNU Project.
  • It can run/interact on a variety of hardware (phones, computers, etc)

What is a kernel

  • The lowest level software that interfaces with hardware.

What is Unix

  • The first multi-user, multi-tasking & portable OS to different hardware by AT&T. It is a proprietary OS — not open-source and many OS branch off UNIX — one of them is Linux.

What’s the difference between Linux and Unix

  • Essentially the same. The only difference is that one is open-source (Linux) and the other is not.

What are Distros

  • Also called Linux distributions — they are different versions of Linux OS (e.g Debian, Ubuntu, Red Hat).
  • Each version is a combination of different software packages or extensions with the Linux kernel.

What is an operating system (OS)

  • A software that sits between software and hardware, manages the communication between them and also manages system’s hardware and resources

What is Linux consisted of

  • Kernel is the main one: interacting directly with the underlying hardware
  • System libraries: provide functions for app to access kernel
  • Everything on top of kernel could be implemented differently. That’s why we have different distributions
Source

What is a Shell

  • Executable binary file acting as an interface to kernel of a unix-like OS — takes commands from user and execute kernel’s functions. Shell lets you tell the computer what to do.
  • 2 types of shells, each has subcategories: Bourne shell and C shell.
Source

What is Bash

  • A type of Shell — one of the most popular ones. Bash stands for Bourne Again shell.
Source

II. Linux directory structure

Before diving into each directory, here are some concepts you should know.

What are binaries

  • They are programs or executable files that the computer can translate to machine code.

What is a driver

  • A software that allows the OS to communicate with the hard drive. It is usually written by the same organisation that makes the hard drive.
Source

What is a filesystem

  • The system that structures data and controls how information is written and retrieved from the disk. Without it, you can’t use any file-related operations.
  1. Root directory ("/")
  • Everything on your Linux system is located under "/" directory.

2. Essential user binaries ("/bin")

  • Contains programs that must be present when the system is mounted (started) in single-user mode, to provide basic functionality for the user (commands like cp, cd, …).

3. Static boot files ("/boot")

  • Contains files needed to boot the system — boot loader files, linux kernels, etc.

4. Device files ("/dev")

  • Contains special files (non standard files or virtual files, not physically on disk) that represent devices on machine (cpu, cd, etc).
  • "/dev/null" is a special device that produces no output and automatically discards all input. For example, when you pipe the output of a command to /dev/null, you discard it: ls -l > /dev/null.
  • "/dev/zero" contains an infinite sequence of 0.
  • "/dev/random" contains an infinite sequence of random value.

5. Config files ("/etc")

  • Contains system-wide config files (user specific config should be in user’s home directory).

6. Home folders ("/home")

  • Contains folder for each user — where user data and user specific config files are.
  • E.g: user Bob has home folder located at “/home/bob”.

7. Essential shared libraries ("/lib")

  • Contains libraries needed by the essential binaries in "/bin" and "/sbin".
  • Libraries needed by "/usr/bin" are located in "/usr/lib".

8. Recovered files ("/lost+found") — not common

  • If file system crashes, any corrupted files will be placed here. On the next boot you can try to recover.

9. Optional packages ("/opt")

  • Contains subdirectories for optional, add-on monolithic software packages, like third party software (java, etc) — meaning these software could be installed in one place, no need for dependencies from/lib or other directories.

10. Root home ("/root")

  • Home directory of root user — this is a special case — NOT located in "/home/root", but under system root directory "/".

11. System admin binaries ("/sbin")

  • Contains essential binaries that are intended to be run by root user — normal user has no access to this.

12. Temporary files ("/tmp")

  • Where applications store temporary files — they are deleted when system is restarted.

13. User binaries & read-only data ("/usr")

  • Stands for Universal System Resources
  • Contains applications and files used by USERS (rather than by the SYSTEM) — such as user commands (e.g: pip, python, npm).
  • Non essential binaries are in "/usr/bin" rather than "/bin".
  • Non essential admin binaries are in "/usr/sbin" rather than "/sbin".
  • Libraries for non essential binaries are in "/usr/lib" rather than "/lib".
  • Locally compiled applications will be installed in "/usr/local" by default so that they don’t mess up the rest of the system. E.g: you have your source code, then you compile your source code, the binary goes into "/usr/local/bin".

14. Variable data files ("/var")

  • Where system programs store runtime information like system logging, user tracking, caches, or any other files that system programs create and manage
  • E.g: "/var/cache", "/var/cache/man", "/var/cache/fonts", "/var/log", "/var/www".
  • Files in here are NOT automatically cleaned so this could be a good place for storing information about system behaviour.
  • The system must be able to write to this during operation.

15. Removable media ("/media") — not common

  • Contains subdirectories where removable media devices inserted into the computer are mounted.

16. Temporary Mount points ("/mnt")

  • When mounting certain system files from a physical device on to the Linux machine, they will be in one of the subdirectories in "/mnt".
  • Usually for media/removable devices, they will be mounted to "/media", whereas "/mnt" is usually for partitioned hard drive.

17. Kernel & process files ("/proc")

  • Similar to "/dev", it doesn’t contain standard files —it contains information about running process with a particular pid (process id) in a special type of file.

18. Service data ("/srv") — not common

  • Contains data for services provided by the system or server specific services data.
  • Eg: if you want to serve a static website, your static html file should be in here.

19. Application state files ( "/run")

  • Contains system’s run-time variable data since last boot — such as logged in user, running daemons.

Some special files that you should know

1. "/etc/fstab"

  • Contains information about what devices are usually mounted where, using which options on booting.
  • The last number is the priority of which filesystem should be check first — only one file is marked as 1 which is the root partition — which should be checked first. The rest could be all 2.
  • On the dump column, 0 means don’t dump on booting, 1 means dump.

2. "/etc/mtab"

  • A list of currently mounted filesystems in the file, same with "/proc/mounts".
  • When you do mount and then enter, you basically do cat /etc/mtab .

3. "/proc/meminfo"

  • Contains memory usage information.

What are differences between all the /bins directories

  • "/sbin" — super important binaries, used by system admin.
  • "/bin" — system wide binaries for very basic commands.
  • "/usr/bin"— other binaries for normal user; usually external stuffs like python, pip or using dependencies from "/usr/lib".
  • "/usr/local/bin"— usually compiled from source code, executable applications local to this system.
  • "/opt"— add-on third party, things that could be installed all in 1 directory, monolithic.

III. Resources

Linux vs UNIX

Linux OS

What is GNU/Linux

What is Bash

Unix — what is Shell

What is Bash

Linux file system structure

Linux FHS

/usr/bin & /usr/local/bin

Beginner’s Guide to Bash Terminal (highly recommend this one)

Until next time. Happy reading!

--

--