Linux Overview

Linux Overview and Command Structure

  • Definition of a Linux Command: A Linux command is an executable program that interacts directly with the operating system, including the kernel and the shell, to perform functions requested by the user.

  • Command Types: Commands can take several forms:

    • Built-in shell commands.

    • Executable shell scripts.

    • Executable files generated from compiled source code.

  • Syntax Structure: The standard structure for a command is as follows: command [option1 … optionM] [argument1 … argumentN].

  • Execution Rules:

    • Multiple commands can be executed sequentially by separating them with a semicolon (;;).

    • To execute a command or file located in the current directory, the prefix ././ must be added if that directory is not in the system path.

  • Components of a Command:

    • Command Name: The specific identifier for the executable program.

    • Options: These are optional switches, usually preceded by a single dash (-) or double dashes (--), used to modify the command's mode of operation.

    • Arguments: Optional data provided to the command, such as a filename, which the command will use or manipulate.

Command Help and Documentation Systems

  • Manual Pager (man):

    • Used by entering man <command> to access detailed documentation.

    • Manual pages are organized into sections based on content. The command man man provides a full list of these sections.

    • Section Categories:

      • Section 11: Executable programs or shell commands.

      • Section 22: System calls.

      • Section 33: C library calls.

      • Section 44: Special Files.

      • Section 55: File formats and conventions.

      • Section 66: Miscellaneous.

      • Section 77: System administration commands and daemons.

    • To access a specific section, use the syntax man n <command>, where nn ranges from 11 to 99.

  • GNU Info System:

    • Accessed via the command info [command].

    • Navigation within Info pages:

      • Arrow Keys: Used to move through the text.

      • ENTER: Used to follow a link at the cursor's location.

      • n: Move to the next logical node.

      • p: Move to the previous logical node.

      • t: Return to the top (root) node.

  • Command-Line Option Help: Use the $--help option after a command to see brief usage information.

Basic Command Categories

File and Directory-Related Commands
  • ls: Lists directory contents.

  • cd: Changes the working directory. Example: cd directory_name.

  • cd ..: Navigates to the parent directory.

  • pwd: Prints the name of the current working directory.

  • mkdir: Creates a new, empty directory. Example: mkdir new_directory.

  • cp: Copies a file or directory. Example: cp t.txt ./directory/file.txt.

  • mv: Moves or renames a file/directory. Example: mv t.txt ./directory/file.

  • rm: Removes a file or directory. Example: rm file.txt.

  • rmdir: Removes an empty directory.

  • file: Determines the specific file type. Example: file t.txt.

  • touch: Changes a file's timestamp or creates a new empty file if it does not yet exist.

  • wc: Prints the count of lines, words, and characters in a file.

  • sort: Sorts lines within a file numerically or alphabetically.

Process Related Commands
  • ps: Displays a snapshot of the current active processes.

  • top: Provides a dynamic, real-time view of running processes.

  • netstat: Prints network-related statistics.

  • pstree: Displays running processes in a tree-like hierarchical structure.

  • kill: Sends a signal to terminate a specific process.

User Related Commands
  • who: Displays a list of users currently logged into the system.

  • whoami: Prints the effective userid (identifies the current user session).

  • groups: Prints the names of the groups the current user belongs to.

Search and Filtering Commands

  • which: Locates the specific executable path associated with a command.

  • whereis: Locates the binary, source code, and manual page files for a command.

  • find: Searches for files within a directory hierarchy based on criteria.

    • Example: find . -name "*.txt" -print (finds and prints all .txt files in the current directory).

    • Example: find ~/ -name "*.o" -exec chmod +x { } \; (finds all .o files in the home directory and changes their permissions to executable).

  • locate: Searches for files by name using a pre-built database (faster than find but may be less current).

  • grep: Searches for lines matching a pattern using regular expressions.

    • Example: grep -i "param" test.c (case-insensitive search for "param").

    • Example using a pipe: more test.c | grep "param".

File Viewing and Terminal Utility Commands

  • Viewing Commands:

    • cat: Concatenates files and prints them to standard output. Example: cat file1 file2.

    • echo: Prints a provided line of text to the terminal.

    • type: Provides a brief description of what a command is (e.g., alias, built-in, or file).

    • more: A file viewer that allows paging through text one screen at a time.

    • less: An advanced file viewer similar to more with enhanced navigation and features.

    • head: Displays the first nn lines of a file.

    • tail: Displays the last nn lines of a file.

    • diff: Compares two files line-by-line to identify differences.

  • Time and Calendar Commands:

    • date: Prints current date and time in various formats.

    • cal: Prints a calendar.

      • cal 1 2019: Shows January 20192019.

      • cal 9 1752: Shows September 17521752.

  • Disk Related Commands:

    • df: Reports the amount of disk space used on file systems.

    • du: Estimates file space usage for a directory and its subdirectories.

  • Miscellaneous:

    • clear: Clears the terminal screen of all text.

    • sleep: Pauses shell activity for a specified duration.

    • history: Lists the history of recently executed commands.

