🐧 Week 2: Linux + Networking Basics

Day 2: File & Directory Operations

Duration: 5 Hours

📚 Learning Objectives

By the end of this session, you will be able to:

  • Create, copy, move, and delete files and directories
  • View and edit file contents
  • Understand file permissions (rwx)
  • Change permissions with chmod
  • Understand file ownership (chown)

📖 Core Concepts (2 Hours)

Creating Files & Directories

# Create empty file touch myfile.txt touch file1.txt file2.txt file3.txt # Create directory mkdir mydir mkdir -p parent/child/grandchild # Create nested dirs # Create file with content echo "Hello World" > hello.txt # Append to file echo "New line" >> hello.txt

Copying, Moving, Deleting

# Copy files cp source.txt destination.txt cp file.txt /path/to/destination/ cp -r folder/ newfolder/ # Copy directory recursively # Move/Rename mv oldname.txt newname.txt # Rename mv file.txt /path/to/destination/ # Move # Delete (CAREFUL!) rm file.txt # Delete file rm -r folder/ # Delete directory rm -rf folder/ # Force delete (dangerous!) rmdir emptydir/ # Remove empty directory only

Viewing File Contents

# View entire file cat file.txt # View with line numbers cat -n file.txt # View first/last lines head file.txt # First 10 lines head -n 20 file.txt # First 20 lines tail file.txt # Last 10 lines tail -n 20 file.txt # Last 20 lines tail -f /var/log/syslog # Follow file (live updates!) # Page through file less file.txt # Navigate with arrows, q to quit more file.txt # Older pager # Word count wc file.txt # lines, words, chars wc -l file.txt # Lines only

Text Editors

# Nano - Beginner friendly nano file.txt # Ctrl+O to save, Ctrl+X to exit # Vim - Powerful but learning curve vim file.txt # i = insert mode # Esc = normal mode # :w = save # :q = quit # :wq = save and quit # :q! = quit without saving # VS Code (if GUI available) code file.txt

File Permissions

# View permissions ls -l file.txt # -rw-r--r-- 1 user group 1234 Jan 15 10:00 file.txt # Permission breakdown: # -rw-r--r-- # │└┬┘└┬┘└┬┘ # │ │ │ └── Others: read only # │ │ └───── Group: read only # │ └──────── Owner: read, write # └────────── Type: - file, d directory, l link # r = read (4) # w = write (2) # x = execute (1) # Change permissions chmod 755 script.sh # rwxr-xr-x chmod 644 file.txt # rw-r--r-- chmod u+x script.sh # Add execute for user chmod g+w file.txt # Add write for group chmod o-r file.txt # Remove read for others chmod a+r file.txt # Add read for all # Common permission patterns # 755 - Executable scripts # 644 - Regular files # 700 - Private files/directories # 600 - Secure files (SSH keys)

File Ownership

# View ownership ls -l file.txt # -rw-r--r-- 1 owner group ... # Change owner sudo chown newowner file.txt sudo chown newowner:newgroup file.txt sudo chown -R owner:group directory/ # Recursive

🔬 Hands-on Lab (2.5 Hours)

Lab 1: File Operations Practice

  • Create a project directory structure
  • Create, copy, move files between directories
  • Practice safe deletion
# Lab 1: Create project structure cd ~ mkdir -p devops-project/{src,config,logs,scripts} # Create files touch devops-project/src/app.py touch devops-project/config/settings.ini touch devops-project/scripts/deploy.sh echo "# My DevOps Project" > devops-project/README.md # View structure ls -R devops-project/ # Copy/Move practice cp devops-project/README.md devops-project/README.backup mv devops-project/scripts/deploy.sh devops-project/scripts/deploy_v1.sh

Lab 2: Log File Analysis

  • Create sample log file
  • Use head, tail, cat to view
  • Practice tail -f for live monitoring
# Lab 2: Working with logs cd ~/devops-project/logs # Create sample log cat > app.log << 'EOF' 2024-01-15 10:00:01 INFO Application started 2024-01-15 10:00:02 INFO Loading configuration 2024-01-15 10:00:03 INFO Database connected 2024-01-15 10:01:00 WARNING High memory usage: 80% 2024-01-15 10:02:00 ERROR Connection timeout 2024-01-15 10:02:01 INFO Retrying connection 2024-01-15 10:02:05 INFO Connection restored 2024-01-15 10:03:00 INFO Processing request #12345 2024-01-15 10:04:00 INFO Request completed 2024-01-15 10:05:00 INFO Shutting down EOF # View file cat app.log head -3 app.log tail -3 app.log wc -l app.log

Lab 3: Permissions Management

  • Create a script file
  • Try to execute (will fail)
  • Add execute permission
  • Run the script
# Lab 3: Script permissions cd ~/devops-project/scripts # Create script cat > hello.sh << 'EOF' #!/bin/bash echo "Hello from the script!" echo "Current user: $(whoami)" echo "Current directory: $(pwd)" EOF # Check permissions ls -l hello.sh # -rw-r--r-- (no execute permission) # Try to run (will fail) ./hello.sh # Permission denied # Add execute permission chmod +x hello.sh # OR chmod 755 hello.sh # Check again ls -l hello.sh # -rwxr-xr-x # Now run it ./hello.sh

📝 Practice Exercises

  1. Create a directory structure for a web project with css, js, images folders
  2. What permission number gives read+write to owner, read to everyone else?
  3. Create a file and change its permissions to 600. Who can access it?
  4. Practice using nano to edit a configuration file

⚠️ Important Safety Tips

  • Never run: rm -rf / or rm -rf *
  • Always double-check: Before any rm command
  • Use -i flag: rm -i file asks for confirmation
  • Backup important files: Before making changes

✅ Day 2 Checklist

  • Can create files and directories
  • Can copy, move, rename files
  • Can safely delete files and directories
  • Can view file contents (cat, head, tail, less)
  • Understand file permissions (rwx)
  • Can change permissions with chmod
  • Understand file ownership