Directories in Linux

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

In this article, you will learn all there is to the directories in Linux-based operating systems. We will be going through each of the directories in the root directory and also looking at what they contain. We will also be looking at some miscellaneous directories important to the system. In the end, we will briefly look into the commands that help in managing these directories.

What is a directory?

A directory is a folder. With that said, we could finish up this section. I mean there is literally nobody on earth who would not know what a folder is, both literally and in operating systems.

Just like Windows and macOS have folders, Linux has directories. A directory or folder contains different files or even subdirectories.

Different files in the Linux file system

Before we explore directories more, let’s take a moment and have an overview of the main file types in the Linux file system.

1. Regular files

This type is the most common file type in the Linux file system. regular files contain program instructions human-readable text and ASCII characters.

Some examples of regular files are simple text files PDF files zipped or compressed files binary files or other files like “.png”,”.mp4”, “jpeg”, “.gif”, and many more.

2. Special files

Special files are the files that represent physical devices for any input and output devices like printers, CD drives, keyboards, mice, etc.

3. Directories

directory basically means folder and works just like the folders that are present in the Windows operating system. a directory is a special type of file that stores both regular and special files in a hierarchical order. this hierarchy starts from the root directory which is denoted by a slash (/).

let us take a look at the hierarchy of the directories in the Linux file system. if I navigate to the root directory using the “cd ..” command (which we will learn later) and use the LS command (which we shall learn later) to list the directories in the root directory I will get the following output:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

 

 

 

directories under root directory

All of the files that are in dark blue color are the already prepackaged directories, and the directories shown in other different colors Are the directories I created later on.

Our topic of interest in this article is directories, so let us forget the remaining 2 and focus on directories

Anyway, the folders which are in the reboot directory are:

1. “root” directory

2. “/dev” directory

3. “/boot” directory

4. “/usr” directory

5. “/var” directory

6. “/etc” directory

7. “/home” directory

8. “/opt” directory

9. “/media” directory

10. “/proc” directory

11. “/bin” directory

12. “/lib” directory

13. “/mnt” directory

Let us look at each of the directories in detail

1. Linux root directory

To root directory is exactly like the C folder on windows systems. The root directory is denoted by a slash “/”, that is why any path on Linux starts with a “/”, just like most paths on windows start with “C:\”.

You must also note that Linux does not have any other drivers like windows has D, E, and F. The root directory contains all of the programs and applications of the machine.

2. Linux bin directory

The bin directory contains all 0f the essential user essential binaries or programs that are needed when the system is mounted in single-user mode. Important system programs and utilities such as the bash shell are located in the “/bin” directory whereas applications like Firefox and LibreOffice are stored in the “/usr/bin” directory.

3. Linux boot directory

This directory contains all the files needed to boot the system like boot loader’s files, Linux kernels, the set of vmlinuz, initrd, system map, GRUB, etc. This is one of the most important directories because it conta9ns all of the files the operating system needs for it to boot. However, the configuration files of the boot loader aren’t stored here (they are in the “/etc” directory)

4. Linux cdrom directory

The cdrom directory is not part of the file hierarchy system (FSH), nonetheless, you will still find it on most Linux-based operating systems. This directory is a temporary location for CD-ROMs inserted into the system. Though the more defined and standard location for temporary media is inside the “/media” directory.

5. Linux dev directory

The dev directory contains a number of special files that represent different devices, these are technically not files, but at least appear to be. For example, the sda file in the dev directory represents the SATA drivers.

If you want to partition the SATA drive in the system, you can start a partition and tell it to edit “/dev/sda”.

The dev directory apart from containing the folders and files related to devices also contains pseudo-devices, which are virtual devices that actually don’t correspond to the hardware. Like the file /dev/random produces random numbers and /dev/null (a weird device) produces np output and automatically deletes all input.

6. Linux etc directory

The etc directory contains the configuration files of the system. These configuration files can generally be edited by hand in a text editor. It must be noted that the etc directory contains only system-wide configuration files and the user configuration files are stored in the user’s home directory.

7. Linux home directory

Home, sweet home, this is where all the pieces of information about the users in the computer are stored. This directory contains a home folder for every user on the PC. It contains all user data and user-specific configuration files.

Each user can access and modify their own home folder and must have elevated permissions (or sudo privileges) if they wish to modify other user files on the system.

8. Linux lib directory

This directory contains all the libraries needed by the essential binaries in the directories “/bin” and “/sbin”.
We can also find the kernel modules and shared files needed for the boot operation of the system in the lib directory. The kernel modules can be found in the subdirectory “/lib/modules/<kernelversion>” and the libraries needed by the binaries in the path “/usr/bin” are located in “/usr/lib”

9. Linux lost + found directory

The lost + found directory contains corrupted files. When a file system crashes, the file system runs a check at the next boot, since the corrupted files are stored in the lost + found directory, you can attempt to recover as much data as possible.

10. Linux media directory

