Linux Files and Directories Explained [A Beginner's Guide]

On
Ubuntu command prompt illustration.If you're just starting out with Linux, getting familiar with the environment and file structure should be your top priority. And, to do that, one must learn (without any fail) about all the important files and directories powering this developer-friendly operating system. Although, as a general user, working in a Linux GUI environment is pretty straightforward, making yourself aware of the critical directories and files can help you get more out of your working sessions. Linux has dozens of distributions but the file structure and organization is almost identical on all of them. So, no matter which Linux flavor you're running, almost all the files and directories discussed below does exist on your system. If you plan to go ahead as a Linux system administrator in future, grabbing knowledge about these important files is absolutely essential. Remember, do not try to tinker with critical system files especially when you're using the system with root privileges, else it may corrupt your Linux installation. Let's get started, now!

Ubuntu command prompt illustration.
Windows 10 users can use WSL to access most of these files. Once you get familiar with these files, you'll be able to manage your system in much better and confident way.

Read Also:
Essential Commands to Manage a VPS Server Through SSH


Before you start, I'll advise you to use a general account instead of using a root account to avoid any mishap that may happen, inadvertently.

Important User-Specific Files and Directories

Every new user—in a Linux environment—gets his very own /home directory which is inaccessible by other general users. Upon login, every user lands in a directory having the same name as the login id (username) with /home as the parent directory.

So, for example, if my account's username is rajeevedmonds, my home directory will be as follows.


/home/rajeevedmonds

Apart from the dedicated home directory for the user, there are some special hidden files which are common to all the users.

Note: Some of the files mentioned below—may not be available—on every Linux distribution.


These files can be listed with the ls -la command. Let's take a look at these files, one-by-one.
  • .bash_history - This important file is useful both for the user as well as for the system administrators. User's command history is stored in this file.

    By default, the number of recently fired commands that are stored in this file is 500 which can be changed, if one desires to do so. Use the shell variables HISTSIZE and HISTFILESIZE to control the number of recent commands you want in this file.

    For example, if you just want last 100 commands in the history file, use the following command.

    
    export HISTSIZE=100 HISTFILESIZE=100

    Administrators often use this file to monitor the activities of a suspicious user. And, the user itself can use this file to quickly recall and fire previously used commands. For example.

    
    # Display the history stored in the file.
    history
    
    # Execute the recently fired (last) command.
    !!
    
    # Execute the 15th command going back in a reverse chronological order.
    !-15
    
    # Execute the oldest command present in the history file.
    !1

    If you're an avid Linux user and use long and cryptic commands—repeatedly—use the history file to salvage your precious time and to increase your productivity.
  • .bash_logout - If a user is using a bash shell on his system, the script in this file gets executed whenever he logs out from the account.

    Generally, various cleanup or small back up tasks are triggered through this script file. Do not include long and consuming tasks while writing the script for this file.
  • .bash_profile - This is yet another important file which is executed upon user's login. Naturally, it's the right place to trigger various types of initialization tasks before the shell is ready for use.

    If this file is absent, the script present in the .bash_login file is executed. And, in case, this second file is also absent, .profile script is executed.
  • .bash_login - As the name suggests, this file is executed upon user's login but only if the previously mentioned .bash_profile file is absent.
  • .bashrc - If you frequently run various shell scripts during your work session which involves spawning of a child shell, this file can be of your use.

    The script in this file is executed whenever a new bash shell is started through different means other than the login action. It's a perfect place to initialize important variables as soon as a new shell session is started.
  • .profile - If .bash_profile and .bash_login files are absent on the system, the script in this file gets executed. Users can take this file as a backup of a initialization routine.
  • .login - If you're not using a bash shell and instead using a different one—for example C shell (csh)—this file gets executed upon a user login.
  • .logout - Now, the function of this one is quite obvious. It's exactly the opposite of the .login file. Put the script in this file you want to run while logging out from a shell other than bash.
  • .viminfo - It's the initialization file for the vim text editor and its derivatives. Advanced users can use it to tweak or change the initialization parameters for the editor.
  • .xinitrc - When switching to the GUI mode using the startx command, this file is executed to activate the preferred window manager and the specified applications.
  • .xsession - This is the file that actually loads the window manager and the required applications after switching to the GUI mode.
So, this completes the list of some of the most common and important user-specific files present on almost all of the popular Linux distributions. Let's move forward!

Important System-Wide Directories

Now that we've gone through important user-specific files, it's time to understand the Linux directory structure to get a good grasp of how the operating system works—under the hood.

Once again, I may reiterate the fact that the directories mentioned below are more or less the same on every Linux distribution. Let's learn about the attributes and functions of these important directories.
  • /bin - This important directory contains executable binary files which are required during the boot process. It also has executables to repair and to put the booted system into a single-user mode.

    Binaries for all the essential commands (for example ls, cd, and cat) too reside here.
  • /boot - As the name suggests, this directory contains the Linux kernel and other important files which are used during the boot process.
  • /dev - Every hardware device connected to your Linux machine is a file within the operating system environment. This directory contains those hardware devices' files.
  • /etc - This essential directory harbors various types of configuration files for the majority of application programs. For example, startup and shutdown scripts for individual applications.
  • /home - We've already discussed about this directory in the first section. It's the parent directory of every user's home directory.
  • /lib - This extremely important directory contains both the kernel modules and the shared system libraries. Both types of these binaries are needed either for extending the power of a kernel or for running privileged commands and processes.
  • /lost+found - Sometimes the system unexpectedly freezes or shuts down leaving some of the files in an inconsistent or orphaned state. The information in this directory is used to recover those files.
  • /media - If you're using removable peripheral devices (external zip drive or a pen drive) on your system, this directory is used to mount them for access and usage.
  • /mnt - It is the legacy mount directory for a wide array of devices used on a Linux system.
  • /opt - Various 3rd party applications are installed in this directory. These are the applications which do not come prepackaged with the default installation.
  • /proc - This interesting directory consists of a virtual file system through which you can get information about the running processes. One can also retrieve system statistics from this directory.
  • /root - This special directory is the home directory for the root user. Unlike general users, who get their home directory within the /home parent directory—a root user—gets it in this directory.
  • /run - This directory is used by the service daemons which start up very early during the boot process. It's generally used to store process and communication files by these daemons.
  • /sbin - The binaries available in this directory are only available to system administrators. They use it for triggering various system maintenance commands.
  • /srv - The name itself hints at the use of this directory. It contains various server and service files which are executed—whenever required.
  • /sys - This directory is found on modern Linux distributions and harbors a virtual file system. It has files associated with connected devices which can be modified, as per need.
  • /tmp - It is the primary directory that is used by both the users and applications to store temporary files which are available until next system reboot.
  • /usr - This useful directory contains libraries, documentation, binaries, and source code for various applications and programs.
  • /var - It is an important administrative directory which is used by various programs and utilities to store temporary files, log files, and spool files.
This finishes the second section through which we learned about the most important and useful directories of the Linux operating system. Let's move on to the third and the final section of this guide.

Important System-Wide Files

In the first section, we discussed user-specific files. This section discusses various types of system files which mostly reside in one of the system directories mentioned in the previous section.

Having basic or working knowledge of these files is essential for an avid Linux user. Let's learn about these system files.
  • /boot/vmlinuz - This is the Linux Kernel file. On some distributions, you may find the release information appended to this file's name.
  • /dev/hda - This is the device file for the first IDE hard drive attached to the system.
  • /dev/hdc - Generally, it is a device file for the IDE CD-ROM device. In fact, it is just a symbolic link to the actual /dev/cdrom device driver file.
  • /dev/null - Well, that's kind of a black hole. It's a pseudo device that just doesn't exist at all. Anything sent to this device (file) is lost forever.
  • /etc/aliases - This file contains the aliases which are primarily used by the sendmail program. Some other email transport agents may also make use of this file.
  • /etc/bashrc - If you're using a bash shell, this file prepares it for use with the help of aliases and system defaults.
  • /etc/crontab - It's used to schedule custom tasks on a Linux machine. Simply give it the scheduled time (one-time or recurring) and the script that needs to be executed at the specified time.
  • /etc/exports - If your Linux system is connected to a network, this file contains the information about all the available file systems on it. This information can be used by the nodes in the network to connect to those file systems through the NFS protocol.
  • /etc/fstab - This important file contains the information about the mount points of all the available disk drives. Various shell scripts make heavy use of this information.
  • /etc/group - All the information related to security group definitions is stored in this file.
  • /etc/grub.conf - If you're using GRUB boot loader on your Linux system, this is the configuration file for the same. Advanced users can make the necessary changes to this file.
  • /etc/init.d - This important script completes two important jobs. It indirectly spawns the init process and loads the service management system.
  • /etc/hosts - Whenever a DNS server is not available, this file is used to map host names with their corresponding IP addresses.
  • /etc/hosts.allow - It's a list of all the hosts allowed to access the services running on the system.
  • /etc/hosts.deny - It's a list of all the hosts forbidden to access the services running on the system.
  • /etc/inittab - This file contains the directives for the init process about how it should set up the entire system at various run levels.
  • /etc/issue - It's generally used to modify the pre-login message. System administrators often use this file to display important information before a user attempts to login.
  • /etc/modules.conf - It holds the configuration information for all the system modules.
  • /etc/motd - This is another file commonly used by system administrators in a multi-user environment. It is used to print a message after a user logs in on his terminal.
  • /etc/mtab - This important file holds the information about all the mounted blocks and partitions.
  • /etc/passwd - You can find information related to registered users in this file. The passwords are stored in a shadow file in an encrypted form.
  • /etc/printcap - All the printer setup information resides in this file.
  • /etc/profile - This is the file that contains the global defaults for the bash shell.
  • /etc/profile.d - Here you can find application-specific startup files which are executed upon user's login.
  • /etc/rc.d - This is the area which holds the information about all the run level scripts.
  • /etc/rc.d/init.d - And, this is where you can find the initialization scripts for those run levels.
  • /etc/resolv.conf - Here, you can find the list of all the DNS servers used by the system.
  • /etc/securetty - This is the file that holds the information about all the terminals where a root login is possible.
  • /etc/skel - This essential script is used to create the standard files for a new user account.
  • /etc/termcap - It's an ASCII text file that defines the properties and behavior of terminals, consoles, and printers.
  • /proc/cpuinfo - If you want to retrieve the CPU information, use this file.
  • /proc/filesystems - This file contains the information about all the file systems—currently in use—on the system.
  • /proc/interrupts - It holds the information about all the interrupts currently in use by the system.
  • /proc/ioports - This file contains the list of I/O addresses used by all the connected devices on the system.
  • /proc/meminfo - It contains the usage information about both the physical memory and the swap partition.
  • /proc/modules - This file enlists all the kernel modules that are currently loaded.
  • /proc/mounts - To see the information related to all the currently mounted file systems, use this file.
  • /proc/stat - This file contains various types of detailed statistics about the current system state.
  • /proc/swaps - To see the swap file utilization information, use this file.
  • /proc/version - This file displays the Linux version information.
  • /var/log/lastlog - This important file holds the detailed information about the last boot process.
  • /var/log/messages - This file captures all the messages emitted by the syslog daemon during the boot process.
  • /var/log/wtmp - This is a binary file that records the login time and duration for each of the currently logged in users.