Shell Basics
- Shell: Program that takes keyboard commands and passes them to the OS
- Terminal Emulator: Interface to interact with the shell in GUI environment
- Shell Prompt:
username@hostname:directory$(e.g.,student@ubuntu:~$)$= user-level account#= root/super-user account~= home directory (/home/username)
Essential Commands
Navigation & Information
| Command | Description | Example Output |
|---|---|---|
pwd | Print working directory - shows current location | /home/student |
cd <path> | Change directory to specified path | (no output) |
cd .. | Go to parent directory | (no output) |
cd ~ or cd | Go to home directory | (no output) |
cd - | Go to previous directory | (no output) |
ls | List files and directories in current directory | Desktop Documents Downloads |
exit | Exit from terminal session | (closes terminal) |
Listing Options (ls command)
| Command | Description | Use Case |
|---|---|---|
ls -l | Long format (detailed) | Shows permissions, owner, size, date |
ls -a | List all files including hidden (start with .) | Shows .bashrc .profile etc. |
ls -h | Human-readable file sizes | Shows 1.5K, 2.3M instead of bytes |
ls -t | Sort by modification time | Newest files first |
ls -S | Sort by file size | Largest files first |
ls -r | Reverse order | Descending alphabetical |
ls -F | Classify (append indicator to names) | / for directories, * for executables |
ls -d | List directory itself, not contents | Used with -l to see dir details |
Example ls -l output:
-rwxr-xr-x 1 root root 1563 May 6 2015 espdiff
- Field 1 (
-rwxr-xr-x): Permissions - Field 2 (
1): Number of links - Field 3 (
root): Owner name - Field 4 (
root): Group name - Field 5 (
1563): Size in bytes - Field 6-8 (
May 6 2015): Last modified date - Field 9 (
espdiff): File/directory name
Pathnames
Absolute Pathname
- Starts with
/(root directory) - Works from any location
- Example:
cd /usr/games - Analogy: Complete address from country level
Relative Pathname
- Starts from current directory
- Uses
.(current) and..(parent) - Example:
cd ./gamesorcd games - Analogy: Directions from current location
Path Comparison
| Command | Type | Meaning |
|---|---|---|
cd /home | Absolute | Go to /home from root (works anywhere) |
cd ./home | Relative | Go to home in current directory |
cd home | Relative | Same as cd ./home (shorthand) |
Special Symbols
| Symbol | Meaning | Example |
|---|---|---|
/ | Root directory OR path separator | /home/student |
~ | Home directory | cd ~ → /home/username |
. | Current directory | ./file |
.. | Parent directory | cd .. goes up one level |
- | Previous directory | cd - goes back |
File Viewing
echo Command
echo "Hello World" # Print text to terminal
echo "text" > file # Write text to file (overwrite)
echo "text" >> file # Append text to fileOutput: Prints text directly to terminal
less Command
less filename # View file contents (paginated)Controls in less:
Spaceorf- Next pageb- Previous pageq- Quit/pattern- Search forward?pattern- Search backward
File Permissions
Permission Format: rwxrwxrwx
-rwxr-xr--
│└┬┘└┬┘└┬┘
│ │ │ └─ Others permissions (r--)
│ │ └──── Group permissions (r-x)
│ └─────── Owner permissions (rwx)
└───────── File type (- = file, d = directory)
Permission Types
r= read (4)w= write (2)x= execute (1)-= no permission (0)
Numeric Permissions
| Number | Permission | Meaning |
|---|---|---|
| 0 | --- | No permissions |
| 1 | --x | Execute only |
| 2 | -w- | Write only |
| 3 | -wx | Write + Execute |
| 4 | r-- | Read only |
| 5 | r-x | Read + Execute |
| 6 | rw- | Read + Write |
| 7 | rwx | All permissions |
Common Permission Examples
| Code | Permissions | Use Case |
|---|---|---|
755 | rwxr-xr-x | Standard executable/directory |
644 | rw-r--r-- | Standard file |
600 | rw------- | Private file (owner only) |
777 | rwxrwxrwx | All permissions (dangerous!) |
Changing Permissions
chmod 755 script.sh # Set rwxr-xr-x
chmod 600 private.txt # Set rw------- (owner only)
chmod u+x file # Add execute for owner
chmod g-w file # Remove write for group
chmod o-r file # Remove read for othersFormat: chmod [ugoa][+-=][rwx] file
u= user/owner,g= group,o= others,a= all
Getting Help & History
Documentation
help cd # Get help for cd command
man ls # Manual page for ls command
man -k keyword # Search man pages by keywordNotes:
helpworks for shell built-insmanworks for external commands- In
man, pressqto quit
Command History
history # Show all previous commands
!n # Execute command number n from history
!! # Execute last command
!string # Execute most recent command starting with "string"Keyboard shortcuts:
↑(Up arrow) - Previous command↓(Down arrow) - Next commandCtrl+R- Search command history
Command Structure
General Format
command -options arguments
Example: ls -lh /home
ls= command-lh= options (long format + human-readable)/home= argument (what to list)
Common Patterns
command # Execute alone
command argument # Command with target
command -option # Command with modifier
command -option argument # Command with both
command -opt1 -opt2 arg1 arg2 # Multiple options and argumentsFilesystem Structure
/ (root directory)
├── bin/ Binary executables
├── dev/ Device files
├── etc/ Configuration files
├── home/ User home directories
│ └── student/ Your home directory (~)
├── usr/ User programs
│ ├── bin/ User binaries
│ ├── lib/ Libraries
│ └── share/ Shared data
├── var/ Variable data
│ └── log/ Log files
└── tmp/ Temporary files
Key Quiz Topics
1. What does pwd stand for?
Answer: Print Working Directory (shows current location)
2. How to exit terminal?
Answer: exit command
3. Remove directory with contents?
Answer: rm -r DirName (recursive removal)
4. Print text to terminal?
Answer: echo command
5. Go to home directory from anywhere?
Answer: cd ~ or cd or cd - (if home was previous location)
6. Show detailed file info without changing directory?
Answer: ls -l /path (list with path, don't cd first)
7. What does # symbol indicate?
Answer: Root level account (super-user)
8. Absolute vs Relative pathname?
Answer:
- Absolute: Begins with
/(root directory), works from anywhere - Relative: Starts from current directory, uses
.or..
9. How to get command documentation?
Answer: man command (manual page)
10. File permission format?
Answer: Access rights for owner, group, and others (read, write, execute)
- First character: file type
- Next 9 characters: permissions in groups of 3 (rwx for owner, group, others)
Quick Tips
✓ Tab completion: Press Tab to auto-complete file/directory names
✓ Clear screen: clear or Ctrl+L
✓ Cancel command: Ctrl+C
✓ Case sensitive: File.txt ≠ file.txt
✓ Spaces in names: Use quotes "My File.txt" or escape My\ File.txt
✓ Hidden files: Start with . (e.g., .bashrc)
✓ No undo: Commands are immediate and permanent
✓ Root access: Use with caution - can damage system
Common Command Combinations
ls -lah # List all files (including hidden) in long format with human sizes
cd /path && ls # Change directory AND list contents
mkdir -p dir1/dir2/dir3 # Create nested directories
cp -r source/ dest/ # Copy directory and all contents
rm -rf directory/ # Force remove directory (dangerous!)
ls -lt | head -10 # Show 10 most recently modified files
history | grep command # Search command historyNetwork Commands (Bonus)
ifconfig # Show network interface configuration
ping <IP> # Test network connectivity
ping 192.168.1.1 # Example: ping routerPing output: Shows if host is reachable and response time
Remember!
- Always double-check before using
rm -r - Absolute paths start with
/, relative paths don't - Use
manorhelpwhen unsure about a command ls -lshows detailed information including permissions~always means your home directorycdwithout arguments goes to home directory- Root account (#) has unlimited power - use carefully!