Navigating the Filesystem
pwd Command
pwd
Displays the absolute path of the current working directory.
cd Command
cd [target_directory]
Changes the current shell session's working directory.
Usage:
cd ..: Move up to the parent directory.cd /home/user/docs: Navigate using an absolute path (starts from root/).cd ../projects: Navigate using a relative path (based on current location).cd ~: Switch to the user's home directory.cd -: Return to the previous working directory.
Linux organizes files in an inverted tree structure. The root is denoted by /. Paths act as unique addresses; absolute paths define the full route from the root, while relative paths define the route from your current location.
Listing and Inspecting Files
ls Command
ls [options] [directory]
Lists directory contents. Files starting with . are hidden.
Key Options:
-a: Show all files, including hidden ones.-l: Display detailed metadata (permissions, owner, size, date).-d: Show directory information rather than its contents.-t: Sort output by modification time.-R: Recursively list subdirectories.
Examples:
ls -la
ls -lt /var/log
tree Concept
Every directory (except root) has a parent, and directories can contain multiple children (files or other directories). This structure ensures unique pathing.
File and Directory Manipulation
touch Command
touch [options] filename
Updates the access and modification times of a file. If the file does not exist, it creates an empty file.
mkdir Command
mkdir [options] dirname
Creates new directories.
Key Options:
-p: Create parent directories as needed. Useful for creating nested paths likemkdir -p project/src/utils.
rm and rmdir Commands
rm [options] target
rmdir [options] dirname
rmdir: Removes empty directories only.rm: Removes files or directories.
Key Options:
-r: Recursive removal (required for directories).-f: Force removal without prompting (bypasses write-protection).-i: Interactive mode (prompts before every removal).
Warning: rm -rf / is extremely dangerous and can destroy the operating system.
cp Command
cp [options] source destination
Copies files or directories.
Key Options:
-r: Copy directories recursivley.-i: Prompt before overwriting existing files.-f: Force copy without prompts.
mv Command
mv [options] source destination
Moves files (cut/paste) or renames them.
Key Options:
-i: Prompt before overwriting.-f: Do not prompt before overwriting.
Examples:
mv oldname.txt newname.txt # Rename
mv report.pdf /tmp/ # Move
Viewing Content
cat and tac
cat [options] file
cat: Prints file content sequentially to standard output.tac: Prints file content in reverse order.
Key Options:
-n: Number all output lines.-b: Number non-blank lines only.-s: Squeeze multiple blank lines into one.
Output Redirection
The shell handles input (stdin) and output (stdout) as streams.
>: Output Redirection. Writes output to a file, overwriting existing content.echo "Hello" > file.txt
>>: Append Redirection. Adds output to the end of a file.echo "World" >> file.txt
<: Input Redirection. Reads input from a file instead of the keyboard.cat < file.txt
> empty.txt: Creating an empty file or clearing an existing one.
less and more
less filename
View file content page by page. less is preferred over more because it allows backward navigation.
Controls:
PageUp/PageDown: Navigate pages./text: Search forward for "text".?text: Search backward for "text".n/N: Next/Previous search match.q: Quit.
head and tail
head -n 5 filename
tail -n 10 filename
head: Outputs the first 10 lines (default) of a file.tail: Outputs the last 10 lines (default) of a file.tail -f logfile: Follows the file output in real-time (useful for logs).
Searching and Filtering
find Command
find [path] [options] [expression]
Searches for files in a directory hierarchy.
Example:
find /home -name "*.conf"
Locates all files ending in .conf starting from /home.
grep Command
grep [options] "pattern" file
Filters text by searching for specific patterns (regex supported).
Key Options:
-i: Ignore case distinctions.-n: Print line numbers with output.-v: Invert match (show lines that do NOT match).
Example:
ps aux | grep nginx
Archives and Compression
zip / unzip
zip -r archive.zip folder/
unzip archive.zip -d /target/path
Compresses or extracts .zip files. The -r flag is required to include subdirectories.
tar Command
tar [options] archive_file [files]
Handles tape archives (.tar), often combined with gzip compression.
Templates:
- Compress:
tar czvf backup.tar.gz /path/to/backupc: Createz: gzip filterv: Verbose (show progress)f: Filename
- Extract:
tar xzvf backup.tar.gz -C /destinationx: ExtractC: Change to directory
System Information and Utilities
date Command
date +"%Y-%m-%d %H:%M:%S"
Displays or sets the system time.
- Timestamp:
date +%s(seconds since Jan 1, 1970). - Convert Timestamp:
date -d @<timestamp>
cal Command
cal [options]
Displays a calendar.
-3: Show previous, current, and next month.-y: Show the whole year.
uname Command
uname -a
Prints system information (Kernel name, hostname, version, architecture).
bc Command
echo "scale=2; 10/3" | bc
A command-line calculator for precision arithmetic.
Documentation and Help
man Command
man [section] command
Accesses the system's manual pages.
- Sections: 1 (User commands), 2 (System calls), 3 (Library functions).
- Search:
man -k keyword
nano Editor
A simple terminal-based text editor.
Ctrl + X: Exit (prompts to save if modified).Ctrl + O: Save (Write Out).
Essential Keyboard Shortcuts
Tab: Auto-completes commands or file paths. Press twice for suggestions.Ctrl + C: Interrupt/Send SIGINT to stop the current running process.Ctrl + D: Signal EOF (End of File) or logout from the shell.Ctrl + R: Search through command history.
Shutdown
shutdown [options] time
shutdown -h now: Halt the system immeidately.shutdown -r +5: Reboot the system in 5 minutes.
Extended Command Reference
Login/Logout: login, exit, last, shutdown, reboot
File Ops: file, ln, dd, diff, chmod, chown
System: top, df, free, ps, kill, crontab
Network: ifconfig, ip, ping, netstat, ssh, scp