This directory contains the subdirectories where removable media devices which are inserted into the computer are mounted. For example, when you insert a CD into your computer with a Linux-based operating system, a directory will automatically be created in the media directory, where you can access the contents of the CD.

11. Linux mnt directory

The mount or mnt directory is where system administrators mounted temporary file systems while using them. The process of making a file system available to the operating system is called mounting.

Note that some Linux distributions like Debian allocate /floppy and /cdrom as mount points whereas other distros like RHEL put them in /mnt/floppy and /mnt/cdrom.

12. Linux opt directory

The optional or opt directory contains the subdirectories for optional software packages. It is the directory where usually manually installed software and add-on packages from vendors are stored.

This directory is commonly used by propriety software that doesn’t obey the standard hierarchy of the filesystem. For example, some arbitrary proprietary program might dump its files in the path “/opt/application” when you install it.

13. Linux proc directory

The proc directory is very much similar to the dev directory as it does not contain standard files. This directory contains special files that represent the system process and information.

14. Linux run directory

This directory gives applications a standard place to store transient files they require (like process ID and sockets). These files cannot be stored in the directory /tmp because tmp might delete them, since tmp stores only temporary files for a specif time period.

15. Linux sbin directory

The sbin directory is very similar to the bin directory. This directory contains the essential binaries that are intended to be run either by the system administrator or root user.

16. SELinux directory

If you use Security enhanced Linux for security, the special files used by SE Linux are stored in the directory “/selinux”

17. Linux srv directory

The service or srv directory contains the data for the services provided by the system. For example, if you are using the Apache HTTP server to serve a website, you would store the files related to the website in a subdirectory inside the “/srv” directory.

18. Linux temp directory

The temp directory stores temporary files of applications and programs. For example, say you are writing a document in LibreOffice, it automatically saves a temporary copy in the temp directory. This directory is usually emptied when you reboot the system.

19. Linux usr directory

The user directory contains the applications and files that are used by the users in the system. You must not that only those files used by the user will be stored here and not the ones used by the system.

This directory is one of the most important directories as it contains all of the user binaries, files, libraries, documentation, applications and so many more.

The applications installed in this directory are considered non-essential for the basic operation of the system. The usr directory also contains other directories like architecture-independent files like graphics are located in the “/usr/share” directory.

20. Linux var directory

The var directory is the writable counterpart to the usr directory. Let us look at the different subdirectories in the var directory and what they store.

1. /var/crash

This subdirectory holds information about processes that have crashed

2. /var/mail

This subdirectory Contains user mailbox files

3. /var/log

This subdirectory contains log files for both the system and other different applications.

4. /var/lib

This subdirectory contains dynamic data files/libraries.

5. /var/spool

This subdirectory spool data of applications

Now that you have seen all of the main useful directories in the root directory, let us take a brief look at how to manage directories in Linux using the terminal.

Managing directories from the terminal

We all know that there are 2 ways to do almost anything in Linux, one is the GUI and the other is the terminal. In this section, we are going to see how to manage directories using different commands in the terminal like pwd, ls, mkdir, rmdir and ch.

You may ask why use the terminal when the GUI is good enough? Well, Linux is not about being ‘good enough and the terminal makes management of the Linux filesystem so much more faster, simple and efficient.

If you really want to get a really good and full-blown idea of how these commands are a 100% better than the GUI, you can read my article on managing the filesystem of Linux through the kernel.

Let us look at the commands that help in managing directories in Linux:

Linux PWD command

The PWD command is an abbreviation for “Present Working Directory”. This command is very straightforward and simple, it prints out the path of your current directory, starting all the way from the root directory.

pwd command

Linux CD command

The “cd” command is used to navigate through the files. Just like in GUI, you press the directory you want to go to, cd does the same, but in the terminal. The cd command is the abbreviation for ‘Change Directory’.

If you simply type ‘cd’, you will go back to the home directory, no matter where you are.

 

cd command in linux

If you press “cd ..”, this command will take you to the previous directory or parent directory. You can go all the way back to the root directory using this command.

linux cd command

 

 

Ok, we have seen how to go to the home directory and how to go back, but how do you go forward, well once you are in a directory, you can simply type the command “cd <subdirectory>”. This command will take you to the subdirectory you have specified.

cd command

If you look at the above method of going to the directory ‘cars’ from the root directory, it is a very long process, I had to use the cd command 4 times. The previous method was on par with the GUI, where you go to the directory and press on the subdirectory.

However, remember when I said the terminal is all about efficiency? Well, there is a way you can even go 100 directories deep from the home directory just by using 1 single cd command! You can do so by using the following syntax: “cd <directory path>” If you specify the path of the directory to cd, it will take you directly there with not additional stops in between.

cd command linux

Take that GUI!!

Linux MKDIR command

We learned how to create file, it is only fair to learn how to make directories too. To make directories we uses the mkdir command with the following syntax: mkdir <name of directory>. The mkdir command is an abbreviation for Make Directory.

To make a directory in a specific parent directory, you must first be there.

mkdir command

 