File Management and Standard Files

  • Definition of a File: A collection of related information defined by its creator (e.g., source code, binaries, data).

  • Operating System Responsibilities:

    • Creation and deletion of files and directories.

    • Providing primitives for manipulation.

    • Ensuring file security.

    • Mapping files onto secondary storage.

    • Abstracting differences between storage types.

  • Linux File Philosophy: Linux treats almost everything (including hardware devices) as a file.

  • Standard Files: Every shell-associated command has three default standard files for interaction:

    • stdin (Standard Input): The default source for input data, usually the keyboard.

    • stdout (Standard Output): The default destination for command output, usually the terminal.

    • stderr (Standard Error): The default destination for error messages, usually the terminal.

  • File Descriptor: This is a unique, non-negative integer assigned by the OS to identify open files on a per-process basis.

Linux File and Directory Permissions

  • Permission Groups: Access is governed by three categories:

    1. User (uu): The owner/creator of the file.

    2. Group (gg): A specific set of users assigned to the file.

    3. Others (oo): All other users (often called the "world").

  • Permission Types:

    1. Read (rr): Capability to view file contents.

    2. Write (ww): Capability to modify file contents or directory structures.

    3. Execute (xx): Capability to run a file as a program or view the contents of a directory.

  • Permission List Format: A sequence of 1010 characters seen in ls -l output.

    • First Character (File Type):

      • d: Directory.

      • -: Ordinary file.

      • l: Symbolic link.

      • s: Socket.mkmktnan

      • p: Named pipe.

      • c: Character device (e.g., terminal).

      • b: Block device (e.g., hard disk).

    • Hard Links: Every file must have at least one hard link (a directory entry associating a name with the file).

Modifying and Interpreting Permissions

Absolute (Numeric) Mode
  • Permissions are represented by a three-digit octal value.

  • Values Assignment:

    • Read (rr) = 44

    • Write (ww) = 22

    • Execute (xx) = 11

  • Calculation Example: For permission code 744744:

    • Owner (77): 4+2+1=rwx4 + 2 + 1 = rwx

    • Group (44): 4+0+0=r4 + 0 + 0 = r--

    • Others (44): 4+0+0=r4 + 0 + 0 = r--

Symbolic Mode
  • Uses letters to represent users and permissions: u (user), g (group), o (others), a (all).

  • Example Syntax: chmod u+rwx file_name.

Ownership Commands
  • chmod: (Change Mode) Changes permissions using numeric or symbolic input.

  • chown: Changes the user ownership of a file.

  • chgrp: Changes the group ownership of a file.

Linux Text Editors

  • vim (Vi IMproved):

    • The improved version of the original BSD Unix text editor, vi.

    • Modes: Insert mode (for typing) and Command mode (for managing the file).

    • Save/Exit Sequence: Escape, then type :wq, then press Enter.

  • nano:

    • An open-source version of the pico editor (originally part of the Pine mail utility).

    • Save/Exit Sequence: Ctrl + x, then y, then press Enter.

Input and Output Redirection

  • Input Redirection (<):

    • Detaches keyboard from stdin and attaches a file instead.

    • Syntax: command < input_file.

    • Example: cat < sample (reads input from file 'sample').

  • Output Redirection (> and >>):

    • Detaches terminal from stdout and attaches a file.

    • Overwrite (>): cat > sample (creates 'sample' and fills it with keyboard input until CTRL-D is pressed. Overwrites existing data).

    • Append (>>): Adds output to the end of an existing file without overwriting.

    • Example: grep "abc" student > abc_file.

  • Combined Redirection: Commands can redirect both simultaneously: command < input_file > output_file.

Pipes and Pipelines

  • Pipe (|): A method of Interprocess Communication (IPC) where the output of one command serves as the input for another.

  • Pipeline: A chain of processes where standard streams are linked together.

  • Complex Example: who | sort | grep "2024" > myfile.

    • This pipeline takes the list of logged-in users, sorts them, filters for the string "20242024", and writes the final result to 'myfile'.

  • Common Use Case: ls -l | less (allows scrolling through a long list of directory permissions).

Practice Exercises

Practice I: Basic File and Directory Tasks
  1. Create two files named t1.txt and t2.txt.

  2. Create two directories named d1 and d2.

  3. Check all subdirectories and files in the working directory using the appropriate command.

  4. Copy t1.txt to a new file within directory d1.

  5. Move t2.txt into directory d2.

  6. Delete all generated .txt files./

  7. Delete all generated directories.

Practice II: Redirection and Editors
  1. Capture input from the keyboard and save it directly into a file.

  2. Open a second file using either nano or vi and add unique content.

  3. Display the text of both files on the screen using a viewing command.