linux directory

Linux Directory structure:

tldp website is very good resource for learning Linux. This pdf is very detailed about Linux dir:

https://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/Linux-Filesystem-Hierarchy.pdf

Below info is copied from here: https://www.howtogeek.com/117435/htg-explains-the-linux-directory-structure-explained/

/ => Everything on your Linux system is located under the / directory, known as the root directory.

Under root dir, we have these other subdir:

  • /bin => The /bin directory contains the essential user binaries (programs) that must be present when the system is mounted in single-user mode. Applications such as Firefox are stored in /usr/bin, while important system programs and utilities such as the bash shell are located in /bin. The /usr directory may be stored on another partition – placing these files in the /bin directory ensures the system will have these important utilities even if no other file systems are mounted. The /sbin directory is similar – it contains essential system administration binaries.
  • /sbin => The /sbin directory is similar to the /bin directory. It contains essential binaries that are generally intended to be run by the root user for system administration.
  • /lib => The /lib directory contains libraries needed by the essential binaries in the /bin and /sbin folder. Libraries needed by the binaries in the /usr/bin folder are located in /usr/lib.
  • /lib64 => The /lib64 contains 64 bit lib.

In CentOS, /bin, /sbin, /lib and /lib64 are all soft links to corresponding dir in /usr dir. So, all these dir are actually in /usr dir. eg. bin -> usr/bin (so when we do /bin, it's actually /usr/bin)

Modules: /lib/modules/<kernel-version>/ dir has all compiled drivers (aka kernel modules). Inside it is kernel/drivers/ subdir which has modules for wireless device, bluetooth device, usb device, etc.

lsmod => list all loaded modules

modinfo <module_name> => This gives detailed info about any of the modules that you got from lsmod.

i.e modinfo video => shows location of compiled kernel module (in /lib/modules/.../*.ko), and other info.

modprobe => useful cmd for loading or unloading modules

Linux directories;

  • /boot => The /boot directory contains the files needed to boot the system – for example, the GRUB boot loader’s files (in grub2 dir) and your Linux kernels (file vmliinuz*) are stored here. The boot loader’s configuration files aren’t located here, though – they’re in /etc with the other configuration files. They generate grub.cfg file in grub2 dir as explained in section below. /boot dir has very few files: inintramfs*, vmlinuz*, grub2 dir. initramfs* is the initial RAM file system. It is the 1st root file system that the m/c has access to. It is used for mounting the real rootfs which has all your data. This is introduced from Linux 2.6 kernel series, and serves same purpose as initrd.
  • /usr => The /usr directory contains applications and files used by users, as opposed to applications and files used by the system. For example, non-essential applications are located inside the /usr/bin directory instead of the /bin directory and non-essential system administration binaries are located in the /usr/sbin directory instead of the /sbin directory. Libraries for each are located inside the /usr/lib directory. The /usr directory also contains other directories – for example, architecture-independent files like graphics are located in /usr/share.
    • The /usr/local directory is where locally compiled applications install to by default – this prevents them from mucking up the rest of the system.
    • The /usr/src/ dir has src code for linux kernels inside "kernels" subdir. /usr/src/kernels/ has further subdir with Linux kernel version number (i.e: 3.10....x86-64). All linux src files along with Makefile are inside that dir.
  • /var => variable data dir. The /var directory is the writable counterpart to the /usr directory (/usr dir is read-only in normal operation). Log files and everything else that would normally be written to /usr during normal operation are written to the /var directory. For example, you’ll find log files in /var/log
  • /home => The /home directory contains a home folder for each user. For example, if your user name is bob, you have a home folder located at /home/bob. This home folder contains the user’s data files and user-specific configuration files. Each user only has write access to their own home folder and must obtain elevated permissions (become the root user) to modify other files on the system. ~ refers to the home dir of currently logged in user. So, if alex is logged in, then ~ refers to /home/alex. Each ~ dir conatins hidden files starting with ".", such as .bashrc, .emacs, etc. These files are configuration files that determine the look, feel or startup behaviour of bash, emacs or any other application for that user.
  • /etc => The /etc directory contains configuration files, which can generally be edited by hand in a text editor. Note that the /etc/ directory contains system-wide configuration files (/etc/bashrc, /etc/profile) – user-specific configuration files are located in each user’s home directory (eg: /home/bob/.bashrc).
  • /root => The /root directory is the home directory of the root user. Instead of being located at /home/root, it’s located at /root. This is distinct from /, which is the system root directory.
  • /dev => Linux exposes devices as files, and the /dev directory contains a number of special files that represent devices. These are not actual files as we know them, but they appear as files – for example, /dev/sda represents the first SATA drive in the system. If you wanted to partition it, you could start a partition editor and tell it to edit /dev/sda.
  • This directory also contains pseudo-devices, which are virtual devices that don’t actually correspond to hardware. For example, /dev/random produces random numbers. /dev/null is a special device that produces no output and automatically discards all input – when you pipe the output of a command to /dev/null, you discard it.
  • /proc => The /proc directory similar to the /dev directory because it doesn’t contain standard files. It contains special files that represent system and process information.
  • /media => The /media directory contains subdirectories where removable media devices inserted into the computer are mounted. For example, when you insert a CD into your Linux system, a directory will automatically be created inside the /media directory. You can access the contents of the CD inside this directory.
  • /mnt => Historically speaking, the /mnt directory is where system administrators mounted temporary file systems while using them. For example, if you’re mounting a Windows partition to perform some file recovery operations, you might mount it at /mnt/windows. However, you can mount other file systems anywhere on the system.
  • /tmp => Applications store temporary files in the /tmp directory. These files are generally deleted whenever your system is restarted and may be deleted at any time by utilities such as tmpwatch.
  • /opt => The /opt directory contains subdirectories for optional software packages. It’s commonly used by proprietary software that doesn’t obey the standard file system hierarchy – for example, a proprietary program might dump its files in /opt/application when you install it.
  • /lost+found => Each Linux file system has a lost+found directory. If the file system crashes, a file system check will be performed at next boot. Any corrupted files found will be placed in the lost+found directory, so you can attempt to recover as much data as possible.

 

Dir Permissions:

Every file on linux has permissions defined for 3 different owners - user, group and all others. User is the one who created the file, group is the group of the user to which he belongs, and others refers to all the other users. Each of these 3 owners have read, write and execute (rwx) permissions specified. You will see the user name and group name, as well as rwx permissions of each file when we run cmd:

ls -al file1

-rw-r--r-x. 1 user1 grp1 0 Feb 26 07:08 file1 => user "user1" in group "grp1". permissions are rw for user1, r for grp1 and rx for others.

groups are assigned based on 1st process that runs as login shell (it's run by login pgm in /bin/login). This login pgm runs as root, and calls a C function called initgroups to set up your groups (by reading /etc/group). Each line in /etc/group file has group_name:password:group_id(GID):Group_list(all users who are members of this group). ex: root:x:0: ...  grp1:x:1000:user1. NOTE: root has User id (UID) of 0, group id (GID) of 0. You will see a lot of other groups besides root, and username groups. Those groups are used by system processes. Once your groups are set, any processes run by you inherit those groups.

GRUB:

In linux mint,

/etc/default/grub => has GRUB bootloader related settings. HEre we can choose which OS to boot into by default, how many seconds the boot screen remains up, etc.

/boot/grub/grub.cfg => This is the final file that is automatically generated when grub-mkconfig is run. It uses templates from /etc/grub.d and settings from /etc/default/grub

To change default OS in grub:

1. In Linux mint, open a terminal and login as root. This can be done by typing " su -" to become root user.

2. Change value of GRUB_DEFAULT from 0 to 2 (or whatever number windows loaded is listed at, numbers start from 0 for 1st entry, and so on) in /etc/default/grub.

3. Now run "update-grub". This will update file /boot/grub/grub.cfg. Now, restart your computer and asterisk will appear infront of "windows" in the boot menu, indicating that's the default OS.