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 manprovides a full list of these sections.Section Categories:
Section : Executable programs or shell commands.
Section : System calls.
Section : C library calls.
Section : Special Files.
Section : File formats and conventions.
Section : Miscellaneous.
Section : System administration commands and daemons.
To access a specific section, use the syntax
man n <command>, where ranges from to .
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
$--helpoption 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 thanfindbut 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 tomorewith enhanced navigation and features.head: Displays the first lines of a file.tail: Displays the last 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 .cal 9 1752: Shows September .
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:
User (): The owner/creator of the file.
Group (): A specific set of users assigned to the file.
Others (): All other users (often called the "world").
Permission Types:
Read (): Capability to view file contents.
Write (): Capability to modify file contents or directory structures.
Execute (): Capability to run a file as a program or view the contents of a directory.
Permission List Format: A sequence of characters seen in
ls -loutput.First Character (File Type):
d: Directory.-: Ordinary file.l: Symbolic link.s: Socket.mkmktnanp: 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 () =
Write () =
Execute () =
Calculation Example: For permission code :
Owner ():
Group ():
Others ():
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 pressEnter.
nano:
An open-source version of the
picoeditor (originally part of the Pine mail utility).Save/Exit Sequence:
Ctrl + x, theny, then pressEnter.
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 untilCTRL-Dis 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 "", 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
Create two files named
t1.txtandt2.txt.Create two directories named
d1andd2.Check all subdirectories and files in the working directory using the appropriate command.
Copy
t1.txtto a new file within directoryd1.Move
t2.txtinto directoryd2.Delete all generated
.txtfiles./Delete all generated directories.
Practice II: Redirection and Editors
Capture input from the keyboard and save it directly into a file.
Open a second file using either
nanoorviand add unique content.Display the text of both files on the screen using a viewing command.