{"id":23692507,"url":"https://github.com/rnaveensrinivas/linux","last_synced_at":"2026-05-05T06:37:32.433Z","repository":{"id":267137618,"uuid":"900279510","full_name":"rnaveensrinivas/linux","owner":"rnaveensrinivas","description":"Notes for learning Linux. This repository contains key concepts, commands, tutorials, and practical tips for using and managing Linux systems.","archived":false,"fork":false,"pushed_at":"2025-02-10T07:41:34.000Z","size":20088,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"tlcl","last_synced_at":"2025-02-20T00:43:33.479Z","etag":null,"topics":["bash","bash-script","linux","linux-shell","ubuntu","ubuntu2404lts"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rnaveensrinivas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-08T11:21:40.000Z","updated_at":"2025-02-10T07:41:37.000Z","dependencies_parsed_at":"2025-01-10T15:26:51.286Z","dependency_job_id":"455619e1-13c1-49f6-bb7e-10c35627adbc","html_url":"https://github.com/rnaveensrinivas/linux","commit_stats":null,"previous_names":["rnaveensrinivas/linux"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnaveensrinivas%2Flinux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnaveensrinivas%2Flinux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnaveensrinivas%2Flinux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnaveensrinivas%2Flinux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rnaveensrinivas","download_url":"https://codeload.github.com/rnaveensrinivas/linux/tar.gz/refs/heads/tlcl","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239758828,"owners_count":19692034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","bash-script","linux","linux-shell","ubuntu","ubuntu2404lts"],"created_at":"2024-12-30T03:28:34.284Z","updated_at":"2026-01-17T09:30:21.513Z","avatar_url":"https://github.com/rnaveensrinivas.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Table of Contents\n* [Introduction](#Introduction)\n* [Linux Directory Structure](#Linux-Directory-Structure)\n  * [Summary](#Summary-of-Linux-Directory-Structure)\n* [Command Line Interface](#Command-Line-Interface)\n* [Basic Commands](#Basic-Commands)\n  * [Summary](#Summary-of-Basic-Commands)\n* [Directories](#Directories)\n  * [Summary](#Summary-of-Directory-Commands)\n* [Viewing File and Directory Details](#Viewing-File-and-Directory-Details)\n  * [Summary](#summary-of-viewing-file-and-directory-commands)\n* [Permissions](#Permissions)\n  * [Summary](#summary-of-permissions-commands)\n* [Viewing and Editing Files](#Viewing-and-Editing-Files)\n  * [Summary](#summary-of-viewing-files)\n* [The Vim Editor](#the-vim-editor)\n  * [Summary](#Summary-of-Vim-Commands)\n* [The Vim Editor](#the-vim-editor)\n  * [Summary](#summary-of-vim-commands)\n* [Deleting, Moving, and Renaming Files and Directories](#deleting-moving-and-renaming-files-and-directories)\n  * [Summary](#Summary-of-Deleting-Moving-and-Renaming-Files-and-Directories-Commands)\n* [Finding, Sorting, and Comparing Files and Directories](#finding-sorting-and-comparing-files-and-directories)\n  * [Summary](#Summary-of-Finding-Sorting-and-Comparing-Files-and-Directories)\n* [I/O Redirection](#io-redirection)\n  * [Summary](#Summary-of-IO-Redirection-Commands)\n* [Additional Command Line Concepts](#Additional-Command-Line-Concepts)\n  * [Summary](#Summary-of-Additional-Command-Line-Concept-Commands)\n* [Processes and Jobs](#Processes-and-Jobs)\n  * [Summary](#Summary-of-Processes-and-Jobs-Control-Commands)\n* [Switching Users](#Switching-Users)\n  * [Summary](#Summary-of-Switching-User-Commands)\n* [Installing Software](#Installing-Software)\n  * [Summary](#Summary-of-Installing-Software-Commands)\n* [**CheatSheet**](#Cheat-Sheet)\n---\n\n# Introduction  \n\n## What is Linux?  \nLinux is a **Unix-like open-source operating system** renowned for its flexibility, security, and extensive community support. At its core is the **[Linux kernel](https://github.com/torvalds/linux)**, the foundational component that bridges the gap between applications and hardware. The kernel manages system resources, ensures hardware-software communication, and provides essential services like memory management and process control.  \n\n### Key Characteristics of Linux  \n- **Unix-like**: Shares architectural and conceptual similarities with Unix.  \n- **Open Source**: Freely available for use, modification, and distribution.  \n- **Kernel-Centric**: The kernel serves as the foundation of the operating system.  \n\n---\n\n## A Brief History of Linux  \nLinux was created in **1991** by **Linus Torvalds**, a Finnish computer science student, as a personal project to develop a free and open-source alternative to proprietary Unix systems. Inspired by **Minix**, a Unix-like system for educational purposes, Torvalds initially released the kernel under a proprietary license but soon adopted the **GNU General Public License (GPL)**. This move aligned Linux with the **GNU Project**, initiated by **Richard Stallman**, which aimed to create a completely free operating system.  \n\nSince its inception, Linux has evolved through collaboration between Torvalds and a global community of developers. Today, it powers desktops, servers, embedded systems, and supercomputers, becoming one of the most versatile and widely used operating systems.  \n\n---\n\n## Linux Distributions  \nA **Linux distribution (distro)** is a complete operating system built around the Linux kernel. Distributions bundle essential software, tools, and utilities tailored to various purposes, creating a cohesive and usable environment.  \n\n### Features of Linux Distributions  \n1. **Shared Core**: All distributions are built on the Linux kernel.  \n2. **Custom Software**: Each distribution bundles software, tools, and configurations based on its focus.  \n3. **Targeted Use Cases**: Some distributions cater to **desktops** (e.g., Linux Mint), others to **servers** (e.g., CentOS, RHEL), while some serve both (e.g., Ubuntu).  \n4. **Support Models**: Distributions are maintained by either **community** volunteers or **corporations** offering professional support.  \n\n### Popular Linux Distributions  \n\n| **Distribution**| **Primary Usage** | **Support Model**| **Notes** |\n|--------------------------|-------------------|-----------------------|---------------------------------------------------------------------------|\n| **Linux Mint**   | Desktop   | Community | User-friendly, ideal for beginners.   |\n| **Ubuntu**   | Both  | Community | Widely used with LTS versions for stability.  |\n| **Debian**   | Both  | Community | Stable and reliable, popular for servers. |\n| **Fedora**   | Both  | Community | Cutting-edge features, upstream for RHEL. |\n| **openSUSE** | Both  | Community/Corporate   | Offers Leap (stable) and Tumbleweed (rolling release) versions.   |\n| **Arch Linux**   | Desktop   | Community | Minimalist and highly customizable.   |\n| **CentOS**   | Server| Community | RHEL-based, stable, enterprise-ready. |\n| **RHEL** | Server| Corporate | Commercial support for enterprise environments.   |\n| **Manjaro**  | Desktop   | Community | Arch-based with user-friendly tools.  |\n| **Elementary OS**| Desktop   | Community | Aesthetic design, excellent for newcomers.|\n| **Kali Linux**   | Desktop   | Community | Specialized for penetration testing and security tasks.   |\n| **Zorin OS** | Desktop   | Community | Perfect for users transitioning from Windows. |\n| **Pop!_OS**  | Desktop   | Community | Developer- and gamer-friendly, based on Ubuntu.   |\n| **AlmaLinux**| Server| Community | RHEL clone, focused on enterprise users.  |\n| **Slackware**| Both  | Community | Traditional UNIX-like experience. |\n| **MX Linux** | Desktop   | Community | Lightweight and efficient for older hardware. |\n\n**Legend**:  \n- **Desktop**: Tailored for personal or workstation use.  \n- **Server**: Designed for stability, often used in enterprise or hosting environments.  \n- **Community**: Developed and maintained by volunteers.  \n- **Corporate**: Backed by companies providing commercial support.  \n\n---\n\n## Why Learn Linux?  \nThe core principles and functionalities of Linux remain consistent across distributions, making it a universal skill for tech enthusiasts and professionals. Linux powers:  \n- **90% of the world's supercomputers**,  \n- **96.3% of the top 1 million web servers**,  \n- **74% of smartphones (via Android)**.  \n\nIts unmatched flexibility, security, and open-source nature have made Linux a cornerstone of modern technology. Learning Linux opens doors to diverse career opportunities and empowers you to work efficiently in today's computing environments.  \n\n---  \n\n# Linux Directory Structure\n\nThe Linux directory structure is hierarchical, resembling an inverted tree with the **root** or **trunk** (`/`) as its base. All directories branch off from the root, with forward slashes (`/`) separating directories.\n\n**Directory** is nothing but a linux jargon for **folder**.\n\n## Common Top-Level Directories\n\n### `/` Root Directory\nThe root of the Linux file system, often referred to as the **slash (/)** or the **trunk**, serves as the starting point for all files and directories. Everything in Linux resides under `/`. This is similar to `C:\\` in Windows. However, unlike Windows, where additional storage is assigned drive letters like `D:\\`, Linux mounts additional storage devices as subdirectories, typically under locations like `/mnt` or `/media`.\n\n```bash\n$ ls /\nbin                dev   lib64              mnt   run                 srv       usr\nbin.usr-is-merged  etc   lib.usr-is-merged  opt   sbin                swap.img  var\nboot               home  lost+found         proc  sbin.usr-is-merged  sys\ncdrom              lib   media              root  snap                tmp\n```\n\n### `/bin` Binaries\nContains **essential executable programs** (e.g., basic commands like `ls`, `cp`). Additional non-essential binaries like graphical apps, mail, and trivial cmd utitlies are found in `/usr/bin`.\n```bash\n$ ls /bin\n'['                                   mpris-proxy\n aa-enabled                           mpstat\n aa-exec                              mscompress\n aa-features-abi                      msexpand\n aconnect                             mt\n acpidbg                              mt-gnu\n add-apt-repository                   mtr\n addpart                              mtrace\n addr2line                            mtr-\n ...\n```\n\n### `/etc` System Configuration Files\nHouses **configuration files** that controls how the operating system and applications (e.g., boot modes, nareetwork settings) behave.\n```bash\n$ ls /etc\nadduser.conf            fuse.conf             logrotate.d          rsyslog.d\nalsa                    fwupd                 lsb-release          rygel.conf\nalternatives            gai.conf              machine-id           sane.d\nanacrontab              gdb                   magic                security\napache2                 gdm3                  magic.mime           selinux\napg.conf                geoclue               manpath.config       sensors3.conf\napm                     ghostscript           mime.types           sensors.d\napparmor                glvnd                 mke2fs.conf          services\napparmor.d              gnome                 ModemManager         sgml\napport                  gnome-remote-desktop  modprobe.d           shadow\napt                     gnutls                modules              shadow-\navahi                   gprofng.rc            modules-load.d       shells\nbash.bashrc             groff                 mtab                 skel\nbash_completion         group                 nanorc               snmp\nbash_completion.d       group-                netconfig            speech-dispatcher\nbindresvport.blacklist  grub.d                netplan              ssh\nbinfmt.d                gshadow               network              ssl\n...\n```\n\n### `/home` Home Directories\nEach user on a Linux system has a subdirectory dedicated to their account in the /home directory. For example,Example: `/home/sri`.\n```bash\n$ ls /home/\nsri\n```\n\n### `/opt` Optional or Third-Party Software\nUsed for **software not bundled with the operating system**, like Google Chrome.\n\n**Fact** - Firefox comes builtin with the operating system. \n```bash\n$ whereis firefox\nfirefox: /usr/bin/firefox /snap/bin/firefox\n```\n\n### `/tmp` Temporary Files\nTemporary workspace for applications and users. Files here are **cleared at boot time**.\n```bash\n$ ls /tmp\nsnap-private-tmp\nsystemd-private-9e8bed51413f4d929422b8d7d854bbcd-bluetooth.service-rcvxLj\nsystemd-private-9e8bed51413f4d929422b8d7d854bbcd-colord.service-1fPQpU\nsystemd-private-9e8bed51413f4d929422b8d7d854bbcd-fwupd.service-RclrPC\nsystemd-private-9e8bed51413f4d929422b8d7d854bbcd-ModemManager.service-ItsVvS\n...\n```\n\n### `/usr` - User-Related Data  \nThe `/usr` directory contains user-related programs and **read-only data**, which are not essential for the operating system's core functionality. Subdirectories include:\n- `/usr/bin`: Executable programs.\n- `/usr/lib`: Libraries.\n- `/usr/share/doc`: Shared documentation and resources.\n\n#### `/usr`\n```bash\n$ ls /usr\nbin  games  include  lib  lib64  libexec  local  sbin  share  src\n```\n#### `/usr/bin`\n```bash\n$ ls /usr/bin\n'['                                   mpris-proxy\n aa-enabled                           mpstat\n aa-exec                              mscompress\n aa-features-abi                      msexpand\n aconnect                             mt\n acpidbg                              mt-gnu\n add-apt-repository                   mtr\n addpart                              mtrace\n addr2line                            mtr-packet\n airscan-discover                     mv\n alsabat                              namei\n alsaloop                             nano\n ...\n```\n#### `/usr/lib`\n```bash\n$ ls /usr/lib\napg                        gvfs                                  pam.d\napparmor                   hdparm                                pcmciautils\napt                        init                                  pcrlock.d\naspell                     initramfs-tools                       pm-utils\nbfd-plugins                ispell                                policykit-1\nbinfmt.d                   kernel                                polkit-1\nbrltty                     klibc                                 pppd\n...\n```\n#### `/usr/share/doc`\n```bash\n$ ls /usr/share/doc\naccountsservice                                libmpc3\nacl                                            libmpcdec6\nadduser                                        libmpeg2-4\nadwaita-icon-theme                             libmpfr6\nalsa-base                                      libmpg123-0t64\nalsa-topology-conf                             libmsgraph-0-1\nalsa-ucm-conf                                  libmtdev1t64\nalsa-utils                                     libmtp9t64\namd64-microcode                                libmtp-common\nanacron                                        libmtp-runtime\napg                                            libmutter-14-0\napparmor                                       libmysofa1\napport                                         libnautilus-extension4\napport-core-dump-handler                       libncurses6\napport-gtk                                     libncursesw6\n...\n```\n\n### What is the difference between `/bin` and `/usr/bin`?\n\n#### `/bin` (Essential Binaries)\n- **Purpose**: Contains essential binaries required for the system to function, especially during boot or when the system is in single-user mode.\n- **Availability**: These binaries are available even if the `/usr` directory is not mounted.\n- **Usage**: Includes basic commands that are needed for system recovery, maintenance, and basic user interaction.\n- **Examples**:\n  - `ls`: List directory contents\n  - `cp`: Copy files\n  - `mv`: Move files\n  - `cat`: Concatenate and display file contents\n  - `bash`: The Bourne Again Shell\n\n\n#### `/usr/bin` (Non-Essential User Binaries)\n- **Purpose**: Contains binaries for general use by all users, but these are not essential for booting or repairing the system.\n- **Availability**: Typically resides on a separate partition and may not be available if `/usr` is not mounted (e.g., during early boot stages or recovery modes).\n- **Usage**: Includes a broader range of applications and tools that are not critical to the core system operation.\n- **Examples**:\n  - `vim`: Text editor\n  - `gcc`: C compiler\n  - `python`: Python interpreter\n  - `git`: Version control system\n\n#### **Historical Context**\n- In older UNIX systems, `/bin` was designed for essential commands, while `/usr/bin` held supplementary tools. Over time, `/usr/bin` grew to contain a significant portion of system utilities.\n- Modern Linux systems often include `/bin` and `/usr/bin` as part of the same filesystem, and some distributions have unified these directories, **symlinking `/bin` to `/usr/bin`** for simplicity.\n- \n---\n\n### What is the difference between `/usr/bin` and `/opt`?\n\n#### **`/usr/bin`**\nThis directory contains user binaries (executable files) that are part of the core system software (not for OS but for system users). These are programs and utilities that are necessary for normal system operations and user tasks. They are typically installed by the distribution's package manager and are shared across all users on the system.\n\n#### **`/opt`**\nThis directory is used for installing optional software packages that are **not part of the default system installation**. Software in `/opt` is typically self-contained, meaning it may include all necessary libraries and dependencies. It is often used for third-party software or larger applications that don't conform to the standard Linux directory structure. E.g. Google Chromee, Microsoft Edge. \n\n#### Key Differences:\n- **Purpose**:  \n  - `/usr/bin` is for essential, system-wide binaries required by the OS and users.  \n  - `/opt` is for optional, third-party software that is installed separately from the core system.\n  \n- **Installation**:  \n  - Binaries in `/usr/bin` are generally installed by the system package manager.  \n  - Software in `/opt` is often manually installed or provided by vendors as standalone packages.\n\n---\n\n### `/var` Variable Data\nStores variable and runtime data, like log files in `/var/log`.\n\n```bash\n$ ls /var\nbackups  cache  crash  lib  local  lock  log  mail  metrics  opt  run  snap  spool  tmp\n```\n---\n\n## Additional Important Directories\n- `/boot`: Files required for system booting.\n- `/dev`: Device files representing hardware.\n- `/lib` and `/lib64`: Shared libraries.\n- `/lost+found`: Used by the file system to store recovered files after a file system check has been performed.\n- `/mnt` and `/media`: Mount points for external file systems or media.\n- `/proc` and `/sys`: Virtual filesystems for process and system information.\n- `/sbin`: System administration binaries.\n- `/srv`: Data served by the system, e.g., web or FTP files.\n- `/root`: Home directory for the root user.\n\n---\n\n### Library in Linux:\n\nIn Linux, a **library** is a collection of precompiled, reusable code that provides specific functionality for programs. Libraries contain functions or routines that programs can call to perform common tasks, such as handling files, performing mathematical operations, or managing network connections. Rather than each program writing its own code for these tasks, it can instead link to a library, saving time, reducing redundancy, and ensuring consistency across programs.\n\nThere are two main types of libraries in Linux:\n\n#### Static Libraries (`.a` files):  \n   These are collections of compiled object files that are embedded directly into the executable at compile time. When a program links to a static library, the library's code becomes part of the program itself, making the executable larger but eliminating the need for the library to be present at runtime.\n\n#### Dynamic Libraries (`.so` files):  \n   These libraries are loaded into memory at runtime when the program is executed. They are not part of the program's executable file but are instead shared by multiple programs. This reduces memory usage because the library is only loaded into memory once. Dynamic libraries can be updated independently of the programs using them, providing greater flexibility.\n\n### Difference Between `/bin` and `/lib`:\n\n#### `/bin` (Binaries):  \n  The `/bin` directory contains **essential system binaries** (executable programs) required for the basic functionality of the system. These are crucial programs that both the operating system and regular users need to perform system tasks. Examples of binaries in `/bin` include commands like `ls`, `cp`, and `mkdir`. These binaries are essential for the system to operate and for users to interact with the system.\n\n#### `/lib` (Libraries):  \n  The `/lib` directory contains **shared libraries** that provide essential support for the binaries in `/bin` (and other directories). These libraries contain the compiled code needed by system programs to execute specific tasks. For example, the C standard library (`libc.so`) is stored in `/lib` and provides fundamental functions like input/output handling and memory management. The files in `/lib` are not executable on their own but are used by the binaries to run efficiently.\n\n#### Key Differences:\n- **Purpose**:  \n  - `/bin` contains **executable programs** (binaries) required for system operation.  \n  - `/lib` contains **shared libraries** that provide support for these programs to run.\n  \n- **Content**:  \n  - `/bin` holds essential **commands** for both system administrators and regular users.  \n  - `/lib` holds **shared code** that is needed by these commands and other applications to function.\n\n- **Functionality**:  \n  - Programs in `/bin` execute tasks directly, while programs in `/lib` provide the necessary libraries that programs in `/bin` rely on to perform their functions.\n\n---\n\n## Finding binary, source, and manual pages\n\nThe `whereis` command in Linux is used to locate the **binary, source, and manual page** files for a command or program. It's a helpful tool for finding the location of executables, their associated documentation, and related source code.\n\n### Syntax:\n```bash\nwhereis [options] \u003ccommand_name\u003e\n```\n\n### Common Options:\n- **`-b`**: Search only for binary files (executable files).\n- **`-m`**: Search only for manual pages.\n- **`-s`**: Search only for source code.\n- **`-u`**: Locate unlinked files (files that don't have a corresponding entry in the database).\n- **`-B \u003cpath\u003e`**: Specify a directory to search for binaries.\n- **`-M \u003cpath\u003e`**: Specify a directory to search for manual pages.\n- **`-S \u003cpath\u003e`**: Specify a directory to search for source files.\n\n### Example Usage:\n\n1. **Locate all files related to the `ls` command**:\n   ```bash\n   $ whereis ls\n   ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz\n   ```\n   This will return locations for the `ls` binary, its manual page, and possibly its source code.\n\n2. **Find the binary for `gcc` only**:\n   ```bash\n   $ whereis -b gcc\n   gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc /usr/share/gcc\n   ```\n   This will only return the location of the `gcc` binary.\n\n3. **Find the manual page for `bash`**:\n   ```bash\n   $ whereis -m bash\n   bash: /usr/share/man/man1/bash.1.gz\n   ```\n   This will return the location of the `bash` manual page.\n\n### How It Works:\n- The `whereis` command searches through predefined directories set in system configuration files (like `/usr/bin`, `/bin`, `/usr/local/bin`, etc.).\n- It uses a database to speed up the process of locating files, unlike the `which` command, which only returns the location of the executable in the directories specified in the `$PATH` environment variable.\n\n### Use Cases:\n- Quickly finding the path to an executable, manual page, or source code.\n- Verifying whether a specific command or tool is installed on the system.\n- Locating specific documentation or source code for a program.\n\n---\n\n## Application Directory Organizatoin Structure\n\nApplication directory structures can be organized similar to the operating system's layout. For example, when Apache is installed in `/usr/local`, its directories can look like:\n- `/usr/local/apache/bin`: Binaries and executables\n- `/usr/local/apache/etc`: Configuration files\n- `/usr/local/apache/lib`: Libraries\n- `/usr/local/apache/logs`: Log files\n\nIf installed in `/opt`, the structure is similar:\n- `/opt/apache/bin`: Binaries\n- `/opt/apache/etc`: Configurations\n- `/opt/apache/lib`: Libraries\n- `/opt/apache/logs`: Logs\n\nAnother common pattern is separating configuration and log data:\n- `/etc/opt/apache`: Configuration files\n- `/opt/apache/bin`: Binaries\n- `/opt/apache/lib`: Libraries\n- `/var/opt/apache`: Logs\n\nApplications can also share common directory spaces. For instance, Apache might be installed in `/usr/local` along with other local applications.\n\n## Summary of Linux Directory Structure\n\n| **Directory**   | **Purpose**| **Key Contents**|\n|------------------|-----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------|\n| `/bin`   | Essential binaries for system operation and recovery.  | Basic commands like `ls`, `cp`, `mv`, `cat`, `bash`. |\n| `/boot`  | Contains bootloader files and the kernel.  | Kernel images, `grub` configuration files.  |\n| `/dev`   | Device files representing hardware and virtual devices.| Files like `/dev/sda` (disk), `/dev/null`, `/dev/tty` (terminals).   |\n| `/etc`   | System-wide configuration files.   | Configs for services like `ssh`, `passwd`, `fstab`.  |\n| `/home`  | User home directories. | Personal files and directories for users (e.g., `/home/naveen`). |\n| `/lib`   | Essential shared libraries and kernel modules. | Libraries used by programs in `/bin` and `/sbin`.   |\n| `/media` | Mount points for removable media.  | Subdirectories for CDs, USB drives (e.g., `/media/usb`).|\n| `/mnt`   | Temporary mount point for filesystems. | Used for mounting partitions during maintenance or temporary usage.  |\n| `/opt`   | Optional software packages installed outside standard directories. | Third-party apps like `/opt/google`.|\n| `/proc`  | Virtual filesystem providing process and system information.   | Files like `/proc/cpuinfo`, `/proc/meminfo`.|\n| `/root`  | Home directory for the root user.  | Files specific to the `root` superuser. |\n| `/run`   | Runtime files like process IDs (PIDs) and sockets.| Files such as `/run/sshd.pid`.  |\n| `/sbin`  | System binaries for administrative tasks.  | Commands like `fsck`, `shutdown`, `mount`.  |\n| `/srv`   | Data served by the system, such as web server or FTP server files. | E.g., `/srv/www` for websites.  |\n| `/sys`   | Virtual filesystem for device and system configuration.   | Hardware information and control files (e.g., `/sys/class`).|\n| `/tmp`   | Temporary files, often cleared on reboot. | Session data, temporary downloads.  |\n| `/usr`   | Secondary hierarchy for user programs and data.   | Subdirectories like `/usr/bin`, `/usr/lib`, `/usr/share`.   |\n| `/var`   | Variable data files that change during operation. | Logs (`/var/log`), spools (`/var/spool`), caches (`/var/cache`). |\n\n### Notes:\n1. Some directories like `/media` or `/mnt` might not have content unless specifically used.\n2. On modern systems, certain directories like `/bin` and `/sbin` may be symlinks to `/usr/bin` and `/usr/sbin`.\n3. The layout and contents of these directories can vary slightly between Linux distributions.\n\n| Commands  | Description   |\n|-----------------------------------|-------------------------------------------------------------------|\n| `ls [dir]`| Lists the contents of a directory.|\n| `whereis -b [command]`| Searches for the binary (executable) file of a command.   |\n| `whereis -m [command]`| Searches for the manual page of a command.|\n| `whereis -s [command]`| Searches for the source code of a command.|\n| `whereis -u [command]`| Finds unlinked files (files not listed in the system's database). |\n| `whereis -B \u003cpath\u003e [command]` | Specifies a directory to search for binaries. |\n| `whereis -M \u003cpath\u003e [command]` | Specifies a directory to search for manual pages. |\n| `whereis -S \u003cpath\u003e [command]` | Specifies a directory to search for source files. | \n\nThis table now includes the `whereis` command, along with its options and descriptions.\n\n---\n\n# Command Line Interface\n\nThe **shell** is a program that accepts commands and directs the operating system to execute them. It serves as the interface between the user and the system. The **Command Line Interface (CLI)** is a text-based interface where users type commands.\n\n## Shell and Interface Types\n- **CLI (Text Interface)**: When connected to a Linux system, a shell is started, and the user interacts through the command line.\n- **GUI (Graphical User Interface)**: When a Linux system is in graphical mode, users interact through a GUI. To access the CLI within a GUI, a terminal emulator (e.g., `xterm`, `GNOME Terminal`) must be used.\n\n## Shell Prompt\nThe **shell prompt** provides information about the user, system, and current directory. For example:\n```bash\nsri@envy:~$\n```\n- `sri`: Username\n- `envy`: Hostname\n- `~`: Current directory (home directory, e.g., `/home/sri`)\n\nThe prompt can vary in format, and users can customize it. Special symbols like `~` (tilde) refer to the user's home directory. For example, `~sri` expands to `/home/sri`.\n\n### Examples of Shell Prompts:\n- `[sri@envy /tmp]$`\n- `envy:/home/sri\u003e`\n- `sri@envy:~\u003e`\n\nShell prompts can also span multiple lines, showing additional information like time and session details. \n```bash\nsri@envy:~/Document/Linux\n$\n```\n## What is the difference between terminal and shell?\n\nThe terms **terminal** and **shell** are often used interchangeably, but they refer to different components of a Linux or Unix-based system. Here's the distinction:\n\n### Terminal:\n- A **terminal** is a program or interface that allows the user to interact with the computer by entering text-based commands. It provides the environment where you can type and execute commands, and it displays the output of those commands.\n- **Terminal** is essentially the window or emulator where the user interacts with the system's shell.\n- In the past, a **terminal** referred to physical hardware (like a teletype machine or a console), but today, it usually refers to terminal emulator software (like `gnome-terminal`, `xterm`, or `Konsole`) that runs on modern graphical desktop environments.\n\n**Example**: `gnome-terminal`, `xterm`, `Konsole`, etc.\n\n### Shell:\n- The **shell** is a program that interprets and executes the commands you type in the terminal. It acts as a command-line interface (CLI) between the user and the operating system.\n- The shell processes the commands you type, interprets them, and sends them to the operating system for execution. It also handles tasks like piping commands, file redirection, environment variables, and script execution.\n- Common types of shells in Linux include:\n - **Bash** (Bourne Again Shell)\n - **Zsh** (Z Shell)\n - **Fish** (Friendly Interactive Shell)\n - **Tcsh** (an enhanced version of the C shell)\n\n**Example**: `bash`, `zsh`, `fish`, `sh`, etc.\n\n### Key Differences:\n- **Terminal**:  \n  - The **interface** or **emulator** where commands are entered and output is displayed.\n  - It's a window or program that allows interaction with the system.\n  \n- **Shell**:  \n  - The **program** that interprets and executes commands typed into the terminal.\n  - It's the command processor that runs inside the terminal and interacts with the operating system.\n\n### Example to Illustrate the Difference:\n- When you open a **terminal** (like `gnome-terminal`), you're launching a program that displays a window.\n- Inside that terminal window, a **shell** (like `bash`) is running, and it waits for you to type commands.\n- When you type a command in the terminal, the **shell** processes and executes it.\n\nIn short, the **terminal** is the interface you use to interact with the system, and the **shell** is the program that interprets the commands you input in that interface.\n\n---\n\n# Basic Commands\n\nIn Linux, commands are **case-sensitive** and typically in **lowercase**. Items surrounded by square brackets are optional. \n\n## Navigation Commands:\n### `pwd` Command\nDisplays the present working directory.\n```bash\n# Syntax\n$ pwd\n\n# Example \nsri@envy:~/Documents/Linux\n$ pwd\n/home/sri/Documents/Linux\n```\n### `cd` Command \nChanges the current directory to the specified directory. Without an argument, it changes to the home directory.\n```bash\n# Syntax\n$ cd [directory]\n\n# Example\nsri@envy:~/Documents/Linux\n$ cd /home\nsri@envy:/home\n$ pwd\n/home\n```\n\nIf you want to go to previous directory use `cd -`, it's like a back button.\n```bash\n# Syntax \n$ cd -\n\n# Example\nsri@envy:~\n$ cd Documents/Linux/\nsri@envy:~/Documents/Linux\n$ cd -\n/home/sri\nsri@envy:~\n$ cd -\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ \n```\n\n\nYou can get to the user's home directory in **four** differnet ways using `cd`. \n\n```bash\n# 1. using \"cd\"\nsri@envy:~/Documents/Linux\n$ cd \nsri@envy:~\n$ \n\n# 2. using \"cd ~\"\nsri@envy:~/Documents/Linux\n$ cd ~\nsri@envy:~\n$ \n\n# 3. using \"cd ~sri\"\nsri@envy:~/Documents/Linux\n$ cd ~sri\nsri@envy:~\n$ \n\n# using \"cd /home/sri\"\nsri@envy:~/Documents/Linux\n$ cd /home/sri\nsri@envy:~\n$ \n```\n\n## Listing and Viewing Files:\n### `ls` Command\nLists the contents of a directory.\n```bash\n# Syntax\n$ ls [path]\n\n# Examples\nsri@envy:~\n$ ls\nDesktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos\nsri@envy:~\n$ \n\n# Using relative path\nsri@envy:~\n$ ls Documents/Linux/\nLinux_command_line_for_you_and_me_Release_0.1.pdf  Linux_Succinctly.pdf  README.md\nsri@envy:~\n$ \n\n# Using absolute path\nsri@envy:~\n$ ls /home/sri/Documents/Linux/\nLinux_command_line_for_you_and_me_Release_0.1.pdf  Linux_Succinctly.pdf  README.md\nsri@envy:~\n$ \n\n```\n### `cat` Command \nDisplays the contents of a **file**.\n```bash\n# Syntax\n$ cat [file]\n\n# Example\nsri@envy:~\n$ cat /home/sri/Documents/Linux/README.md \n# Table of Contents\n* [Introduction](#Introduction)\n* [Linux Directory Structure](#Linux-Directory-Structure)\n  * [Summary](#Summary-of-Linux-Directory-Structure)\n* [Command Line Interface](#Command-Line-Interface)\n* [Basic Commands](#Basic-Commands)\n  * [Summary](#Summary-of-Basic-Commands)\n\n# Without any arguments\n$ cat\nThis will just repeat whatever I type in standard input.\nThis will just repeat whatever I type in standard input.\nTo quit press, Ctrl+c\nTo quit press, Ctrl+c\n^C\nsri@envy:~\n$ \n```\n\n## Exiting the Shell:\n\n### `exit` Command\nExit the current shell. \n```bash\nsri@envy:~\n$ exit\n```\n### `logout` Command\nExit from current login session.\n```bash\n# Not available in Ubuntu 24.04 Desktop\n$ logout \nbash: logout: not login shell: use `exit'\n```\n### `CTRL + d` Shortcut\nExit from shell using the shortcut, `CTRL + d` or `CTRL + D`.\n```bash\n$ ^D\n```\n\n## Command Line Help:\nLinux provides built-in documentation through **man** pages, detailing options, example usage and arugments of a specified command.\n\n### `man` Command \nDisplays the manual page for a command.\n```bash\n# Syntax\n$ man command\n\n# Example\nsri@envy:~\n$ man ls\n```\n\n#### Navigating man pages\n\n| Key   | Action  |\n|-----------------------|-----------------------------|\n| Enter, Down Arrow | Move down one line. |\n| Up Arrow  | Move up one line.   |\n| Spacebar, Page Down   | Move down one page. |\n| Page Up   | Move up one page.   |\n| `g`   | Go to the start or top. |\n| `G`   | Go to the end or bottom.|\n| `h`   | Display help.   |\n| `j`   | Move down one line. |\n| `k`   | Move up one line.   |\n| `q`   | Quit.   |\n\n\n#### Searching man pages\n\n##### `man -k` Command\nReturns all the man pages that contain the **keyword**.\n```bash\nsri@envy:~\n# Syntax \n$ man -k [keyword]\n\n# Example\nsri@envy:~\n$ man -k reboot\ngrub-reboot (8)      - set the default boot entry for GRUB, for the next boot only\nhalt (8)             - Power off, reboot, or halt the machine\npoweroff (8)         - Power off, reboot, or halt the machine\nreboot (2)           - reboot or enable/disable Ctrl-Alt-Del\nreboot (8)           - Power off, reboot, or halt the machine\nshutdown (8)         - Halt, power off or reboot the machine\nsystemd-pcrlock-secureboot-authority.service (8) - Analyze and predict TPM2 PCR states and ...\nsystemd-pcrlock-secureboot-policy.service (8) - Analyze and predict TPM2 PCR states and gen...\nsystemd-reboot.service (8) - System shutdown logic\nsystemd-soft-reboot.service (8) - Userspace reboot operation\nsystemd-sysupdate-reboot.service (8) - Automatically Update OS or Other Resources\nsystemd-sysupdate-reboot.timer (8) - Automatically Update OS or Other Resources\nsri@envy:~\n$ \n```\n##### `apropos` Command\nWork similar to `man -k [keyword]`.\n\n```bash\n# Syntax\n$ apropos [keyword]\n\n# Example\nsri@envy:~\n$ apropos reboot\ngrub-reboot (8)      - set the default boot entry for GRUB, for the next boot only\nhalt (8)             - Power off, reboot, or halt the machine\npoweroff (8)         - Power off, reboot, or halt the machine\nreboot (2)           - reboot or enable/disable Ctrl-Alt-Del\nreboot (8)           - Power off, reboot, or halt the machine\nshutdown (8)         - Halt, power off or reboot the machine\nsystemd-pcrlock-secureboot-authority.service (8) - Analyze and predict TPM2 PCR states and ...\nsystemd-pcrlock-secureboot-policy.service (8) - Analyze and predict TPM2 PCR states and gen...\nsystemd-reboot.service (8) - System shutdown logic\nsystemd-soft-reboot.service (8) - Userspace reboot operation\nsystemd-sysupdate-reboot.service (8) - Automatically Update OS or Other Resources\nsystemd-sysupdate-reboot.timer (8) - Automatically Update OS or Other Resources\nsri@envy:~\n$ \n```\n\n### `--help` option\nDisplays a help message for a command.\n```bash\n# Syntax\n$ command --help\n\n# Example\nsri@envy:~\n$ ls --help\nUsage: ls [OPTION]... [FILE]...\nList information about the FILEs (the current directory by default).\nSort entries alphabetically if none of -cftuvSUX nor --sort is specified.\n\nMandatory arguments to long options are mandatory for short options too.\n  -a, --all                  do not ignore entries starting with .\n  -A, --almost-all           do not list implied . and ..\n      --author               with -l, print the author of each file\n...\n\nsri@envy:~\n$ cd --help\ncd: cd [-L|[-P [-e]] [-@]] [dir]\n    Change the shell working directory.\n    \n    Change the current directory to DIR.  The default DIR is the value of the\n    HOME shell variable. If DIR is \"-\", it is converted to $OLDPWD.\n    \n    The variable CDPATH defines the search path for the directory containing\n    DIR.  Alternative directory names in CDPATH are separated by a colon (:).\n    A null directory name is the same as the current directory.  If DIR begins\n    with a slash (/), then CDPATH is not used.\n...\n```\n\n## Exploring Commands:\nYou can explore commands in directories like `/bin` and `/usr/bin` using `ls` and `man` to learn their functions.\n```bash\nsri@envy:~\n$ cd /bin\nsri@envy:/bin\n$ ls\n'['                                   mpris-proxy\n aa-enabled                           mpstat\n aa-exec                              mscompress\n aa-features-abi                      msexpand\n aconnect                             mt\n acpidbg                              mt-gnu\n add-apt-repository                   mtr\n addpart                              mtrace\n addr2line                            mtr-packet\n airscan-discover                     mv\n alsabat                              namei\n alsaloop                             nano\n alsamixer                            nautilus\n alsatplg                             nautilus-autorun-software\n alsaucm                              nautilus-sendto\n amidi                                nawk\n amixer                               nc\n apg                                  nc.openbsd\n apgbfm                               neqn\n aplay                                netaddr\nsri@envy:/bin\n$ man mv\nsri@envy:/bin\n$ \n```\n\n### Know what a command does with `whatis`\n```bash\n# Syntax\n$ whatis [command]\n\n# Example\nsri@envy:~/Documents\n$ whatis ln\nln (1)               - make links between files\nsri@envy:~/Documents\n$ whatis ls\nls (1)               - list directory contents\nsri@envy:~/Documents\n$ whatis cd\ncd: nothing appropriate.\nsri@envy:~/Documents\n$ whatis man\nman (1)              - an interface to the system reference manuals\nsri@envy:~/Documents\n$ whatis whatis\nwhatis (1)           - display one-line manual page descriptions\nsri@envy:~/Documents\n$ \n```\n\n## Clearing the terminal screen.\nScreen can be cleared using \n\n#### `clear` command\nThe below command clears the entire terminal. Gives a fresh terminal.\n```bash\n$ clear\n```\n#### `CTRL + l` Shortcut\nThis doesn't doesn't clear the terminal, but clears the Window. If you scroll up you can see the previous commands. \n```bash\n$ ^l\n```\n---\n\n## Creating files\nTo create an empty files use `touch`\n```bash\n# Syntax\n$ touch file[s]\n\n# Example\nsri@envy:~\n$ ls\nDesktop    Downloads  Pictures  snap       Videos\nDocuments  Music      Public    Templates\nsri@envy:~\n$ touch random_file\nsri@envy:~\n$ ls\nDesktop    Downloads  Pictures  random_file  Templates\nDocuments  Music      Public    snap         Videos\nsri@envy:~down\n$ touch random1 randome2\nsri@envy:~\n$ ls\nDesktop    Downloads  Pictures  random1   random_file  Templates\nDocuments  Music      Public    randome2  snap         Videos\nsri@envy:~\n$ \n```\n---\n\n## Executing Dynamic Commands\nThe `eval` command in Unix/Linux shells is used to execute arguments as a command in the current shell environment. It evaluates the given string or expression, processes it, and then runs the result as a command. This makes it particularly useful in scenarios where commands are dynamically constructed or where special shell syntax needs to be interpreted.\n\n```bash\n# Syntax\n$ eval [arguments]\n\n# Example\nsri@envy:~/Documents/Linux\n$ eval \"ls -l\"\ntotal 3276\n-rw-rw-r-- 1 sri sri  734199 Dec 21 17:42 Linux_command_line_for_you_and_me_Release_0.1.pdf\n-rw-rw-r-- 1 sri sri 2349921 Dec 21 17:42 Linux_Succinctly.pdf\n-rw-rw-r-- 1 sri sri  265126 Jan  4 15:20 README.md\nsri@envy:~/Documents/Linux\n$ \n```\n\n### How It Works\n1. **First Pass**: `eval` takes its arguments as a single string and processes them.\n2. **Second Pass**: The shell then re-evaluates the processed result as a command.\n\n---\n\n### Use Cases\n#### Executing Dynamically Constructed Commands:\n```bash\nsri@envy:~/Documents/Linux\n$ cmd=\"echo Hello, World!\"\nsri@envy:~/Documents/Linux\n$ eval $cmd\nHello, World!\nsri@envy:~/Documents/Linux\n$ $cmd\nHello, World!\nsri@envy:~/Documents/Linux\n$ \n```\nHere, `eval` processes the variable `cmd` and executes the resulting command: `echo Hello, World!`.\n\n### Setting Environment Variables:\nFor example, starting the SSH agent:\n```bash\nsri@envy:~\n$ eval \"$(ssh-agent -s)\"\nSSH_AUTH_SOCK=/tmp/ssh-abc12345/agent.1234; export SSH_AUTH_SOCK;\nSSH_AGENT_PID=1234; export SSH_AGENT_PID;\necho Agent pid 1234;\n```\nThe `ssh-agent -s` outputs commands to set environment variables, and `eval` ensures those commands are executed in the current shell.\n\n### Combining Multiple Commands:\nIf you construct a command dynamically and need the shell to interpret it fully:\n```bash\n$ command=\"ls -l | grep 'myfile'\"\n$ eval $command\n```\n---\n\n### Why Use `eval`?\n- It allows you to execute commands that are dynamically generated or stored in variables.\n- It interprets and executes complex shell expressions that require multiple levels of processing.\n\n---\n\n### Caution\nThe `eval` command can be risky if used with untrusted input, as it can execute unintended or malicious commands. For example:\n```bash\ninput='rm -rf /'\neval $input  # Dangerous!\n```\nAlways sanitize or validate input before passing it to `eval`.\n\n---\n\n## Summary of Basic Commands\n\n| Command| Description |\n|------------------------|-------------|\n| `pwd`  | Displays the present working directory. |\n| `cd [directory]`   | Changes the current directory to the specified directory. |\n| `cd -` | Changes to the previous directory. |\n| `cd`   | Changes to the user's home directory. |\n| `cd ~` | Changes to the user's home directory. |\n| `cd ~user`  | Changes to the home directory of the user `user`. |\n| `cd /home/user` | Changes to the directory `/home/user`. |\n| `ls [path]`| Lists the contents of the specified path. |\n| `ls`   | Lists the contents of the current directory. |\n| `cat [file]`   | Displays the contents of a specified file. |\n| `cat`  | Reads from the standard input and displays the input. |\n| `exit` | Exits the current shell. |\n| `logout`   | Exits the current login session (not available in all shells). |\n| `^D` or `^d`   | Exits the shell using the shortcut `CTRL+D`. |\n| `man command`| Displays the manual page for a command. |\n| `man -k keyword` | Returns all man pages containing the specified keyword. |\n| `apropos keyword`| Searches for the keyword in the manual page descriptions. |\n| `[command] --help` | Displays a help message for the specified command. |\n| `whatis [command]`| Display one-line manual page descriptions |\n| `clear`| Clears the entire terminal screen. |\n| `^L` or `^l`   | Clears the terminal window (history still accessible via scroll). |\n| `touch file(s)`  | Creates empty file(s). |\n| `eval [arguments]` | Executes the command in arguments. |\n\n### Navigating man pages\n\n| Key  | Action   |\n|----------------------|--------------------------------------|\n| Enter, Down Arrow | Move down one line. |\n| Up Arrow, `k` | Move up one line.   |\n| Spacebar, Page Down   | Move down one page. |\n| `g`   | Go to the start or top. |\n| `G`   | Go to the end or bottom.|\n| `h`   | Display help.   |\n| `j`   | Move down one line. |\n| `k`   | Move up one line.   |\n| `l`   | Move right (scroll horizontally).   |\n| `q`   | Quit.   |\n\n---\n\n# Directories\nIn linux, we call **folder** as **directory**. In Linux, directories end with a trailing forward slash.\n\n## Absolute vs Relative Paths\n### Absolute Path: \nStarts with a `/` and points to a location from the root of the file system.\n```bash\nsri@envy:~/Documents/Linux\n$ cd /home/sri/Music/\nsri@envy:~/Music\n$ \n```\n\n### Relative Path: \nDoesn't start with a `/` and is relative to the current working directory.\n```bash\nsri@envy:~/Documents/Linux\n$ cd ../../Music/\nsri@envy:~/Music\n$ \n```\n\n## Home Directory\nThere are many ways to go to user's home directory from anywhere. \n```bash\n# 1. using \"cd\"\nsri@envy:~/Documents/Linux\n$ cd \nsri@envy:~\n$ \n\n# 2. using \"cd ~\"\nsri@envy:~/Documents/Linux\n$ cd ~\nsri@envy:~\n$ \n\n# 3. using \"cd ~sri\"\nsri@envy:~/Documents/Linux\n$ cd ~sri\nsri@envy:~\n$ \n\n# using \"cd /home/sri\"\nsri@envy:~/Documents/Linux\n$ cd /home/sri\nsri@envy:~\n$ \n```\n\n## Special Directory References\n- `.` represents the **current directory**.\n  ```bash\n  # Syntax\n  $ cd .   # Stays in the current directory\n\n  # Example\n  sri@envy:~/Documents/Linux\n  $ cd .\n  sri@envy:~/Documents/Linux\n  $\n  ```\n\n- `..` represents the **parent directory**.\n  ```bash\n  # Syntax\n  $ cd ..   # Moves up one level to the parent directory\n  \n  # Example\n  sri@envy:~/Documents/Linux\n  $ cd ..\n  sri@envy:~/Documents\n  $ \n  ```\n\n## Navigating Directories\n### Change directories using `cd`: \n```bash\n# Syntax\n$ cd [directory]\n\n# Example\nsri@envy:~/Documents/Linux\n$ cd ~/Music/\nsri@envy:~/Music\n$ pwd\n/home/sri/Music\nsri@envy:~/Music\n$ \n```\n\n### Return to the previous directory with `cd -`:\n```bash\n# Syntax:\n$ cd -\n\n# Example\nsri@envy:~/Documents/Linux\n$ cd ~/Music/\nsri@envy:~/Music\n$ cd -\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ \n```\n\nNote using `cd .` affects the navigation of `cd -`.\n```bash\nsri@envy:~\n$ cd ~/Documents/Linux/     # going to Linux\nsri@envy:~/Documents/Linux\n$ cd -                      # going to home\n/home/sri\nsri@envy:~\n$ cd -                      # going to Linux\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ cd .                      # Staying in Linux\nsri@envy:~/Documents/Linux\n$ cd -                      # Staying in Linux itself\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ cd -\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ cd -\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ \n\n# Internally uses $OLDPWD\n# When you use \"cd .\", \n# $OLDPWD gets modified.\n\nsri@envy:~\n$ cd ~/Documents/Linux/\nsri@envy:~/Documents/Linux\n$ echo $OLDPWD\n/home/sri\nsri@envy:~/Documents/Linux\n$ cd -\n/home/sri\nsri@envy:~\n$ echo $OLDPWD\n/home/sri/Documents/Linux\nsri@envy:~\n$ cd -\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ echo $OLDPWD\n/home/sri\nsri@envy:~/Documents/Linux\n$ cd .\nsri@envy:~/Documents/Linux\n$ echo $OLDPWD\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ cd -\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ cd -\n/home/sri/Documents/Linux\nsri@envy:~/Documents/Linux\n$ \n```\nThe loop happens because `cd -` toggles between the current directory and the last visited directory. When you use `cd .`, it doesn't change the directory, so the \"previous directory\" remains the same. Repeatedly using `cd -` in the same location keeps toggling to the same directory, causing the apparent loop.\n\n## Creating and Removing Directories\n### Create a directory with `mkdir`:\n```bash\n# Syntax\n$ mkdir newdir   # Creates a new directory called newdir\n\n# Example\nsri@envy:~/Desktop\n$ ls \nsri@envy:~/Desktop\n$ mkdir dir\nsri@envy:~/Desktop\n$ ls\ndir\nsri@envy:~/Desktop\n$ ls -ld dir\ndrwxrwxr-x 2 sri sri 4096 Jan  1 12:00 dir\nsri@envy:~/Desktop\n$ \n```\n\n### Create directories with `mkdir -p` (including intermediate directories):\n```bash\n# Syntax\n$ mkdir -p newdir/one/two   # Creates newdir, then one, and then two inside it\n\n# Example\nsri@envy:~/Desktop\n$ ls\nsri@envy:~/Desktop\n$ mkdir dir/sub-dir/sub-sub-dir\nmkdir: cannot create directory ‘dir/sub-dir/sub-sub-dir': No such file or directory\nsri@envy:~/Desktop\n$ mkdir -p dir/sub-dir/sub-sub-dir\nsri@envy:~/Desktop\n$ tree dir\ndir\n└── sub-dir\n    └── sub-sub-dir\n\n3 directories, 0 files\nsri@envy:~/Desktop\n$ \n\n```\n\n### Remove an empty directory with `rmdir`:\n```bash\n# Syntax\n$ rmdir newdir   # Removes the empty directory newdir\n\n# Example\nsri@envy:~/Desktop\n$ ls\ndir\nsri@envy:~/Desktop\n$ ls -l dir # Empty directory\ntotal 0\nsri@envy:~/Desktop\n$ rmdir dir\nsri@envy:~/Desktop\n$ ls\nsri@envy:~/Desktop\n$ \n\nsri@envy:~/Desktop\n$ ls\ndir\nsri@envy:~/Desktop\n$ ls -l dir # Empty directory\ntotal 0\nsri@envy:~/Desktop\n$ rm -r dir\nsri@envy:~/Desktop\n$ ls\nsri@envy:~/Desktop\n$ \n\n```\n\n### Forcefully remove a directory and its contents with `rm -rf`:\n\nConsider this setup to demonstrate the need for forcefull removal of directory. We creating a temporary git repository. \n```bash\nsri@envy:~/Desktop\n$ mkdir temp_repo\nsri@envy:~/Desktop\n$ cd temp_repo/\nsri@envy:~/Desktop/temp_repo\n$ touch a\nsri@envy:~/Desktop/temp_repo\n$ git init\nhint: Using 'master' as the name for the initial branch. This default branch name\nhint: is subject to change. To configure the initial branch name to use in all\nhint: of your new repositories, which will suppress this warning, call:\nhint: \nhint: \tgit config --global init.defaultBranch \u003cname\u003e\nhint: \nhint: Names commonly chosen instead of 'master' are 'main', 'trunk' and\nhint: 'development'. The just-created branch can be renamed via this command:\nhint: \nhint: \tgit branch -m \u003cname\u003e\nInitialized empty Git repository in /home/sri/Desktop/temp_repo/.git/\nsri@envy:~/Desktop/temp_repo\n$ git add .\nsri@envy:~/Desktop/temp_repo\n$ git commit -m \"sample commit\"\n[master (root-commit) 681a648] sample commit\n 1 file changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 a\n```\n\nNow, let's try to delete this directory with `rm -r`\n```bash\nsri@envy:~/Desktop/temp_repo\n$ cd .. \nsri@envy:~/Desktop\n$ rm -r temp_repo/\nrm: remove write-protected regular file 'temp_repo/.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391'? \n```\nThere will be lot of warnings, requesting us about deletion. The input `y` has to be given repeatedly to delete the directory entirely. Here is where `-f` option comes in, it allows us to delete the directory in one go, without any warnings. But this can be dangerous, since there is **no reversion** of a wrong delete. \n\n```bash\n# Syntax\n$ rm -rf newdir   # Deletes the newdir directory and all of its contents FORCEFULLY\n\n# Example\nsri@envy:~/Desktop\n$ rm -rf temp_repo/\nsri@envy:~/Desktop\n$ \n```\n\n## Summary of Directory Commands\n\n| Command  | Description   |\n|--------------------------|-------------------------------------------------------------------------------------------------------|\n| `cd [directory]` | Changes the current directory to the specified directory.|\n| `cd -`   | Returns to the previous directory.   |\n| `cd` | Changes to the home directory.   |\n| `cd ~`   | Changes to the home directory (shortcut).|\n| `cd ~[username]` | Changes to the home directory of the specified user. |\n| `cd /home/[username]`| Changes to the absolute path of the specified user's home directory. |\n| `cd .`   | Stays in the current directory.  |\n| `cd ..`  | Moves up one level to the parent directory.  |\n| `mkdir [directory]`  | Creates a new directory with the specified name. |\n| `mkdir -p [path]`| Creates nested directories, including intermediate ones, if they don't exist.|\n| `rmdir [directory]`  | Removes an empty directory.  |\n| `rm -r [directory]`  | Removes a directory and its contents recursively, prompting for confirmation for protected files.|\n| `rm -rf [directory]` | Removes a directory and its contents forcefully, without any prompts or confirmation.|\n| `pwd`| Displays the present working directory.  |\n| `ls [path]`  | Lists the contents of the specified directory.   |\n| `ls` | Lists the contents of the current directory.|\n| `tree [directory]`   | Displays the directory structure in a tree format.   |\n| `touch [file]`   | Creates an empty file with the specified name.   |\n| `git init`   | Initializes a new Git repository in the current directory.   |\n| `git add .`  | Stages all changes in the current directory for the next commit. |\n| `git commit -m [message]`| Creates a commit with the specified message. |\n\n---\n\n# Viewing File and Directory Details\n\nIn Linux, the `ls` command is used to list files and directories, but it can also provide detailed information about them when combined with different options. \n\n## Basic `ls` Command\n\nThe `ls` command by itself will simply list the files and directories in the current directory:\n```bash\n# Syntax\n$ ls\n\n# Example\nsri@envy:~\n$ ls\nDesktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos\nsri@envy:~\n$ \n```\n\n### Using `ls -l` for Detailed Listings\n\nThe `ls -l` command provides a long listing format that shows detailed information about files and directories. \n\n```bash\n# Syntax\n$ ls -l\n\n# Example\nsri@envy:~\n$ ls -l\ntotal 36\ndrwxr-xr-x  2 sri sri 4096 Jan  1 12:11 Desktop\ndrwxr-xr-x 13 sri sri 4096 Dec 29 18:04 Documents\ndrwxr-xr-x  2 sri sri 4096 Dec 31 20:15 Downloads\ndrwxr-xr-x  2 sri sri 4096 Dec 21 16:45 Music\ndrwxr-xr-x  3 sri sri 4096 Dec 23 15:41 Pictures\ndrwxr-xr-x  2 sri sri 4096 Dec 21 16:45 Public\ndrwx------  8 sri sri 4096 Dec 31 18:07 snap\ndrwxr-xr-x  2 sri sri 4096 Dec 21 16:45 Templates\ndrwxr-xr-x  2 sri sri 4096 Dec 21 16:45 Videos\nsri@envy:~\n$\n```\n\n### Breakdown of `ls -l` Output\n\nEach line in the output contains several columns with specific information:\n\n| **Field**  | **Description**  | **Example**|\n|------------------------|----------------------------------------------------------|------------------------|\n| **Permissions** | Indicates file type and access permissions. | `drwxr-xr-x`   |\n| **Links**   | Number of hard links to the file or directory.  | `2`|\n| **Owner**   | Name of the user who owns the file or directory.| `sri`  |\n| **Group**   | Group associated with the file or directory.| `sri`  |\n| **File Size**   | Size of the file in bytes.  | `4096` |\n| **Modification Time**   | Last modification date and time.| `Jan  1 12:11` |\n| **Name**| Name of the file or directory.  | `Desktop`  |\n\n---\n\n### Example Breakdown\n\nFor a specific file:\n```bash\n-rw-r--r-- 1 sri sri 512 Dec 31 21:00 shopping-list.txt\ndrwxr-xr-x 13 sri sri 4096 Dec 29 18:04 Documents\n\n```\n\n| **Field**  | **Value**   | **Explanation**  |\n|------------------------|---------------------|-----------------------------------------------------|\n| **Permissions** | `drwxr-xr-x`   | Regular directory with read/write/execute for owner, read and execute for group and others. |\n| **Links**   | `13`| Single hard link.|\n| **Owner**   | `sri`  | File is owned by the user `sri`.|\n| **Group**   | `sri`  | Associated group is `sri`.|\n| **File Size**   | `4096`  | File is 4096 bytes in size.|\n| **Modification Time**| `Dec 29 18:04`| File was last modified on Dec 31 at 9:00 PM.|\n| **Name**| `Document`| Name of the directory is `Document`.|\n\n## Displaying Hidden Files\n\nBy default, `ls` does not show hidden files (files that begin with a dot `.`). To display hidden files, use the `-a` option:\n```bash\n# Syntax\n$ ls -a\n\n# Example\nsri@envy:~\n$ ls -a\n.                 .cache      .gnupg    Public                     .vim\n..                .config     .lesshst  .python_history            .viminfo\n.aspell.en.prepl  Desktop     .local    snap                       .vimrc\n.aspell.en.pws    Documents   Music     .ssh                       .vscode\n.bash_history     .dotnet     Pictures  .sudo_as_admin_successful\n.bash_logout      Downloads   .pki      Templates\n.bashrc           .gitconfig  .profile  Videos\nsri@envy:~\n$ \n```\n\nTo list hidden files with details, use both the `-a` and `-l` options:\n\n```bash\nsri@envy:~\n$ ls -al\ntotal 164\ndrwxr-x--- 20 sri  sri   4096 Jan  1 11:58 .\ndrwxr-xr-x  3 root root  4096 Dec 30 14:41 ..\n-rw-rw-r--  1 sri  sri     24 Dec 30 18:03 .aspell.en.prepl\n-rw-rw-r--  1 sri  sri    183 Dec 30 18:03 .aspell.en.pws\n-rw-------  1 sri  sri  25677 Jan  1 07:14 .bash_history\n-rw-r--r--  1 sri  sri    220 Mar 31  2024 .bash_logout\n-rw-r--r--  1 sri  sri   4316 Dec 29 18:48 .bashrc\ndrwx------ 19 sri  sri   4096 Dec 25 20:21 .cache\ndrwx------ 23 sri  sri   4096 Dec 31 16:12 .config\ndrwxr-xr-x  2 sri  sri   4096 Jan  1 12:11 Desktop\ndrwxr-xr-x 13 sri  sri   4096 Dec 29 18:04 Documents\ndrwxrwxr-x  3 sri  sri   4096 Dec 21 17:15 .dotnet\ndrwxr-xr-x  2 sri  sri   4096 Dec 31 20:15 Downloads\n-rw-rw-r--  1 sri  sri    152 Dec 21 17:40 .gitconfig\ndrwx------  2 sri  sri   4096 Jan  1 11:50 .gnupg\n-rw-------  1 sri  sri     90 Jan  1 11:58 .lesshst\ndrwx------  4 sri  sri   4096 Dec 21 16:45 .local\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Music\ndrwxr-xr-x  3 sri  sri   4096 Dec 23 15:41 Pictures\ndrwx------  3 sri  sri   4096 Dec 21 17:15 .pki\n-rw-r--r--  1 sri  sri    807 Mar 31  2024 .profile\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Public\n-rw-------  1 sri  sri    329 Dec 31 12:34 .python_history\ndrwx------  8 sri  sri   4096 Dec 31 18:07 snap\ndrwx------  2 sri  sri   4096 Dec 21 17:29 .ssh\n-rw-r--r--  1 sri  sri      0 Dec 21 16:45 .sudo_as_admin_successful\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Templates\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Videos\ndrwxr-xr-x  2 sri  sri   4096 Dec 27 20:42 .vim\n-rw-------  1 sri  sri  15521 Dec 31 11:22 .viminfo\n-rw-rw-r--  1 sri  sri   1260 Dec 26 10:43 .vimrc\ndrwxrwxr-x  4 sri  sri   4096 Dec 21 17:15 .vscode\nsri@envy:~\n$ \n```\n\n## Colorized Output with `--color`\n\nTo add color to the `ls` output, use the `--color` option. This is **present by default**. This helps differentiate file types:\n```bash\n# Syntax\n$ ls --color\n```\n\n## File Type Indicators with `-F`\n\nThe `-F` option appends a symbol to each file or directory to indicate its type. Here's how it works:\n```bash\n# Syntax\n$ ls -aF\n\n# Example\nsri@envy:~\n$ ls -aF\n./                .config/    .local/          .sudo_as_admin_successful\n../               Desktop/    Music/           Templates/\na.out*            Documents/  Pictures/        Videos/\n.aspell.en.prepl  .dotnet/    .pki/            .vim/\n.aspell.en.pws    Downloads/  .profile         .viminfo\n.bash_history     .gitconfig  Public/          .vimrc\n.bash_logout      .gnupg/     .python_history  .vscode/\n.bashrc           hello.c     snap/\n.cache/           .lesshst    .ssh/\nsri@envy:~\n$ \n```\n\nFor a long listing with file type indicators:\n```bash\n# Syntax\n$ ls -alf\n\nsri@envy:~\n$ ls -alF\ntotal 184\ndrwxr-x--- 20 sri  sri   4096 Jan  1 13:20 ./\ndrwxr-xr-x  3 root root  4096 Dec 30 14:41 ../\n-rwxrwxr-x  1 sri  sri  15960 Jan  1 13:20 a.out*\n-rw-rw-r--  1 sri  sri     24 Dec 30 18:03 .aspell.en.prepl\n-rw-rw-r--  1 sri  sri    183 Dec 30 18:03 .aspell.en.pws\n-rw-------  1 sri  sri  25677 Jan  1 07:14 .bash_history\n-rw-r--r--  1 sri  sri    220 Mar 31  2024 .bash_logout\n-rw-r--r--  1 sri  sri   4316 Dec 29 18:48 .bashrc\ndrwx------ 19 sri  sri   4096 Dec 25 20:21 .cache/\ndrwx------ 23 sri  sri   4096 Dec 31 16:12 .config/\ndrwxr-xr-x  2 sri  sri   4096 Jan  1 12:11 Desktop/\ndrwxr-xr-x 13 sri  sri   4096 Dec 29 18:04 Documents/\ndrwxrwxr-x  3 sri  sri   4096 Dec 21 17:15 .dotnet/\ndrwxr-xr-x  2 sri  sri   4096 Dec 31 20:15 Downloads/\n-rw-rw-r--  1 sri  sri    152 Dec 21 17:40 .gitconfig\ndrwx------  2 sri  sri   4096 Jan  1 11:50 .gnupg/\n-rw-rw-r--  1 sri  sri     72 Jan  1 13:20 hello.c\n-rw-------  1 sri  sri     90 Jan  1 11:58 .lesshst\ndrwx------  4 sri  sri   4096 Dec 21 16:45 .local/\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Music/\ndrwxr-xr-x  3 sri  sri   4096 Dec 23 15:41 Pictures/\ndrwx------  3 sri  sri   4096 Dec 21 17:15 .pki/\n-rw-r--r--  1 sri  sri    807 Mar 31  2024 .profile\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Public/\n-rw-------  1 sri  sri    329 Dec 31 12:34 .python_history\ndrwx------  8 sri  sri   4096 Dec 31 18:07 snap/\ndrwx------  2 sri  sri   4096 Dec 21 17:29 .ssh/\n-rw-r--r--  1 sri  sri      0 Dec 21 16:45 .sudo_as_admin_successful\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Templates/\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Videos/\ndrwxr-xr-x  2 sri  sri   4096 Dec 27 20:42 .vim/\n-rw-------  1 sri  sri  14641 Jan  1 13:20 .viminfo\n-rw-rw-r--  1 sri  sri   1260 Dec 26 10:43 .vimrc\ndrwxrwxr-x  4 sri  sri   4096 Dec 21 17:15 .vscode/\nsri@envy:~\n$ \n```\n\nHere's a list of file type indicators used in Linux with the `ls` command:\n\n| **Symbol in Permission String** | **File Type**   | **Description**   | **Default Color**   |\n|------------|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------|\n| `-`| Regular file | Standard files that contain data, such as text, images, videos, or executables.| Default (white) |\n| `b`| Block special file   | Files that represent hardware devices capable of data storage, such as hard drives or USB drives, accessed in fixed-size blocks.   | Yellow with bold|\n| `c`| Character special file   | Files that represent hardware devices, such as keyboards or terminals, accessed character by character. Interfaces with devices that transfer data in streams. | Yellow with bold|\n| `d`| Directory| Containers for files and other directories, forming a hierarchical structure in the file system.   | Blue|\n| `l`| Symbolic link| Special files that act as shortcuts, pointing to another file or directory.| Cyan (light blue)   |\n| `p`| FIFO (named pipe)| Used in IPC. Files that act as conduits, allowing two processes to communicate by writing and reading data in a first-in, first-out (FIFO) order.   | Yellow (brown)  |\n| `s`| Socket   | Special files used for inter-process communication (IPC), typically for network services or local socket connections. Enables bidirectional communication. | Magenta (purple)|\n| `?`| Unknown file type| Files that do not match any recognized file type.  | N/A |\n| `*`| Executable   | Files that can be executed as programs or scripts.| Green   |\n| `/`| Directory (Indicator)| Indicates a directory in file listings.   | Blue|\n| `@`| Symlink (Symbolic link)  | Indicates a symbolic link in file listings.   | Cyan (light blue)   |\n| `=`| Socket (Indicator)   | Indicates a socket in file listings.  | Magenta (purple)|\n| `\\|`   | Named pipe (FIFO) (Indicator) | Indicates a named pipe in file listings.  | Yellow (brown)  |\n\n\n### Notes\nThe colors shown above are defaults in most Linux distributions. These may vary based on user configuration (`~/.bashrc`, `~/.zshrc`) or terminal settings. - Colors can be customized using the `LS_COLORS` environment variable. To check the current configuration, run:\n```bash\nsri@envy:~\n$ echo $LS_COLORS \nrs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=00:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:\n```\nThe `$LS_COLORS` variable defines the colors and styles for file types and extensions in `ls --color` output. It's a series of `key=value` pairs separated by colons (`:`):  \n\n- **Keys**: File types (`di`=directory, `ln`=symlink, `pi`=pipe, etc.) or extensions (`*.tar`, `*.zip`).  \n- **Values**: Style codes (`00`=default, `01`=bold) and colors (`31`=red, `34`=blue, `40`=black background, etc.).  \n\n### Examples\n- `di=01;34`: Directories are bold blue.  \n- `ln=01;36`: Symlinks are bold cyan.  \n- `*.tar=01;31`: `.tar` files are bold red.  \n\nCustomize with `dircolors` or directly export a new `LS_COLORS` value:\n```bash\n$ export LS_COLORS=\"di=01;32:ln=01;33\"\n```\n\n## Symbolic Links\n\nA **symbolic link** (also known as a symlink or soft link) is a special type of file in Linux that points to another file or directory. It acts as a shortcut, redirecting access to the original target file or directory.\n\n### Characteristics of Symbolic Links:\n- **Independent from the target:** If the target file or directory is moved or deleted, the symlink becomes \"broken,\" but the symlink itself remains.\n- **Flexible:** Can link to files or directories located anywhere in the file system, including remote locations.\n- **Different inode:** The symlink has its own inode, separate from the target file.\n\n### Creating a Symbolic Link\nUse the `ln -s` command to create a symbolic link:\n```bash\n# Syntax:\n$ ln -s [target] [symlink]\n\n# Example:\nsri@envy:~/Desktop\n$ ln -s /home/sri/Documents/myfile.txt myfilelink\nsri@envy:~/Desktop\n$ ls -F\nmyfilelink@\nsri@envy:~/Desktop\n$\n```\nThis creates a symbolic link `myfilelink` that points to `/home/sri/Documents/myfile.txt`.\n\n### Viewing Symbolic Links\nUse the `ls -l` command to identify symbolic links. The symlink is indicated with an `@` in `ls -F` or displayed with an arrow (`-\u003e`) pointing to the target:\n```bash\n# Syntax\n$ ls -lF\n\n# Example\nsri@envy:~/Desktop\n$ ls -alF\ntotal 8\ndrwxr-xr-x  2 sri sri 4096 Jan  1 13:30 ./\ndrwxr-x--- 20 sri sri 4096 Jan  1 13:25 ../\nlrwxrwxrwx  1 sri sri   30 Jan  1 13:30 myfilelink -\u003e /home/sri/Documents/myfile.txt\nsri@envy:~/Desktop\n$ \n```\n\n### Using a Symbolic Link\nYou can use a symbolic link just like the original file or directory. For example:\n- **Open the linked file:**\n  ```bash\n  sri@envy:~/Desktop\n  $ cat myfilelink\n  This is myfile.txt\n  sri@envy:~/Desktop\n  $ \n  ```\n- **Navigate to a linked directory:**\n  ```bash\n  $ cd mydirlink\n  ```\n\n### Managing Symbolic Links\n- **Remove a symlink:** Use the `rm` command (this removes only the symlink, not the target):\n  ```bash\n  $ rm mylink\n  ```\n- **Edit or recreate a symlink:** Re-run the `ln -s` command with the desired target.\n\n### Broken Symlinks\nA symlink becomes \"broken\" if the target is moved or deleted. You can identify broken symlinks with:\n```bash\n$ ls -l\nsri@envy:~/Desktop\n$ ls -l\ntotal 0\nlrwxrwxrwx 1 sri sri 30 Jan  1 13:30 myfilelink -\u003e /home/sri/Documents/myfile.txt (red font and highlighted)\n```\n\nSymbolic links are commonly used for tasks such as creating shortcuts, linking configuration files, or managing library paths efficiently.\n\n## Sorting Files by Time\n\nTo list files sorted by their modification time, use the `-t` option:\n```bash\n# Syntax\n$ ls -t\n\n# Example\nsri@envy:~\n$ ls -alt\ntotal 168\ndrwxr-xr-x  2 sri  sri   4096 Jan  1 13:36 Desktop\ndrwxr-xr-x 13 sri  sri   4096 Jan  1 13:35 Documents\ndrwxr-x--- 20 sri  sri   4096 Jan  1 13:25 .\n-rw-------  1 sri  sri  16617 Jan  1 13:25 .viminfo\n-rw-------  1 sri  sri     90 Jan  1 11:58 .lesshst\ndrwx------  2 sri  sri   4096 Jan  1 11:50 .gnupg\n-rw-------  1 sri  sri  25677 Jan  1 07:14 .bash_history\ndrwxr-xr-x  2 sri  sri   4096 Dec 31 20:15 Downloads\ndrwx------  8 sri  sri   4096 Dec 31 18:07 snap\ndrwx------ 23 sri  sri   4096 Dec 31 16:12 .config\n-rw-------  1 sri  sri    329 Dec 31 12:34 .python_history\n-rw-rw-r--  1 sri  sri     24 Dec 30 18:03 .aspell.en.prepl\n-rw-rw-r--  1 sri  sri    183 Dec 30 18:03 .aspell.en.pws\ndrwxr-xr-x  3 root root  4096 Dec 30 14:41 ..\n-rw-r--r--  1 sri  sri   4316 Dec 29 18:48 .bashrc\ndrwxr-xr-x  2 sri  sri   4096 Dec 27 20:42 .vim\n-rw-rw-r--  1 sri  sri   1260 Dec 26 10:43 .vimrc\ndrwx------ 19 sri  sri   4096 Dec 25 20:21 .cache\ndrwxr-xr-x  3 sri  sri   4096 Dec 23 15:41 Pictures\n-rw-rw-r--  1 sri  sri    152 Dec 21 17:40 .gitconfig\ndrwx------  2 sri  sri   4096 Dec 21 17:29 .ssh\ndrwxrwxr-x  3 sri  sri   4096 Dec 21 17:15 .dotnet\ndrwxrwxr-x  4 sri  sri   4096 Dec 21 17:15 .vscode\ndrwx------  3 sri  sri   4096 Dec 21 17:15 .pki\n-rw-r--r--  1 sri  sri      0 Dec 21 16:45 .sudo_as_admin_successful\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Music\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Public\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Templates\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Videos\ndrwx------  4 sri  sri   4096 Dec 21 16:45 .local\n-rw-r--r--  1 sri  sri    220 Mar 31  2024 .bash_logout\n-rw-r--r--  1 sri  sri    807 Mar 31  2024 .profile\nsri@envy:~\n$ \n```\nTo reverse the order (show the oldest files first), use `-r`:\n```bash\n# Syntax \n$ ls -lr\n\n# Example\nsri@envy:~\n$ ls -altr\ntotal 168\n-rw-r--r--  1 sri  sri    807 Mar 31  2024 .profile\n-rw-r--r--  1 sri  sri    220 Mar 31  2024 .bash_logout\ndrwx------  4 sri  sri   4096 Dec 21 16:45 .local\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Videos\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Templates\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Public\ndrwxr-xr-x  2 sri  sri   4096 Dec 21 16:45 Music\n-rw-r--r--  1 sri  sri      0 Dec 21 16:45 .sudo_as_admin_successful\ndrwx------  3 sri  sri   4096 Dec 21 17:15 .pki\ndrwxrwxr-x  4 sri  sri   4096 Dec 21 17:15 .vscode\ndrwxrwxr-x  3 sri  sri   4096 Dec 21 17:15 .dotnet\ndrwx------  2 sri  sri   4096 Dec 21 17:29 .ssh\n-rw-rw-r--  1 sri  sri    152 Dec 21 17:40 .gitconfig\ndrwxr-xr-x  3 sri  sri   4096 Dec 23 15:41 Pictures\ndrwx------ 19 sri  sri   4096 Dec 25 20:21 .cache\n-rw-rw-r--  1 sri  sri   1260 Dec 26 10:43 .vimrc\ndrwxr-xr-x  2 sri  sri   4096 Dec 27 20:42 .vim\n-rw-r--r--  1 sri  sri   4316 Dec 29 18:48 .bashrc\ndrwxr-xr-x  3 root root  4096 Dec 30 14:41 ..\n-rw-rw-r--  1 sri  sri    183 Dec 30 18:03 .aspell.en.pws\n-rw-rw-r--  1 sri  sri     24 Dec 30 18:03 .aspell.en.prepl\n-rw-------  1 sri  sri    329 Dec 31 12:34 .python_history\ndrwx------ 23 sri  sri   4096 Dec 31 16:12 .config\ndrwx------  8 sri  sri   4096 Dec 31 18:07 snap\ndrwxr-xr-x  2 sri  sri   4096 Dec 31 20:15 Downloads\n-rw-------  1 sri  sri  25677 Jan  1 07:14 .bash_history\ndrwx------  2 sri  sri   4096 Jan  1 11:50 .gnupg\n-rw-------  1 sri  sri     90 Jan  1 11:58 .lesshst\n-rw-------  1 sri  sri  16617 Jan  1 13:25 .viminfo\ndrwxr-x--- 20 sri  sri   4096 Jan  1 13:25 .\ndrwxr-xr-x 13 sri  sri   4096 Jan  1 13:35 Documents\ndrwxr-xr-x  2 sri  sri   4096 Jan  1 13:36 Desktop\nsri@envy:~\n$ \n```\n\n## Recursive Listing with `-R`\n\nTo list all files and directories recursively, use the `-R` option:\n```bash\n# Syntax\n$ ls -R\n.:\nDesktop  Documents  Downloads  link-to-to-do  Music  program  to-do.txt\n\n./Desktop:\n...\n\n./Documents:\ncat.jpg  report.txt\n\n./Downloads:\n...\n\n./Music:\nJohnColtrane\n\n./Music/JohnColtrane:\ngiant-steps.mp3\n```\n\n## Working with Directories Using `-d`\n\nIf you want to list the **directory itself** (without showing its contents), use the `-d` option:\n```bash\n# Syntax\n$ ls -d [Directory]\n\n# Example\nsri@envy:~\n$ ls -d Music/\nMusic/\nsri@envy:~\n$ ls -ld Music/\ndrwxr-xr-x 2 sri sri 4096 Dec 21 16:45 Music/\nsri@envy:~\n$ \n```\n\n## Escaping Special Characters in Filenames\n\nFiles or directories with spaces or special characters in their names require special handling. You can either **use quotes** or **escape the characters with a backslash** (`\\ ` - escapes the whitespace).\n\n```bash\n# Syntax\n$ ls \"file name with space\"\n$ ls file\\ name\\ with\\ space\n\n# Example\nsri@envy:~\n$ touch \"my to do list\"\nsri@envy:~\n$ ls -b\nDesktop    Downloads  my\\ to\\ do\\ list  Public  Templates\nDocuments  Music      Pictures          snap    Videos\nsri@envy:~\n$ ls -l my\\ to\\ do\\ list \n-rw-rw-r-- 1 sri sri 0 Jan  1 14:11 'my to do list'\nsri@envy:~\n$ ls -l \"my to do list\" \n-rw-rw-r-- 1 sri sri 0 Jan  1 14:11 'my to do list'\nsri@envy:~\n$ \n```\n\n### Using `-b` to View Escaped Names:\n```bash\n# Syntax\n$ ls -b\n\n# Example\nsri@envy:~\n$ ls -b\nDesktop    Downloads  my\\ to\\ do\\ list  Public  Templates\nDocuments  Music      Pictures          snap    Videos\nsri@envy:~\n$\n```\n\n\n## Tree Command for Visual Directory Structure\n\nThe `tree` command provides a more visually appealing way to view directory structures. \n\n`tree` is not installed by default: \n```bash\n$ sudo apt install tree\n```\n\nYou can use it as follows:\n```bash\n# Syntax\n$ tree\n.\n|-- Desktop\n|-- Documents\n|   |-- cat.jpg\n|   |-- report.txt\n|-- Downloads\n|-- Music\n|   |-- JohnColtrane\n|   |-- giant-steps.mp3\n|-- program\n|-- to-do.txt\n5 directories, 6 files\n```\n\nFor only directories, use `-d`:\n```bash\n# Syntax\n$ tree -d\n.\n|-- Desktop\n|-- Documents\n|-- Downloads\n|-- Music\n|-- JohnColtrane\n5 directories\n\n# Example\nsri@envy:~\n$ tree -d\n.\n├── Desktop\n├── Documents\n│   ├── Git\n│   ├── Introduction-to-Vedanta\n│   │   ├── Audio\n│   │   └── Summary\n│   ├── Linux\n│   ├── Markdown\n│   ├── Nitya-Karma\n│   │   ├── Sandhyavandanam\n│   │   ├── Shlokas\n│   │   └── Stotrams\n│   │       ├── Hanuman_Chalisa\n│   │       ├── Nama_Ramayanam\n│   │       └── Vishnu_Sahasranamam\n│   ├── Power-Of-Posture\n│   ├── Python\n│   ├── Python-DSA\n│   │   ├── Chapter-01\n│   │   │   └── Question-10\n│   │   │       └── __pycache__\n│   │   ├── Chapter-02\n│   │   │   ├── Programming-Exercises\n│   │   │   ├── Self-Check\n│   │   │   └── TimeitExperiments\n│   │   ├── Chapter-03\n│   │   │   ├── Deque\n│   │   │   │   └── __pycache__\n```\n\nTo colorize the output, use `-C` (by deafult this option is used):\n```bash\n# Syntax\n$ tree -C\n```\n\n## Summary of Viewing File and Directory Commands\n| Command | Description  |\n|---------------------------------|----------------------------------------------------------------------------------------------|\n| `ls`  | Lists files and directories in the current directory.|\n| `ls -l`   | Lists files and directories with detailed information (long format).|\n| `ls -a`   | Lists all files, including hidden files (starting with `.`).|\n| `ls --color`  | Displays files with color-coded output (for file types).|\n| `ls -F`   | Appends indicators (e.g., `/` for directories, `*` for executables) to file names.  |\n| `ls -t`   | Lists files sorted by modification time (most recent first).|\n| `ls -r`   | Reverses the order of the file list.|\n| `ls -R`   | Recursively lists all files and directories.|\n| `ls -d [Directory]`   | Displays information about directories, not their contents. |\n| `ls \"file name with space\"`   | Lists a file with spaces in its name by enclosing it in quotes. |\n| `ls file\\ name\\ with\\ space`  | Lists a file with spaces in its name by escaping spaces with a backslash (`\\`). |\n| `ls -b`   | Displays file names with non-printable characters as escape sequences.  |\n| `ls -alf` | Combines options: lists all files, including hidden ones, in long format.   |\n| `ls -altr`| Lists all files, including hidden ones, in long format, sorted by time in reverse order.|\n| `echo $LS_COLORS` | Displays the current color settings for the `ls` command.   |\n| `export LS_COLORS=\"di=01;32:ln=01;33\"` | Customizes the `ls` color scheme (e.g., green for directories, yellow for links).  |\n| `ln -s [target] [symlink]`| Creates a symbolic link named `[symlink]` pointing to `[target]`.   |\n| `cat myfilelink`  | Displays the contents of the file pointed to by the symbolic link `myfilelink`. |\n| `cd mydirlink`| Changes the current directory to the one pointed to by the symbolic link `mydirlink`.   |\n| `tree`| Displays the directory structure as a tree.|\n| `tree -d` | Displays only directories in the tree structure.   |\n| `tree -C` | Displays the tree structure with color-coded output.   |\n\n---\n\n# Permissions \n\nIn linux permissions are called **modes**. \n\n## Understanding File Types\n\nThe `ls -l` command displays long listings of files and directories, including the file types. \n\n```bash\nsri@envy:~/Documents/Linux\n$ ls -l\ntotal 3248\n-rw-rw-r-- 1 sri sri  236154 Jan  3 05:21 README.md\n^\n^\n```\nThe first character of the permission string indicates the file type. Here are the common file types:\n\n| **Symbol** | **File Type** | **Example** |\n|------------|---------------|-------------|\n| `-`| Regular file| `-rw-r--r-- 1 sri users 10400 Jun 14 09:31 sales.data`|\n| `b`| Block special file  | `brw-rw---- 1 root disk 8, 0 Jan  1 00:00 sda`|\n| `c`| Character special file | `crw-rw-rw- 1 root tty 4, 1 Jan  1 00:00 tty1`|\n| `d`| Directory| `drwxr-xr-x 2 sri users 4096 Jun 14 09:31 documents`|\n| `l`| Symbolic link| `lrwxrwxrwx 1 sri users   11 Jun 14 09:31 mylink -\u003e /etc/hosts`|\n| `p`| FIFO (named pipe)| `prw-r--r-- 1 sri users    0 Jun 14 09:31 mypipe`|\n| `s`| Socket| `srw-rw-rw- 1 sri users    0 Jun 14 09:31 mysocket`|\n| `?`| Unknown file type| `?-????????? ? ?    ?       ?            ? unknownfile`|\n\nThe remaining characters in the permission string represent three main types of permissions.\n\n## Types of Permissions\n\nPermissions are represented by three symbols: **read (`r`)**, **write (`w`)**, and **execute (`x`)**. Each permission grants specific abilities over files and directories.\n\n| **Symbol** | **Permission** |\n|------------|----------------|\n| `r`        | Read           |\n| `w`        | Write          |\n| `x`        | Execute        |\n\n### Permission Types for Directories\n\nFor files, the meanings of these permissions are straightforward:\n- **Read** (`r`): Allows viewing the file's contents.\n- **Write** (`w`): Allows modifying the file.\n- **Execute** (`x`): Allows running the file as a program.\n\n### Permission Types for Directories\n\nFor directories, the meanings differ slightly:\n- **Read** (`r`): Allows viewing the names of files in the directory.\n- **Write** (`w`): Allows modifying the directory's contents (creating, renaming, or deleting files).\n- **Execute** (`x`): Allows entering the directory (`cd` command).\n\n```bash\nsri@envy:~\n$ ls -ld /home/sri/Documents/\ndrwxr-xr-x 13 sri sri 4096 Jan  1 13:35 /home/sri/Documents/\n```\n\nIn this case:\n- `drwxr-xr-x`: Indicates a directory (`d` at the beginning).\n- The permissions `rwxr-xr-x` mean that the owner (`sri`) can read, write, and execute; the group (`sri`) can read and execute, but not write; others can also read and execute but cannot write.\n\n\u003e **Note** the permission `r` for a directory allows an entity to view the entries of the directory, but to read the content of items present in the directory, they need appropriate file permissions. \n\n## User Categories for Permissions\n\nAll files in Linux are owned by a user and a group. This allows for unique permissions to be applied for three sets of users for managing file ownership and permissions:\n1. **User** (`u`): The file owner.\n2. **Group** (`g`): The group associated with the file.\n3. **Other** (`o`): All other users.\n4. **All** (`a`): Refers to all three categories (`u`, `g`, and `o`).\n\nWhen modifying file permissions, these categories are referenced to define who gets which permission.\n\n| **Symbol** | **Category** |\n|------------|--------------|\n| `u`| User |\n| `g`| Group|\n| `o`| Other|\n| `a`| All  |\n\nFor example, \n```bash\n$ ls -l sales.data\n-rw-r--r-- 1 sri users 10400 Jun 14 09:31 sales.data\n```\n\nHere:\n- **User** (`sri`) is the file owner.\n- **Group** (`users`) is the group owner.\n- **Others** are anyone else who is not the owner or part of the group.\n\n### Group\nEvery user is a member of at least one group, called their primary group. However, users can be members of many groups. Groups bring in user organization, eg. group called \"sales\" can contain all the employees from sales department. \n\nTo see which groups you belong to, you can run the following commands:\n\n```bash\n# Syntax\n$ groups\n\n# Example\nsri@envy:~\n$ groups\nsri adm cdrom sudo dip plugdev users lpadmin\nsri@envy:~\n$ \n\n# Syntax\n$ id\n$ id -G\n$ id -Gn\n\n# Examples\nsri@envy:~\n$ id\nuid=1000(sri) gid=1000(sri) groups=1000(sri),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),100(users),114(lpadmin)\nsri@envy:~\n$ id -G\n1000 4 24 27 30 46 100 114\nsri@envy:~\n$ id -Gn\nsri adm cdrom sudo dip plugdev users lpadmin\nsri@envy:~\n$ \n```\n\nFor another user:\n\n```bash\n# Syntax\n$ groups [username]\n\n# Example\nsri@envy:~/Documents/Linux\n$ groups root\nroot : root\nsri@envy:~/Documents/Linux\n$ groups sri\nsri : sri adm cdrom sudo dip plugdev users lpadmin\nsri@envy:~/Documents/Linux\n$ \n```\n\nThese commands display the groups the user belongs to, which help in determining the permissions granted to each user category.\n\n\n## Decoding Permissions\n\nLinux file permissions are displayed using a **10-character string**, which can be decoded as follows:  \n\n1. **File Type** (1st Character):  \n- Indicates the type of file.  \n  - `-` = Regular file  \n  - `d` = Directory  \n  - `l` = Symbolic link  \n\n1. **Permissions** (Next 9 Characters):  \n- Split into **user (owner)**, **group**, and **others**.  \n- Each group has **read (r)**, **write (w)**, and **execute (x)** permissions.  \n- If a permission is missing, it's represented by `-`.\n\n1. **Trailing Characters in Permission Strings**  \n* Sometimes, additional characters indicate special access control methods:  \n  - **`.` (Trailing Period)**: Indicates an SELinux security context.  \n  - **`+` (Trailing Plus)**: Indicates Access Control Lists (ACLs).\n\n```bash\n$ ls -l sales.data\n-rw-r--r-- 1 sri users 10400 Jun 14 09:31 sales.data\n```\n- File type: `-` (regular file)  \n- User (owner): `rw-` (read and write)  \n- Group: `r--` (read-only)  \n- Others: `r--` (read-only)  \n- Owner: `sri`  \n- Group: `users`  \n\n```bash\n$ ls -l sales.data.selinux\n-rw-r--r--. 1 sri users 1040 Jun 14 09:31 sales.data.selinux\n# SELinux context applied.\n\n$ ls -l sales.data.acl\n-rw-r--r--+ 1 sri users 1040 Jun 14 09:31 sales.data.acl\n# ACLs are in use.\n```\n\n**Tips for Troubleshooting:**  \n- If you notice a trailing character (`.` or `+`), additional investigation may be required, as these security mechanisms can affect file access.  \n\n## Changing Permissions\n\nLinux provides the `chmod` (**change mode**) command to modify file permissions. Permissions can be changed either using **symbolic** or **numeric (octal) modes**.\n\n### 1. Symbolic Mode Permissions\n\nIn **symbolic mode**, you can modify file or directory permissions in a human-readable format. The symbolic notation allows you to specify **who** the change applies to, **what action** to perform, and **which permissions** to apply or remove.\n\n```bash\n# Syntax\n$ chmod [user][operation][permissions] [file]\n```\n\n#### Components\n1. **[user]:** Specifies which set of users the change applies to:\n   - `u`: The owner of the file (user).\n   - `g`: The group associated with the file.\n   - `o`: All other users (others).\n   - `a`: All users (shorthand for `u`, `g`, and `o`).\n\n2. **[operation]:** Defines what action to take:\n   - `+`: Add the specified permissions if not already present.\n   - `-`: Remove the specified permissions if not already present.\n   - `=`: Set the permissions **exactly** as specified, overriding existing ones.\n\n3. **[permissions]:** The type of access being granted or removed:\n   - `r`: Read permission (view file contents).\n   - `w`: Write permission (modify file contents).\n   - `x`: Execute permission (run the file as a program/script).\n\n---\n\n#### Examples\n\n##### 1. Add execute permission for the user (owner):\n```bash\n# Syntax\n$ chmod u+x myscript.sh\n\n# Example\nsri@envy:~/Desktop\n$ ls afile.txt -l\n-rw-rw-r-- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod u+x afile.txt \nsri@envy:~/Desktop\n$ ls -l afile.txt \n-rwxrw-r-- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n```\n- Adds execute (`x`) permission for the file's owner (`u`).\n- After this, the owner can run the script as a program.\n\n##### 2. Remove write permission for the group:\n```bash\n# Syntax\n$ chmod g-w myscript.sh\n\n# Example\nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rwxrwxr-- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod g-x afile.txt\nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rwxrw-r-- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n\n```\n- Removes write (`w`) permission for the file's group (`g`).\n- Members of the group can no longer modify the file.\n\n##### 3. Set read-only permission for others:\n```bash\n# Syntax\n$ chmod o=r file.txt\n\n# Example\nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rwxrw-rwx 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod o=r afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rwxrw-r-- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n```\n- Sets the permission for others (`o`) to read-only (`r`).\n- Removes any existing write or execute permissions for others.\n\n##### 4. Allow all users to execute a file:\n```bash\n# Syntax\n$ chmod a+x program.sh\n\n# Example\nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---------- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod a+x afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---x--x--x 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n```\n- Adds execute permission for all users (`a`), including owner, group, and others.\n\n##### 5. Set exact permissions for the group:\n```bash\n# Syntax\n$ chmod g=rw myfile.txt\n\n# Example\nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---x--x--x 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod g=rw afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---xrw---x 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n```\n- Sets the group (`g`) permissions to read (`r`) and write (`w`) only.\n- Removes any execute or other permissions previously set for the group.\n\n##### 6. Remove all permissions for others:\n```bash\n# Syntax\n$ chmod o= mydoc.txt\n\n# Example\nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---xrw---x 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod o= afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---xrw---- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n```\n- Removes all permissions for others (`o`), making the file inaccessible to them.\n- Simiarly you can remove all the permission for others as well using `a=`.\n\n##### 7. Multiple Changes in One Command:\nYou can modify permissions for different categories simultaneously. For example:\n```bash\n# Sytnax\n$ chmod u+x,g-w,o=r file.txt\n\n# Example\nsri@envy:~/Desktop\n$ chmod a= afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---------- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod a+x,g+r,u+rw afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rwxr-x--x 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n```\n\n##### 8. Explicit Permission Setting:\nThe equal sign (`=`) can be used to overwrite existing permissions for a user category:\n```bash\n# Syntax\n$ chmod u=rwx file.txt\n\n# Example\nsri@envy:~/Desktop\n$ chmod u=wx afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n--wxr-x--x 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n\n```\nThis sets the user's permissions to read, write, and execute, overriding any existing permissions for the user category. \n\n---\n\n\n### 2. Numeric Mode Permissions\n\nIn **numeric (octal) mode**, file permissions are represented using a three-digit number, where each digit specifies the permissions for a different category of users: **owner (user)**, **group**, and **others**. Each digit is the sum of the numeric values assigned to the permissions:\n\n- `r` (read) = **4**\n- `w` (write) = **2**\n- `x` (execute) = **1**\n- No permission = **0**\n\nBy adding these values together, you can represent any combination of permissions numerically.\n\n---\n\n#### Permissions Table\n\nThe following table shows how the numeric values correspond to permission combinations:\n\n| **Permissions** | **Numeric Value** | **Description** |\n|------------------|-------------------|-------------------------------------|\n| `rwx`| 7 | Read, write, and execute|\n| `rw-`| 6 | Read and write  |\n| `r-x`| 5 | Read and execute|\n| `r--`| 4 | Read only   |\n| `-wx`| 3 | Write and execute   |\n| `-w-`| 2 | Write only  |\n| `--x`| 1 | Execute only|\n| `---`| 0 | No permissions  |\n\n---\n\n#### How It Works\n\n For example, `chmod 754` assigns:\n- **7** (rwx): Owner has full permissions (read, write, execute).\n- **5** (r-x): Group can read and execute, but not write.\n- **4** (r--): Others can only read.\n\n1. **Structure of Permissions**:\n   - The **first digit** applies to the file **owner**.\n   - The **second digit** applies to the **group**.\n   - The **third digit** applies to **others** (everyone else).\n\n2. **Calculating the Numeric Value**:\n   - Add the numeric values for the desired permissions.\n   - Example: `rw-` (read + write) = `4 + 2 = 6`.\n\nThe below method of specification also works: \n```bash\nsri@envy:~/Desktop\n$ chmod 7 dir\nsri@envy:~/Desktop\n$ ls -ld dir\nd------rwx 2 sri sri 4096 Jan  3 10:58 dir\n\nsri@envy:~/Desktop\n$ chmod 77 dir\nsri@envy:~/Desktop\n$ ls -ld dir\nd---rwxrwx 2 sri sri 4096 Jan  3 10:58 dir\n\nsri@envy:~/Desktop\n$ chmod 777 dir \nsri@envy:~/Desktop\n$ ls -ld dir\ndrwxrwxrwx 2 sri sri 4096 Jan  3 10:58 dir\n\nsri@envy:~/Desktop\n$ \n```\n\n\n---\n\n\n#### Examples of chmod in Numeric Mode\n\n##### 0. Remove all permissions (lock a file):\n```bash\n$ chmod 000 secretfile.txt\n```\n- **0** (---): Owner, group, and others have no access.\n\n##### 1. Give the owner full permissions, and the group and others read-only:\n```bash\n# Syntax\n$ chmod 744 myfile.txt\n\n# Example\nsri@envy:~/Desktop\n$ chmod 000 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---------- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod 774 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rwxrwxr-- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n\n```\n- **7** (rwx): Owner can read, write, and execute.\n- **4** (r--): Group can only read.\n- **4** (r--): Others can only read.\n\n##### 2. Give the owner read and write permissions, and no permissions to the group and others:\n```bash\n# Syntax\n$ chmod 600 myfile.txt\n\n# Example\nsri@envy:~/Desktop\n$ chmod 000 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---------- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod 600 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rw------- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n\n```\n- **6** (rw-): Owner can read and write.\n- **0** (---): Group has no permissions.\n- **0** (---): Others have no permissions.\n\n##### 3. Give everyone execute permission only:\n```bash\n# Syntax\n$ chmod 111 script.sh\n\n# Example\nsri@envy:~/Desktop\n$ chmod 000 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---------- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod 111 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---x--x--x 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n\n```\n- **1** (--x): Owner can only execute.\n- **1** (--x): Group can only execute.\n- **1** (--x): Others can only execute.\n\n##### 4. Set permissions for a shared script:\n```bash\n# Syntax\n$ chmod 775 shared_script.sh\n\n# Example\nsri@envy:~/Desktop\n$ chmod 000 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---------- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod 775 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rwxrwxr-x 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n```\n- **7** (rwx): Owner can read, write, and execute.\n- **7** (rwx): Group can read, write, and execute.\n- **5** (r-x): Others can read and execute, but not write.\n\n##### 5. Make a file readable and writable by everyone:\n```bash\n# Syntax\n$ chmod 666 openfile.txt\n\n# Example\nsri@envy:~/Desktop\n$ chmod 000 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n---------- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ chmod 666 afile.txt \nsri@envy:~/Desktop\n$ ls -l\ntotal 0\n-rw-rw-rw- 1 sri sri 0 Jan  3 05:43 afile.txt\nsri@envy:~/Desktop\n$ \n```\n- **6** (rw-): Owner can read and write.\n- **6** (rw-): Group can read and write.\n- **6** (rw-): Others can read and write.\n\n\n\n#### Key Benefits of Numeric Mode\n- **Precision**: Numeric mode explicitly defines all permissions at once.\n- **Efficiency**: A single command can adjust permissions for all user categories.\n- **Control**: You can quickly lock or open a file for specific users.\n\n---\n\n#### Common Octal Permissions\n\nHere are some commonly used permission sets and their symbolic, binary, and octal representations:\n\n| **Symbolic**   | **Octal** | **Meaning**  |\n|----------------|-----------|------------------------------------------------------------------------------------------------------|\n| `-rwx------`   | `700` | Owner has full control over the file; no one else has access. For confidential file|\n| `-rwxr-xr-x`  | `755` | Owner has full control; everyone else can execute the file but only the owner can edit it.|\n| `-rw-rw-r--`  | `664` | Owner and group members can modify the file; others can only read it.|\n| `-rw-rw----`  | `660` | Owner and group members can modify the file; others have no access.  |\n| `-rw-r--r--`  | `644` | Owner can modify the file; everyone else can only read it. For public document|\n\n\n### Understanding Security Risks with Permissions\n\nIt's crucial to avoid overly permissive permissions, such as `777` and `666`. These allow anyone on the system to read, write, or execute the file, which poses significant security risks. Some of the potential issues include:\n\n- **Accidental Changes**: If a file has `777` permissions, anyone on the system can modify it, which could lead to accidental changes.\n- **Privilege Escalation**: Malicious users could modify a script or program, introduce harmful code, and cause it to execute when someone else runs the file.\n- **Data Destruction**: With `777` or `666` permissions, malicious or careless users can delete or corrupt important data.\n\n### Best Practice:\n* Use appropriate permissions for different users and groups. \n* Limit access to sensitive files and make use of groups to provide controlled access. \n* Avoid using `777` or `666` permissions unless absolutely necessary. For example, if multiple users need write access, use groups and restrict permissions for others.\n\n## Working with Groups\n\nWhen multiple users need access to the same file, Linux group ownership and permissions are useful tools for managing file access.\n\n\n#### To view existing Groups\n```bash\n# Syntax\n$ cat /etc/group\n\n# Example\nsri@envy:~/Desktop\n$ cat /etc/group\nroot:x:0:\ndaemon:x:1:\nbin:x:2:\nsys:x:3:\nadm:x:4:syslog,sri\ntty:x:5:\ndisk:x:6:\n```\nThe output shows the contents of the `/etc/group` file, which defines the groups on a Linux system. Each line represents a group and follows this format:\n\n```bash\nGROUP_NAME:PASSWORD:GID:MEMBERS\n```\n\n- **GROUP_NAME**: The name of the group.  \n- **PASSWORD**: Typically `x`, indicating that group passwords are stored in a shadow file (not commonly used anymore).  \n- **GID**: The Group ID, a unique numeric identifier for the group.  \n- **MEMBERS**: A comma-separated list of users who belong to the group.\n\n---\n\n#### Creating a Group\n```bash\n# Syntax\n$ groupadd [options] GROUP_NAME\n\n# Example\nsri@envy:~/Desktop\n$ groupadd a_new_group\ngroupadd: Permission denied.\ngroupadd: cannot lock /etc/group; try again later.\nsri@envy:~/Desktop\n$ sudo groupadd a_new_group\n[sudo] password for sri: \nsri@envy:~/Desktop\n$ cat /etc/group | grep a_new_\na_new_group:x:1001:\nsri@envy:~/Desktop\n$ \n```\n\n---\n\n### Adding Users to the Group \n\nTo add a user (e.g., `sri`) to the group:  \n```bash\n# Syntax\n$ sudo usermod -aG group userid\n\n# Example\nsri@envy:~/Desktop\n$ sudo usermod -aG a_new_group sri\nsri@envy:~/Desktop\n$ groups sri | grep a_new\nsri : sri adm cdrom sudo dip plugdev users lpadmin a_new_group\nsri@envy:~/Desktop\n$ \n```\n---\n\n### Creating a Group with a Specific GID\nIf you want to assign a specific Group ID (GID) to the group:  \n```bash\n# Syntax\n$ sudo groupadd -g \u003cnumber\u003e \u003cgroup_name\u003e\n\n# Example\nsri@envy:~/Desktop\n$ sudo groupadd another_new_group -g 2000 \nsri@envy:~/Desktop\n$ cat /etc/group | grep another\nanother_new_group:x:2000:\nsri@envy:~/Desktop\n$ \n```\n\n---\n\n### Deleting a Group\nIf you need to delete a group:  \n```bash\n# Syntax\n$ sudo groupdel \u003cgroup_name\u003e\n\n# Examples\nsri@envy:~/Desktop\n$ sudo groupdel another_new_group \nsri@envy:~/Desktop\n$ sudo groupdel a_new_group \nsri@envy:~/Desktop\n$ \n```\n\n---\n\n### Summary of group related commands\n\n| **Command**   | **Description**  |\n|---------------------------|----------------------------------------------|\n| `sudo groupadd GROUP_NAME`| Create a group   |\n| `cat /etc/group`  | List all groups  |\n| `sudo usermod -aG GROUP_NAME USER_NAME` | Add a user to a group  |\n| `groups USER_NAME`| Check user groups|\n| `sudo groupdel GROUP_NAME`| Delete a group   |\n\n---\n\n### Changing Group Ownership\n\nUse the `chgrp` command to change the group owner of a file. \n\n**Example:**  \nFirst let's create a group called `sales`.\n```bash\nsri@envy:~/Desktop\n$ sudo groupadd sales\nsri@envy:~/Desktop\n$ cat /etc/group | grep sal\nsales:x:1001:\nsri@envy:~/Desktop\n$ \n```\nCreate a file, `sales.report`, with default group ownership:  \n```bash\nsri@envy:~/Desktop\n$ touch sales.report\nsri@envy:~/Desktop\n$ ls -l sales.report \n-rw-rw-r-- 1 sri sri 0 Jan  3 06:53 sales.report\nsri@envy:~/Desktop\n$ \n```\n- Default group: `sri` (sri's primary group).  \n\nChange group ownership to `sales`:  \n```bash\nsri@envy:~/Desktop\n$ ls -l sales.report \n-rw-rw-r-- 1 sri sri 0 Jan  3 06:53 sales.report\nsri@envy:~/Desktop\n$ chgrp sales sales.report\nchgrp: changing group of 'sales.report': Operation not permitted\nsri@envy:~/Desktop\n$ sudo chgrp sales sales.report\nsri@envy:~/Desktop\n$ ls -l sales.report \n-rw-rw-r-- 1 sri sales 0 Jan  3 06:53 sales.report\nsri@envy:~/Desktop\n$ \n```\n- New group: `sales`.  \n\n---\n\n#### Setting Permissions for Group Access\n\n- Use `chmod` to grant appropriate permissions to the group.  \n  - Recommended permissions for non-executable files:  \n    - `664 (rw-rw-r--)`: User and group can read/write; others can read.  This is also the default option in Ubuntu 24.04.\n    - `660 (rw-rw----)`: User and group can read/write; no access for others.  \n\n**Example:**  \nSet group read/write permissions (660):  \n```bash\nsri@envy:~/Desktop\n$ chmod 660 sales.report \nsri@envy:~/Desktop\n$ ls -l sales.report \n-rw-rw---- 1 sri sales 0 Jan  3 06:53 sales.report\nsri@envy:~/Desktop\n$ \n\n```\n- Group members (`sales`) can now read and modify the file.\n\n**Deleting a group**\n```bash\nsri@envy:~/Desktop\n$ ls -l sales.report \n-rw-rw---- 1 sri sales 0 Jan  3 06:53 sales.report\nsri@envy:~/Desktop\n$ sudo groupdel sales\nsri@envy:~/Desktop\n$ ls -l sales.report \n-rw-rw---- 1 sri 1001 0 Jan  3 06:53 sales.report\n                 ^^^^\nsri@envy:~/Desktop\n$\n```\n\n---\n\n#### Sharing Files in a Common Directory\n\n- **Shared Location**: It's more convenient to use a common directory for group files, such as `/usr/local/sales`.  \n  - The directory group owner should be set to the relevant group (e.g., `sales`).  \n  - Recommended directory permissions:  \n    - `775 (rwxrwxr-x)`: User and group have full access; others can read/execute.  \n    - `770 (rwxrwx---)`: Only user and group have full access.  \n\n**Example:**  \nCreate a shared directory:  \n```bash\n$ ls -ld /usr/local/sales\ndrwxrwxr-x 2 root sales 4096 Jun 15 20:53 /usr/local/sales\n```\n- Group owner: `sales`.  \n- Permissions: `775`.\n\nMove the file to the shared directory:  \n```bash\n$ mv sales.report /usr/local/sales/\n$ ls -l /usr/local/sales\ntotal 4\n-rw-rw-r-- 1 sri sales 6 Jun 15 20:41 sales.report\n```\n- `sales.report` is now accessible to all group members.\n\n---\n\n### Key Points\n- Use `chgrp` to assign a file to a group.  \n- Adjust file permissions (`chmod`) to control group access.  \n- Shared directories simplify collaboration and should have appropriate group ownership and permissions.\n\n## Directory Permissions\n\nDirectory permissions are critical in controlling access to files and subdirectories within the directory. While file permissions dictate how a file can be read, written, or executed, directory permissions determine how the files and subdirectories inside it are accessed, listed, or modified.\n\n### Understanding Directory Permissions\n\nThe permissions for directories are interpreted differently compared to regular files. Here's what each permission means for a directory:\n\n| **Permission** | **Effect on Directory** |\n|----------------|-------------------------|\n| `r` (read) | Allows viewing the names of the files and subdirectories inside the directory (e.g., `ls` command).  |\n| `w` (write)| Allows creating, deleting, or renaming files and subdirectories within the directory.|\n| `x` (execute)  | Allows entering the directory (e.g., `cd` command) and accessing files or subdirectories if permissions allow. |\n\nDirectory permission usually only contain **0s**, **5s**, and **7s**. Common directory permissions include **755**, **700**, **770**, and **750**.\n\n---\n\n### Permission Combinations for Directories","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnaveensrinivas%2Flinux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frnaveensrinivas%2Flinux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnaveensrinivas%2Flinux/lists"}