What You’ll Learn in This Tutorial
- Navigating and checking directories
- File and directory operations
- Displaying file contents
- Basic file searching
History of Linux and UNIX
Why Learn Linux
Linux runs on approximately 96% of the world’s servers and is used everywhere from smartphones (Android) to supercomputers and IoT devices. Command line skills are essential for engineers.
The Birth of UNIX
The roots of modern Linux commands trace back to 1969. UNIX was developed at Bell Labs by Ken Thompson and Dennis Ritchie.
“UNIX is an operating system made by programmers for programmers” — Brian Kernighan
The Birth of Linux
In 1991, Finnish university student Linus Torvalds developed a free OS kernel compatible with UNIX. His famous post:
“Hello everybody out there using minix. I’m doing a (free) operating system (just a hobby, won’t be big and professional like gnu) for 386(486) AT clones.” — Linus Torvalds, 1991
UNIX Philosophy
The UNIX design philosophy has greatly influenced modern software development:
- Combine small programs: Create programs that do one thing well and combine them
- Text streams: Use text for communication between programs
- Early prototyping: Build something that works first, then improve
“This is the Unix philosophy: Write programs that do one thing and do it well.” — Doug McIlroy
Terminal and Shell
What is a Terminal
A terminal (terminal emulator) is an application that provides a command-line interface.
- macOS: Terminal.app, iTerm2
- Windows: Windows Terminal, PowerShell
- Linux: GNOME Terminal, Konsole
What is a Shell
A shell is a program that interprets user commands and communicates with the OS.
| Shell | Description |
|---|---|
| bash | Most common. Default for Linux/macOS (older macOS) |
| zsh | Default since macOS Catalina. bash-compatible |
| fish | Feature-rich and user-friendly |
| sh | POSIX standard shell. Highly portable |
# Check your current shell
echo $SHELL
Basic Directory Operations
pwd - Check Current Location
Use pwd (Print Working Directory) to check your current directory.
$ pwd
/home/username
Fun fact: In UNIX, directories are also treated as “files”. A directory is a “special file that contains links to other files”.
ls - List Files
Use ls to list files in a directory.
# Basic listing
$ ls
# Detailed view (permissions, size, etc.)
$ ls -l
# Show hidden files too
$ ls -a
# Detailed + hidden files
$ ls -la
# Human-readable sizes
$ ls -lh
Reading ls -l Output
| Field | Description |
|---|---|
-rw-r--r-- | File type and permissions (owner/group/others) |
1 | Hard link count |
user | Owner |
group | Owner group |
4096 | File size |
Jan 1 12:00 | Modified date |
file.txt | Filename |
File types: - = regular file, d = directory, l = symbolic link
Official Documentation: GNU Coreutils - ls
cd - Change Directory
# Go to specified directory
$ cd /var/log
# Go to home directory
$ cd ~
$ cd
# Go up one directory
$ cd ..
# Go back to previous directory
$ cd -
Types of Paths
| Type | Description | Example |
|---|---|---|
| Absolute path | Complete path starting from root (/) | /home/user/docs |
| Relative path | Path relative to current location | ./docs or ../other |
Special directory notation:
.- Current directory..- Parent directory~- Home directory/- Root directory
Creating and Deleting Files/Directories
mkdir - Create Directory
# Create directory
$ mkdir myproject
# Create deep hierarchy at once (-p: parents)
$ mkdir -p myproject/src/components
# Set permissions when creating
$ mkdir -m 755 secure_dir
Best Practice: Always use the
-poption when creating multiple levels. It won’t error if the directory already exists.
touch - Create File
touch originally “updates a file’s timestamp”, but creates an empty file if it doesn’t exist.
# Create empty file
$ touch index.html
# Create multiple files at once
$ touch style.css script.js
# Set timestamp to specific date
$ touch -t 202501011200 file.txt
Official Documentation: GNU Coreutils - touch
rm - Delete File/Directory
# Delete file
$ rm file.txt
# Delete directory (including contents)
$ rm -r mydir
# Delete without confirmation (caution!)
$ rm -rf mydir
# Ask before deleting
$ rm -i file.txt
Warning:
rm -rfdeletes immediately without confirmation. There is no recycle bin. Be especially careful when using it near/or with variable expansion (likerm -rf $DIR/).
Best Practices for Safe Deletion
# 1. Check with ls first
ls /path/to/delete
# 2. Delete with -i option for confirmation
rm -ri /path/to/delete
# 3. Set up alias (add to ~/.bashrc)
alias rm='rm -i'
Copying and Moving Files
cp - Copy
# Copy file
$ cp original.txt copy.txt
# Copy directory (-r: recursive)
$ cp -r mydir mydir_backup
# Copy multiple files to directory
$ cp file1.txt file2.txt destination/
# Preserve timestamp and permissions
$ cp -p original.txt copy.txt
# Ask before overwriting
$ cp -i source.txt dest.txt
Official Documentation: GNU Coreutils - cp
mv - Move/Rename
mv is used for both moving and renaming files.
# Move file
$ mv file.txt /path/to/destination/
# Rename file
$ mv oldname.txt newname.txt
# Move directory
$ mv mydir /path/to/destination/
# Ask before overwriting
$ mv -i source.txt dest.txt
Displaying File Contents
| Command | Description | Usage |
|---|---|---|
cat | Display entire file | cat file.txt |
head | Display first N lines (default 10) | head -n 20 file.txt |
tail | Display last N lines | tail -f log.txt |
less | Display with pager (scrollable) | less file.txt |
more | Older pager | more file.txt |
cat Applications
# Display line numbers
$ cat -n file.txt
# Concatenate multiple files
$ cat file1.txt file2.txt > combined.txt
# Compress empty lines
$ cat -s file.txt
less Navigation
less lets you interactively browse files:
Space/f- Forward one pageb- Back one page/pattern- Search forward?pattern- Search backwardn- Next search resultN- Previous search resultg- Go to beginningG- Go to endq- Quit
Fun fact: The name
lesscomes from “less is more”. It’s an improved version of themorecommand.
Log Monitoring with tail -f
# Monitor log in real-time
$ tail -f /var/log/syslog
# Monitor multiple files simultaneously
$ tail -f file1.log file2.log
# Start monitoring from last 100 lines
$ tail -n 100 -f app.log
Permission Basics
Reading Permissions
| Position | Meaning | Example |
|---|---|---|
| 1 | File type | - (file), d (directory), l (link) |
| 2-4 | Owner permissions | rwx (all permissions) |
| 5-7 | Group permissions | r-x (read + execute) |
| 8-10 | Others permissions | r-- (read only) |
| Symbol | Number | Meaning |
|---|---|---|
| r | 4 | read |
| w | 2 | write |
| x | 1 | execute |
| - | 0 | no permission |
chmod - Change Permissions
# Specify with numbers (755 = rwxr-xr-x)
$ chmod 755 script.sh
# Specify with symbols
$ chmod u+x script.sh # Add execute to owner
$ chmod g-w file.txt # Remove write from group
$ chmod o=r file.txt # Set others to read only
$ chmod a+r file.txt # Add read to all
Official Documentation: GNU Coreutils - chmod
Commonly Used Permissions
| Permission | Use Case |
|---|---|
| 755 | Executable scripts |
| 644 | Regular files |
| 600 | Sensitive files (SSH keys, etc.) |
| 700 | Private directories |
Practice: Create a Project Structure
Let’s use the commands we learned to create a basic web project structure.
# Create project directory
mkdir -p mywebsite/{css,js,images}
# Create basic files
touch mywebsite/index.html
touch mywebsite/css/style.css
touch mywebsite/js/main.js
# Check structure
ls -R mywebsite
# Use tree for better visualization if available
tree mywebsite
Practical Project Setup
# Node.js project creation example
mkdir -p my-app/{src/{components,utils,styles},tests,docs}
touch my-app/{package.json,README.md,.gitignore}
touch my-app/src/index.js
# Check structure
tree my-app
# my-app
# ├── README.md
# ├── docs
# ├── package.json
# ├── src
# │ ├── components
# │ ├── index.js
# │ ├── styles
# │ └── utils
# └── tests
Common Mistakes and Troubleshooting
Filenames with Spaces
# Wrong: Filename split by spaces
$ rm my file.txt # Tries to delete "my" and "file.txt"
# Correct: Wrap in quotes
$ rm "my file.txt"
$ rm 'my file.txt'
# Or escape with backslash
$ rm my\ file.txt
Permission Errors
# For Permission denied errors
$ sudo command # Run with admin privileges
# Check file permissions
$ ls -l file.txt
# If missing execute permission
$ chmod +x script.sh
File Not Found
# Search for file
$ find /path -name "filename"
# Partial match search
$ find . -name "*.txt"
# locate command (uses database, fast)
$ locate filename
Useful Shortcuts and Tips
Terminal Shortcuts
| Shortcut | Description |
|---|---|
Tab | Auto-complete commands and paths |
Ctrl + C | Cancel running command |
Ctrl + L | Clear screen (same as clear) |
Ctrl + A | Go to beginning of line |
Ctrl + E | Go to end of line |
Ctrl + U | Delete left of cursor |
Ctrl + K | Delete right of cursor |
Ctrl + R | Search command history |
↑ / ↓ | Browse command history |
!! | Re-run last command |
Combining Commands
# Pipe (|): Pass output to next command
$ ls -la | grep ".txt"
# Redirect (>): Save output to file
$ ls > filelist.txt
# Append (>>): Append to file
$ echo "new line" >> file.txt
# Command chaining
$ mkdir newdir && cd newdir # Run next only if successful
$ command1 || command2 # Run next only if failed
$ command1 ; command2 # Run in sequence
Next Steps
Once you’ve mastered basic commands, learn shell scripting to automate your work.
- Shell scripting intro → Shell Script Introduction
- Learn regular expressions → Using grep, sed, awk
Reference Links
Official Documentation
- GNU Coreutils Manual - Official reference for basic commands
- The Linux Documentation Project - General Linux documentation
- Linux man pages - Online man pages
Recommended Books/Articles
- The Linux Command Line (English/Free) - Classic command line intro
- Explainshell - Enter a command and get explanations
Cheat Sheets/Tools
- Linux Command Cheat Sheet - Common commands list
- tldr pages - Simplified man pages. Use like
tldr ls - cheat.sh -
curl cheat.sh/lsto display cheat sheet