If you feel lazy to go to that specific directory to create a subdirectory in it, you can create a directory anywhere, just by writing its path, nomatter where you are (in the computer).

mkdir linux

In the above output, I created the directory “BMW_pictures” in the path “/home/gopikiran/Desktop/cars” even though I was in the path “/home/gopikiran/Desktop/DataFlair” (different directories).

Linux RMDIR command

Need to remove a created directory, rmdir does just that. Rmdir is the abbreviation for Remove Directory. It functions exactly like the mkdir command.

If you simply used the syntax: rmdir <directory name>, you will remove the directory with that specific name, only if it is there in the directory you are currently in.

rmdir command

If you use the syntax: rmdir <path of directory>, you will remove that specified directory regardless of which directory you are currently in.

 

linux rmdir command

In the above output, I removed the directory “BMW_pictures” in the path “/home/gopikiran/Desktop/cars” even though I was in the path “/home/gopikiran/Desktop/DataFlair” (different directories).

Linux LS command

Ok, now you can check your present working directory and navigate through the files. But what if you need to list out the files or subdirectories present in a directory? That is exactly where the ls command comes to help. The ls command stands for “list”

To list the contents of a directory, you can simply use the ls command:

 

ls command

The ls command is a very powerful command with so many options that the GUI can barely even touch! If you want to see the full usage of the LS command along with its different options, you can read my article on the LS command.

Exploring more files!

Before we call it a day, let us look at some more files and what they store in it that are useful to th system. We know by now that everything in Linux is a file and the Linux operating system is a very complex filesystem. Let us at least try to scratch the surface of this filesystem by taking a brief look at some of the important files in the Linux file system:

System Configuration Files:

1. /etc/bashrc

This file contains system defaults and aliases.

2. /etc/crontab

This file contains the files related to crontab (like cronjobs)

3. /etc/exports

This file contains the information on the file system available on the network.

4. /etc/fstab

This file contains the information about the Disk Drive and its mount point.

5. /etc/group

This file contains the information of the Security Group.

6. /etc/grub.conf

This file contains the grub bootloader configuration file.

7. /etc/init.d

This file contains the service startup script.

8. /etc/lilo.conf

This file contains the configuration file of the lilo bootloader.

9. /etc/hosts

This file contains the Information of IP and corresponding hostnames.

10. /etc/hosts.allow

This file contains a list of hosts allowed to access services on the local machine.

11. /etc/host.deny

This file contains the list of hosts denied to access services on the local machine.

12. /etc/inittab

This file contains the INIT process and their interaction at the various run level.

13. /etc/modules.conf

This file contains the configuration files for the system modules.

14. /etc/motd

This file contains the message of the day.

15. /etc/passwd

This file contains the username, password of the system, and users in a shadow file.

16. /etc/printcap

This file contains printer Information.

17. /etc/profile

This file contains bash shell defaults.

18. /etc/profile.d

This file contains other scripts like application scripts, executed after login.

19. /etc/rc.d

This file avoids script duplication.

20. /etc/rc.d/init.d

This file runs Level Initialisation Script.

21. /etc/security

This file contains the name of terminals where root login is possible.

22. /etc/X11

This file contains all the conf files for the X-window System.

23. /etc/termcap

This file defines the behavior of different types of terminals.

24. /etc/skel

This file initiates a new user home directory.

Linux kernel file:

1. /boot/vmlinux

This file contains the Linux kernel file.

Version information file:

1. /version

This file contains the information about the Linux version.

Device Files:

1. /dev/hda

This file contains the device file for the first IDE HDD.

2. /dev/hdc

This file contains the pseudo-device that output garbage output is redirected to /dev/null.

Log files:

1. /var/log/lastlog

This file stores the user’s last login info.

2. /var/log/messages

This file contains all the global system messages.

3. /var/log/wtmp

This file keeps a history of login and logout information.

User related files

1. /usr/bin

This file contains most of the executable files.

2. /usr/bin/X11

This file contains the symbolic link of /usr/bin.

3. /usr/include

This file contains the standard include files used by the C program.

4. /usr/share

This file contains architecture-independent shareable text files.

5. /usr/lib

This file contains the object files and libraries.

6. /usr/sbin

This file contains the commands for Super User, for System Administration.

Pseudo process and virtual related files:

1. /proc/cpuinfo

This file contains the information about the CPU.

2. /proc/filesystems

This file information about the processes that are running currently.

3. /proc/interrupts

This file contains information about the number of interrupts per IRQ.

4. /proc/ioports

This file contains all the Input and Output addresses used by devices on the server.

5. /proc/meminfo

This file contains information about memory usage.

6. /proc/modules

This file contains the kernel modules.

7. /proc/mount

This file contains information about the mounted File-system.

8. /proc/stat

This file contains information about the statistics of the current system.

9. /proc/swaps

This file contains information about swap files.

Summary

You have now learned, what a directory is and the different directories present in the root directory. You have also learned the different commands like PWD, CD, MKDIR, RMDIR, and LS for managing directories from the kernel, without using the GUI.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *