Directory and File Inspection
ls
Syntax: ls [options] [path]
Displays the contents of a directory or details about a specific file.
-a: Reveal all entries, including hidden ones starting with a dot.-l: Output in a long listing format, showing permissions, ownership, and size.-d: Display information about the directory itself, not its contents.-R: Recursively list subdirectories encountered.-t: Sort items by modification time.-F: Append an indicator character to entries (e.g.,*for executables,/for directories).
Options can be combined, such as ls -al. Every directory implicitly contains the hidden entries . (current directory) and .. (parent directory). A file fundamentally consists of its content and its metadata (attributes).
pwd
Syntax: pwd
Outputs the absolute path of the current working directory. In Linux, the forward slash / serves as the path separator; the final component in a path is either a file or a directory.
tree
Syntax: tree [options] [directory]
Renders the directory structure in a visual tree format. Install it via sudo apt-get install tree or yum install -y tree.
-L n: Limit the display depth tonlevels.-d: Restrict output to directories only.
Navigation and System State
cd
Syntax: cd [directory]
Alters the current working directory. The file system is structured as an inverted tree, where every file or directory has exactly one parent directory.
cd ..: Move up one level to the parent directory.cd /absolute/path: Navigate using an absolute path from the root.cd relative/path: Navigate using a path relative to the current directory.cd ~: Switch to the current user's home directory.cd -: Return to the previous working directory.
The home directory is the default landing spot upon login. Regular users typically reside in /home/username, whereas the root user operates out of /root. The tilde ~ symbol acts as a shorthand for this location.
clear
Syntax: clear
Wipes the terminal screen clean.
whoami
Syntax: whoami
Prints the username of the effective current user.
uname
Syntax: uname [options]
Retrieves system and kernel information.
-a: Print all available system data, including kernel name, hostname, and version.-r: Output the specific kernel release version.
File Creation and Deletion
touch
Syntax: touch [options] filename
Primarily used to generate an empty file if it does not exist, or to modify the access and modification timestamps of an existing file.
-a: Alter only the access time.-m: Alter only the modification time.
mkdir
Syntax: mkdir [options] dirname
Constructs a new directory.
-p: Create necessary parent directories recursively. E.g.,mkdir -p src/components/app.
rmdir
Syntax: rmdir [options] dirname
Removes empty directories only.
-p: Delete parent directories if they become empty after the target removal.
rm
Syntax: rm [options] target
Erases files or directories permanently. Caution is advised as this action is typically irreversible.
-f: Force deletion, bypassing confirmation prompts for write-protected files.-i: Prompt for confirmation before each removal.-r: Recursively delete a directory and all its contents.-v: Verbosely output the name of each file being removed.
File Manipulation and Viewing
cp
Syntax: cp [options] source destination
Copies files or directories from one location to another.
-f: Force the copy, overwriting existing destination files without prompting.-i: Interactively prompt before overwriting.-r/-R: Recursively copy an entire directory tree.-n: Do not overwrite an existing file.
Example: cp config.yml config_backup.yml
mv
Syntax: mv [options] source destination
Moves or renames files and directories. If the destination is an existing directory, the source is moved into it. If the destination is a new name, the source is renamed.
-f: Overwrite without prompting.-i: Prompt before overwriting.
Example - safe delete alias setup:
mkdir -p ~/.local/Trash
alias del='mv -t ~/.local/Trash'
alias restore='mv ~/.local/Trash/$@ ./'cat & tac
Syntax: cat [options] file
Reads and outputs the entire content of a file sequentially. tac performs the same function but in reverse order.
-n: Number all output lines.-b: Number only non-empty lines.-s: Suppress repeated empty output lines.
more & less
Paging utilities for reading large files without loading the entire file into memory.
more allows forward navigation only. less is more advanced, permitting both forward and backward scrolling, as well as searching.
- In
less, use/patternto search forward and?patternto search backward. -N(in less): Display line numbers.
head & tail
Syntax: head/tail [options] file
head prints the beginning of a file (default 10 lines). tail prints the end of a file.
-n count: Specify the number of lines to output.-f(tail only): Follow the file in real-time, appending new data as it is written.
Example - extracting the 45th line from a dataset:
seq 10 10 1000 > numbers.log
head -45 numbers.log | tail -1Data Processing and Search
find
Syntax: find [path] [expression]
Traverses the file hierarchy to locate files matching specific criteria.
-name pattern: Locate files by their name.
grep
Syntax: grep [options] pattern file
Searches for text patterns within files and prints matching lines.
-i: Perform a case-insensitive search.-n: Prefix each line of matching output with its line number.-v: Invert the match, printing lines that do not contain the pattern.
wc
Counts lines, words, and bytes in a file. E.g., wc -l data.csv outputs the line count.
Redirection and Piping
- Output Redirection (
>): Writes command output to a file, overwriting existing content. E.g.,ls -l > listing.txt. - Append Redirection (
>>): Appends command output to the end of a file. E.g.,echo 'New Entry' >> log.txt. - Input Redirection (
<): Feeds file content as input to a command. - Pipe (
|): Connects the standard output of one command directly to the standard input of another. E.g.,dmesg | grep 'usb'.
Compression and Archiving
zip / unzip
Creates or extracts ZIP archives.
zip -r archive.zip folder/: Compress a directory recursively.unzip archive.zip -d target_dir/: Extract to a specific target directory.
tar
A robust utility for archiving files, often paired with compression algorithms.
-c: Create a new archive.-x: Extract files from an archive.-z: Filter the archive through gzip.-v: Verbosely list processed files.-f: Specify the archive filename (must follow immediately).-C: Directory to extract into.
Examples:
tar -czvf project_backup.tar.gz project_dir/
tar -xzvf project_backup.tar.gz -C /opt/restore/Archiving reduces disk usage, prevents file corruption during transfer, and facilitates network movement. For cross-system transfers, scp (Secure Copy) can be used: scp archive.tgz user@remote_ip:/destination/path. Tools like rz and sz enable Zmodem file transfers between local and remote terminals.
Time and System Utilities
date
Syntax: date [+FORMAT]
Displays or sets the system date and time.
date +%Y-%m-%d: Custom format output.date +%s: Print the Unix timestamp (seconds since Jan 1, 1970).date -d @timestamp: Convert a Unix timestamp back to a readable date.-s: Set the system time (requires root privileges).
cal
Displays a calendar in the terminal.
-3: Show the previous, current, and next month.-y: Display the entire year's calendar.
bc
An arbitrary-precision calculator language. Type quit to exit.
man
Syntax: man [section] command
Opens the system manual pages, the primary documentation source for commands and system calls.
- Section 1: User commands
- Section 2: System calls
- Section 3: Library functions
- Section 8: System administration commands
stat
Displays detailed status information about a file or file system, including the crucial ACM timestamps:
- Access: Last time the file content was read.
- Modify: Last time the file content was altered.
- Change: Last time the file metadata (attributes) was modified.
User Management
adduser / useradd
Commands for creating new user accounts. adduser is often an interactive script that sets up the home directory and password automatically, whereas useradd is a low-level utility requiring explicit flags (e.g., useradd -m -s /bin/bash newuser).
userdel
Removes a user account. Use userdel -r username to delete the user's home directory simultaneously.
System Control and Shortcuts
shutdown
Syntax: shutdown [options]
-h: Halt the system after shutdown.-r: Reboot the system.-t secs: Delay the shutdown by a specified number of seconds.
Keyboard Shortcuts
Tab: Auto-complete commands, file names, and directory paths.Ctrl + C: Terminate the currently running foreground process.Ctrl + D: Signal End of File (EOF) or log out of the current session.Ctrl + Insert/Shift + Insert: Copy and paste text in terminal emulators like XShell.
Command Architecture
Every shell command is fundamentally an executable program, typically located in directories like /usr/bin.
which
Scans the PATH environment variable to locate the absolute path of an executable file.
alias
Creates custom shorthand for longer commands. E.g., alias c='clear'. To persist these across sessions, append them to the ~/.bashrc configuration file.
nano
A straightforward, user-friendly terminal text editor. nano -w script.sh opens a file without automatic line wrapping.
Example workflow to write and execute a C program:
nano compute.c
// Write C code inside the editor
#include <stdio.h>
int main() {
int a = 15, b = 25;
printf("Sum: %d\n", a + b);
return 0;
}
// Save with Ctrl+X, then Y, then Enter
gcc compute.c -o compute
./computeCore Design Philosophy
Linux adheres strictly to the principle that "everything is a file." This implies a unified interface for interacting with hardware devices, pipes, sockets, and regular data files, utilizing standard read and write operations.