2.6. Lab: Filesystem Practice#
Apply concepts from Chapter 2 through hands-on exercises. These labs build progressive skills from basic navigation to advanced file management.
2.6.2. Part 2: File Permissions and Attributes#
2.6.2.1. Exercise 2.1: Understanding Permissions#
# Create test files
$ mkdir -p ~/lab/perms
$ cd ~/lab/perms
# Create files with different content
$ echo "script" > script.sh
$ echo "data" > data.txt
$ mkdir restricted
# Check permissions
$ ls -l
total 8
-rw-r--r-- 1 user group 7 Jan 10 data.txt
drwxr-xr-x 2 user group 64 Jan 10 restricted
-rw-r--r-- 1 user group 7 Jan 10 script.sh
# Make script executable
$ chmod +x script.sh
$ ls -l script.sh
-rwxr-xr-x 1 user group 7 Jan 10 script.sh
# Create private directory
$ chmod 700 restricted
$ ls -ld restricted
drwx------ 2 user group 64 Jan 10 restricted
# Restrict data file
$ chmod 600 data.txt
$ ls -l data.txt
-rw------- 1 user group 7 Jan 10 data.txt
# Create world-readable file
$ chmod 644 data.txt
2.6.2.2. Exercise 2.2: Hard Links vs Symlinks#
# Create original file
$ echo "original content" > original.txt
# Create hard link
$ ln original.txt hardlink.txt
# Create symlink
$ ln -s original.txt softlink.txt
# Compare inode numbers
$ ls -i
1234567 hardlink.txt
1234567 original.txt # Same inode!
1234568 softlink.txt # Different inode
# Verify symlink target
$ readlink softlink.txt
original.txt
# Delete original
$ rm original.txt
# Hardlink still works!
$ cat hardlink.txt
original content
# But symlink is broken
$ cat softlink.txt
cat: softlink.txt: No such file or directory
2.6.2.3. Exercise 2.3: File Metadata#
# Use stat to see all metadata
$ touch test.txt
$ stat test.txt
# Find recently modified files
$ find ~ -type f -mtime -1 # Modified in last day
# Find large files
$ find ~ -type f -size +10M
# Find empty files
$ find ~ -type f -empty
# Find by ownership
$ find ~ -type f -user $USER
2.6.3. Part 3: Configuration and Project Organization#
2.6.3.1. Exercise 3.1: Exploring Your Shell Configuration#
# View your bash startup files
$ cat ~/.bashrc | head -20
# Check which files were loaded
$ bash -x -i -c exit 2>&1 | grep -E '(bashrc|bash_profile)'
# Create a custom alias
$ echo "alias work='cd ~/work && ls -la'" >> ~/.bashrc
# Source the file to apply immediately
$ source ~/.bashrc
# Test the alias
$ work
2.6.3.2. Exercise 3.2: Organizing Dotfiles#
# List all your dotfiles
$ ls -la ~ | grep '^\.'
# Create a dotfiles repository (practice)
$ mkdir ~/dotfiles-test
$ cd ~/dotfiles-test
$ git init
# Copy your important config files
$ cp ~/.bashrc .
$ cp ~/.gitconfig .
# Commit them
$ git add -A
$ git commit -m "Initial dotfiles"
# You could then create symlinks back to home:
# cd ~
# ln -s ~/dotfiles/.bashrc ~/.bashrc
2.6.3.3. Exercise 3.3: Creating a Project Structure#
Create a realistic web application structure:
$ mkdir -p ~/lab/webapp/{src,tests,public/{css,js,img},data,docs,scripts}
# Create README
$ cat > ~/lab/webapp/README.md << 'EOF'
# My Web Application
A simple web application built with Bash and curl.
## Project Structure
- src/: Source code
- tests/: Test files
- public/: Static assets
- data/: Data files
- docs/: Documentation
## Usage
See docs/SETUP.md
EOF
# Create .gitignore
$ cat > ~/lab/webapp/.gitignore << 'EOF'
*.log
node_modules/
.env
__pycache__/
.DS_Store
EOF
# Create essential scripts
$ mkdir -p ~/lab/webapp/scripts
$ cat > ~/lab/webapp/scripts/setup.sh << 'EOF'
#!/bin/bash
echo "Setting up project..."
mkdir -p data
mkdir -p logs
echo "Setup complete!"
EOF
$ chmod +x ~/lab/webapp/scripts/setup.sh
# Initialize git
$ cd ~/lab/webapp
$ git init
$ git add -A
$ git commit -m "Initial project structure"
2.6.4. Part 4: Advanced Challenges#
2.6.4.1. Challenge 4.1: Bulk File Operations#
# Create test files
$ mkdir ~/lab/bulk
$ cd ~/lab/bulk
$ for i in {1..10}; do
touch document_$i.txt
done
# Rename all .txt to .md
$ for file in *.txt; do
mv "$file" "${file%.txt}.md"
done
# Verify
$ ls -la
# Create subdirectories by first letter
$ for file in *.md; do
letter=${file:0:1}
mkdir -p "$letter"
mv "$file" "$letter/"
done
# List the result
$ find . -type f | sort
2.6.4.2. Challenge 4.2: Symlink Management#
# Create version management scenario
$ mkdir ~/lab/versions
$ cd ~/lab/versions
# Create "versions" of a file
$ echo "version 1" > app-v1.0.jar
$ echo "version 2" > app-v2.0.jar
$ echo "version 3" > app-v3.0.jar
# Create symlink to current version
$ ln -s app-v2.0.jar app.jar
# Verify
$ readlink app.jar
app-v2.0.jar
$ cat app.jar
version 2
# "Update" to version 3
$ rm app.jar
$ ln -s app-v3.0.jar app.jar
$ cat app.jar
version 3
2.6.4.3. Challenge 4.3: Finding and Processing Files#
# Create test directory structure
$ mkdir -p ~/lab/search/{docs,code,archive}
$ touch ~/lab/search/docs/readme.txt
$ touch ~/lab/search/code/script.py
$ touch ~/lab/search/code/main.py
$ touch ~/lab/search/archive/old.bak
# Find all Python files and show their size
$ find ~/lab/search -name "*.py" -exec ls -lh {} \;
# Find all files, exclude archive
$ find ~/lab/search -not -path "*/archive/*" -type f
# Count lines in Python files
$ find ~/lab/search -name "*.py" -exec wc -l {} \; | awk '{sum+=$1} END {print "Total lines:", sum}'
# Create backups of all .py files
$ find ~/lab/search -name "*.py" -exec cp {} {}.bak \;
2.6.5. Part 5: Reflection Questions#
Answer these in your own words:
Symlinks vs Hard Links
When would you use a hard link instead of a symlink?
Why can’t you create hard links to directories?
Permissions
What does permission 755 mean? What about 644?
Why would you set a directory to 700 but a file to 644?
Configuration Files
Why do you think shell config files start with a dot?
What happens if you delete ~/.bashrc?
Project Organization
How does your current project structure compare to the examples?
What improvements could you make?
File Finding
When would
findbe better thanls?How would you find all your Bash scripts in your home directory?
2.6.6. Cleanup#
When finished with labs, clean up test directories:
# Remove all lab directories
$ rm -rf ~/lab/
# Or keep just what you want
$ cp ~/lab/webapp ~/projects/my-webapp