# File and Directory Exploration This guide covers essential command-line tools for exploring file systems, viewing directory structures, and searching for files and content—all without needing to change directories. ## Quick Reference | Tool | Purpose | Example | |------|---------|---------| | `ls` | List directory contents | `ls -la /path/to/dir` | | `tree` | Visual directory hierarchy | `tree -L 2 /path/to/dir` | | `find` | Locate files and folders | `find . -type d -name "Audio*"` | | `grep` | Search file contents | `grep -r "pattern" /path` | ## ls - List Directory Contents View what's in a directory without navigating to it. ### Basic Usage ```bash # List files in current directory ls # List files in specific directory ls /Users/username/Documents # Long format with details (permissions, size, date) ls -l /path/to/dir # Show hidden files (starting with .) ls -a /path/to/dir # Combine flags: long format + hidden files ls -la /path/to/dir ``` ### Useful ls Flags - **`-l`** - Long format (shows permissions, owner, size, date) - **`-a`** - Show all files including hidden (`.git`, `.DS_Store`, etc.) - **`-h`** - Human-readable file sizes (KB, MB, GB instead of bytes) - **`-t`** - Sort by modification time (newest first) - **`-S`** - Sort by file size (largest first) - **`-R`** - Recursive (shows subdirectories too) ### Practical Examples ```bash # Show only directories in a path ls -d /path/to/dir/*/ # List files sorted by modification date ls -lt /path/to/dir # Show sizes in readable format ls -lh /path/to/dir # See everything, sorted by size ls -lhS /path/to/dir ``` ## tree - Visual Directory Structure The `tree` command displays directories as a graphical hierarchy. This is perfect for understanding folder structure at a glance. ### Installation ```bash # macOS (using Homebrew) brew install tree # Linux (Ubuntu/Debian) sudo apt install tree ``` ### Quick Command Reference | What you want | Command | |---------------|---------| | Limit to 2 levels deep | `tree -L 2` | | Show only folders | `tree -d` | | Show only folders, 2 levels | `tree -d -L 2` | | Show file sizes (human-readable) | `tree -h -s` | | Ignore node_modules and .git | `tree -I 'node_modules\|.git'` | | Show only .md files | `tree -P '*.md'` | | Show directory sizes | `tree --du -h` | | Show with full paths | `tree -f` | | Show hidden files | `tree -a` | | Folders first, then files | `tree --dirsfirst` | ### Basic Usage ```bash # Show tree of current directory tree # Show tree of specific directory tree /path/to/dir # Limit depth to 2 levels (RECOMMENDED to avoid overwhelming output) tree -L 2 /path/to/dir # Show hidden files tree -a /path/to/dir ``` ### Controlling Depth - Essential for Large Directories By default, `tree` shows **all** subdirectories recursively, which can produce thousands of lines. Always use `-L` to limit depth: ```bash # Show only top-level structure (depth 1) tree -L 1 # Show 2 levels (most common for quick overview) tree -L 2 ~/Code/midimaze # Show 3 levels for deeper exploration tree -L 3 "Computer Tech/" # Unlimited depth (use cautiously!) tree ``` **Tip:** Start with `-L 1` or `-L 2`, then increase depth as needed to avoid terminal flooding. ### Common tree Flags **Depth and Filtering:** - **`-L <number>`** - Limit directory depth - Example: `tree -L 2` shows only 2 levels deep - Replace `<number>` with 1, 2, 3, etc. - **Most important flag** to avoid overwhelming output - **`-d`** - Directories only (hide all files) - Example: `tree -d` shows folder structure without files - **`-I '<pattern>'`** - Ignore files/folders matching pattern - Example: `tree -I 'node_modules'` skips node_modules folders - Multiple patterns: `tree -I 'node_modules|.git|dist'` (use `|` separator) - Always use quotes around the pattern - **`-P '<pattern>'`** - Show only files matching pattern - Example: `tree -P '*.md'` shows only markdown files - Example: `tree -P '*.js|*.ts'` shows JavaScript and TypeScript files **Display Options:** - **`-a`** - Show hidden files (files starting with `.`) - Example: `tree -a` reveals `.git`, `.env`, `.DS_Store`, etc. - **`-h`** - Human-readable file sizes (KB, MB, GB) - Example: `tree -h` shows "2.3M" instead of "2415919" - Must use with `-s` or `--du` - **`-s`** - Show file sizes in bytes - Example: `tree -s` displays size next to each file - **`--du`** - Show directory sizes (disk usage) - Example: `tree --du` shows total size of each directory - Combines well with `-h`: `tree --du -h` **File Information:** - **`-p`** - Show file permissions - Example: `tree -p` displays `drwxr-xr-x` style permissions - **`-u`** - Show file owner - Example: `tree -u` displays username who owns the file - **`-g`** - Show group ownership - Example: `tree -g` shows the group that owns the file - **`-D`** - Show last modification date - Example: `tree -D` displays date/time of last change **Output Control:** - **`-C`** - Colorize output (usually enabled by default) - Example: `tree -C` adds colors to differentiate file types - **`-n`** - No color (plain text) - Example: `tree -n` removes all color formatting - **`--dirsfirst`** - List directories before files - Example: `tree --dirsfirst` groups all folders at top of each level - **`-f`** - Show full path for each file - Example: `tree -f` displays `./Audio Science/Audio Basics/file.md` - **`-i`** - No indentation (removes tree lines) - Example: `tree -i` shows flat list with no graphical tree structure ### Practical Examples **Basic depth control:** ```bash # Show 1 level (immediate contents only) tree -L 1 # Show 2 levels (common for quick overview) tree -L 2 ~/Code/midimaze # Show 3 levels (deeper exploration) tree -L 3 "Audio Science/" ``` **Directories only:** ```bash # Show only folders, 3 levels deep tree -d -L 3 ~/Documents # Quick view of immediate subdirectories tree -d -L 1 # All directories with full paths tree -d -f ``` **Filtering and ignoring:** ```bash # Ignore node_modules and .git folders tree -I 'node_modules|.git' /path/to/project # Show only markdown files, 2 levels tree -P '*.md' -L 2 # Ignore multiple common patterns tree -I 'node_modules|.git|.next|dist|build|__pycache__' ~/Code/my-project # Show only Python files in current directory tree -P '*.py' ``` **With file information:** ```bash # Show tree with human-readable file sizes tree -h -s -L 2 ~/Projects # Show directory sizes, 2 levels tree --du -h -L 2 "Computer Tech/" # Show permissions and file sizes tree -p -h -s -L 2 # Show modification dates tree -D -L 2 ``` **Combined examples:** ```bash # Directories only, sorted directories first, 2 levels tree -d -L 2 --dirsfirst # Full file info: sizes, permissions, dates tree -p -h -s -D -L 2 # Clean view: directories first, human sizes, ignore bloat tree --dirsfirst -h -s -L 3 -I 'node_modules|.git' # Development project overview tree -L 2 -I 'node_modules|.git|dist|build' --dirsfirst ~/Code/my-app # Show hidden files with sizes tree -a -h -s -L 2 ``` **Specific use cases:** ```bash # Quick overview of Audio Science folder (2 levels) tree -L 2 "Audio Science/" # See what's immediately in a directory (fastest) tree -d -L 1 # Find all markdown files in project structure tree -P '*.md' -f # Check project size by directory tree --du -h -L 2 -d # Show full structure ignoring common files tree -I '*.pyc|__pycache__|.DS_Store|node_modules' -a ``` ### Example Output ``` Audio Science/ ├── Audio Basics/ │ ├── Sound Waves.md │ └── Frequency.md ├── Audio Effects/ │ ├── Delays/ │ └── Reverb/ └── Audio Equipment Theory/ ├── Microphones.md └── Preamps.md ``` ## find - Locate Files and Folders The `find` command searches for files and directories matching specific criteria. ### Finding Directories ```bash # Find all directories in current location find . -type d # Find directories with specific name find . -type d -name "Audio*" # Find directories matching pattern (case-insensitive) find . -type d -iname "*science*" # Find directories in specific location find ~/Documents -type d -name "Projects" # Find directories modified in last 7 days find . -type d -mtime -7 ``` ### Finding Files ```bash # Find all .md files find . -type f -name "*.md" # Find files by name (case-insensitive) find . -type f -iname "readme*" # Find files larger than 10MB find . -type f -size +10M # Find files modified in last 24 hours find . -type f -mtime -1 ``` ### Useful find Flags - **`-type d`** - Directories only - **`-type f`** - Files only - **`-name "pattern"`** - Match name (case-sensitive) - **`-iname "pattern"`** - Match name (case-insensitive) - **`-size +10M`** - Files larger than 10MB - **`-size -1M`** - Files smaller than 1MB - **`-mtime -7`** - Modified within last 7 days - **`-maxdepth n`** - Limit search depth to n levels ### Practical Examples ```bash # Find all folders named "attachments" anywhere find ~ -type d -name "_attachments" # List all markdown files in Audio Science (max 3 levels deep) find "Audio Science/" -maxdepth 3 -type f -name "*.md" # Find large video files (over 100MB) find ~/Videos -type f -size +100M # Find empty directories find . -type d -empty # Find files modified today find . -type f -mtime 0 ``` ## grep - Search File Contents `grep` searches inside files for text patterns. Unlike `find` (which searches filenames), `grep` searches file contents. ### Basic Usage ```bash # Search for text in a file grep "pattern" file.txt # Search recursively in all files in a directory grep -r "pattern" /path/to/dir # Case-insensitive search grep -i "pattern" file.txt # Recursive + case-insensitive grep -ri "dante" ~/Documents ``` ### Useful grep Flags - **`-r`** - Recursive (search subdirectories) - **`-i`** - Case-insensitive - **`-n`** - Show line numbers - **`-l`** - Show only filenames (not matching lines) - **`-c`** - Count matches per file - **`-v`** - Invert match (show lines that DON'T match) - **`-w`** - Match whole words only - **`--include="*.md"`** - Search only specific file types ### Practical Examples ```bash # Find all files containing "microphone" (case-insensitive) grep -ri "microphone" . # Find files with "Pro Tools" and show line numbers grep -rn "Pro Tools" "DAW Operation/" # List files containing "reverb" in markdown files only grep -rl --include="*.md" "reverb" . # Search for whole word "mix" (not "mixing" or "remix") grep -rw "mix" . # Count how many times "frequency" appears in each file grep -rc "frequency" "Audio Science/" # Find files NOT containing "deprecated" grep -rL "deprecated" . ``` ## Command Operators and Chaining Understanding how to chain commands together unlocks powerful workflows. Here are the key operators: ### Operators Explained | Operator | Name | Purpose | Example | |----------|--------------|------------------------------------------------|----------------------------------| | `\|` | Pipe | Send text output of one command to another | `ls \| grep .md` | | `>` | Redirect | Write output to file (overwrite) | `tree > structure.txt` | | `>>` | Append | Add output to end of file | `ls >> log.txt` | | `;` | Sequential | Run commands one after another (always) | `ls; pwd` | | `&&` | AND | Run next command only if first succeeds | `cd dir && ls` | | `\|\|` | OR | Run next command only if first fails | `cd dir \|\| echo "failed"` | | `$()` | Substitution | Use command output as input | `tree $(find . -name dir)` | ### Pipe (|) - Chain Commands Together The pipe sends the **text output** of one command as **input** to another. **Important:** Pipes only pass stdout (text), not environment changes like directory switches. ```bash # List directories, then filter for "Audio" ls -d */ | grep Audio # Find all markdown files, count them find . -type f -name "*.md" | wc -l # Find directories, sort alphabetically find . -type d | sort # List files, show only first 5 results ls -la | head -5 ``` **Common mistake with pipes and directory changes:** ```bash # ❌ DOESN'T WORK - pipe doesn't preserve directory changes z midimaze | tree . # The 'z' command changes directory in a subshell, # then tree runs in the original directory # ✅ CORRECT - use && to chain commands in same shell z midimaze && tree . # ✅ ALSO CORRECT - run separately z midimaze tree . ``` **Why this matters:** Use `&&` when you need the *effect* of a command (like `cd` or `z`), not its *output*. ### Redirect (>) - Save Output to File Redirects write command output to a file instead of the terminal. ```bash # Save directory tree to file tree -L 2 > directory_structure.txt # Save list of markdown files find . -name "*.md" > markdown_files.txt # Overwrite existing file with new ls output ls -la "Audio Science/" > audio_contents.txt ``` ### Append (>>) - Add to File Appends command output to the end of a file without overwriting. ```bash # Add to existing log file echo "Backup completed" >> backup.log # Append directory listing to file ls -la >> all_files.txt # Build a comprehensive log date >> session.log tree -L 1 >> session.log ``` ### Command Substitution $() - Use Output as Input Command substitution runs a command and uses its output as an argument to another command. ```bash # Find a directory, then show its tree tree $(find . -type d -name "Audio*" | head -1) # List contents of the most recently modified directory ls -la $(ls -td */ | head -1) # Show tree of directory containing specific file tree $(dirname $(find . -name "config.json")) ``` ## Combining Commands - Real Examples Here are practical examples combining grep, find, tree, and operators. ### Example 1: Find Directory, Then Show Its Tree **Goal:** Search for a directory containing "Audio" and display its structure. ```bash # Find the directory first find . -type d -name "*Audio*" # Output: ./Audio Science/Audio Effects # Now show its tree (2 levels deep) tree -L 2 "./Audio Science/Audio Effects" ``` **Combined with command substitution:** ```bash # Automatically find and show tree in one command tree -L 2 $(find . -type d -name "*Audio Effects*" | head -1) ``` This finds the first directory matching "Audio Effects" and shows its tree. ### Example 2: Grep for File, Then Tree Its Directory **Goal:** Find files containing specific text, then show the directory structure. ```bash # Find files containing "microphone" grep -rl "microphone" . # Output: ./Microphone Techniques/Microphone Basics/Condenser Mics.md # Get the directory name dirname "./Microphone Techniques/Microphone Basics/Condenser Mics.md" # Output: ./Microphone Techniques/Microphone Basics # Show tree of that directory tree "./Microphone Techniques/Microphone Basics" ``` **Combined version:** ```bash # Find file with "microphone", extract directory, show tree tree $(dirname $(grep -rl "microphone" . | head -1)) ``` ### Example 3: Find Multiple Directories, Tree Each One **Goal:** Find all directories named "_attachments" and show each one's structure. ```bash # Find all _attachments directories find . -type d -name "_attachments" # Output: # ./Audio Science/_attachments # ./Live Sound/_attachments # ./Computer Tech/Obsidian/_attachments # Show tree of each (using a loop) for dir in $(find . -type d -name "_attachments"); do echo "=== $dir ===" tree -L 1 "$dir" echo "" done ``` ### Example 4: Search Content, Save Directory Structure **Goal:** Find directories containing markdown files with "audio interface", then save their structure. ```bash # Find markdown files containing "audio interface" grep -rl --include="*.md" "audio interface" . # Get unique directories containing these files grep -rl --include="*.md" "audio interface" . | xargs dirname | sort -u # Show tree of each directory and save to file for dir in $(grep -rl --include="*.md" "audio interface" . | xargs dirname | sort -u); do echo "=== $dir ===" >> audio_interface_dirs.txt tree -L 1 "$dir" >> audio_interface_dirs.txt echo "" >> audio_interface_dirs.txt done ``` ### Example 5: Sequential Commands for Exploration **Goal:** Systematically explore a directory structure before deciding where to save a file. ```bash # Run multiple commands sequentially (separated by semicolons) tree -L 1 "Computer Tech/"; echo "---"; ls -la "Computer Tech/AI/"; echo "---"; find "Computer Tech/AI" -name "*.md" ``` Or use `&&` to stop if any command fails: ```bash # Only continue if each command succeeds tree -L 1 "Computer Tech/" && ls -la "Computer Tech/AI/" && find "Computer Tech/AI" -name "*.md" ``` ### Example 6: Build Directory Report **Goal:** Create a comprehensive report about a directory's structure and contents. ```bash # Create report file with multiple commands echo "Directory Report - $(date)" > report.txt echo "==================" >> report.txt echo "" >> report.txt echo "Directory Structure:" >> report.txt tree -L 2 "Audio Science/" >> report.txt echo "" >> report.txt echo "Markdown Files:" >> report.txt find "Audio Science/" -name "*.md" >> report.txt echo "" >> report.txt echo "File Count:" >> report.txt find "Audio Science/" -type f | wc -l >> report.txt ``` ### Example 7: Interactive Directory Selection **Goal:** Find directories, let grep filter them, then explore with tree. ```bash # Find all directories with "Audio" in name find . -type d | grep -i audio # Pick one from results and explore tree -L 2 "Audio Science/Audio Effects/" # Or combine: show tree of directories matching pattern find . -type d -name "*Effects*" -exec tree -L 1 {} \; ``` ### Example 8: Advanced Filtering **Goal:** Find directories created recently, filter by name, show structure. ```bash # Find directories modified in last 7 days find . -type d -mtime -7 # Pipe to grep to filter by name find . -type d -mtime -7 | grep "Audio" # Show tree of results (using command substitution) for dir in $(find . -type d -mtime -7 | grep "Audio"); do tree -L 1 "$dir" done ``` ## Practical Chaining Patterns Here are common patterns for everyday use: ```bash # Pattern 1: Find → Filter → Display find . -type d | grep "Audio" | head -5 # Pattern 2: Search → Extract Directory → Tree tree $(dirname $(grep -rl "pattern" . | head -1)) # Pattern 3: List → Count → Save ls -la | wc -l > file_count.txt # Pattern 4: Find → Execute Command on Each Result find . -name "*.md" -exec wc -l {} \; # Pattern 5: Multiple Commands → Same File echo "Report" > output.txt; tree -L 1 >> output.txt; ls -la >> output.txt # Pattern 6: Conditional Execution cd "Audio Science/" && tree -L 2 || echo "Directory not found" # Pattern 7: Find → Loop → Tree Each for dir in $(find . -maxdepth 2 -type d -name "*Audio*"); do tree -L 1 "$dir"; done ``` ## Practical Workflow When asked to save a file and unsure about directory structure: ```bash # 1. Get visual overview of target area tree -L 2 "Computer Tech/" # 2. Find similar directories find "Computer Tech/" -type d -name "*Unix*" # 3. See what's in the proposed directory ls -la "Computer Tech/Unix/UNIX Commands/" # 4. Verify similar files exist find "Computer Tech/Unix" -name "*.md" -type f ``` ## Tips - **Start with `tree`** for quick visual overview - **Use `find` with `-maxdepth`** to avoid overwhelming output - **Use `grep -l`** to see which files match (not every matching line) - **Combine `ls -la`** to see permissions, sizes, and dates - **Use quotes** around paths with spaces: `ls "Audio Science/"` - **Test in small directories first** before running on entire system ## Common Patterns ```bash # "Show me the structure of this folder" tree -L 2 /path/to/folder # "Which folders contain 'Audio' in the name?" find . -type d -iname "*audio*" # "What markdown files are here?" find . -type f -name "*.md" # "Which files mention 'mixer'?" grep -ril "mixer" . # "What's in this directory without going there?" ls -lah /path/to/directory ``` ## Related Commands - **[[find]]** - Detailed find command reference - **[[grep]]** - Detailed grep command reference - **`man grep`** - Manual page for grep (in terminal) - **`man find`** - Manual page for find (in terminal) - **`man tree`** - Manual page for tree (in terminal)