# Listing Recently Edited Files ## Using ls with Time Sorting (Current Directory Only) The simplest way to see recently edited files is using `ls` with the `-t` flag: ```bash ls -lt ``` **Flags:** - `-l` - Long format (shows permissions, owner, size, timestamp) - `-t` - Sort by modification time, newest first **Example output:** ``` -rw-r--r-- 1 user staff 2048 Nov 3 17:30 config.json -rw-r--r-- 1 user staff 1024 Nov 3 16:45 notes.md drwxr-xr-x 5 user staff 160 Nov 2 14:20 scripts/ ``` **Important limitation:** `ls` only shows items in the current directory. The timestamp for a directory shows when files were added/removed from it, **not** when files inside it were modified. ### Show Human-Readable File Sizes ```bash ls -lth ``` The `-h` flag displays sizes in KB, MB, GB instead of bytes. ### Reverse Order (Oldest First) ```bash ls -ltr ``` The `-r` flag reverses the sort order. ## Using find for Recursive Searching **Use `find` when you need to search through subdirectories.** This is the main tool for discovering recently modified files across your entire directory tree. ### Find Files Modified Within Last N Days ```bash find . -type f -mtime -7 ``` **Options:** - `.` - Search current directory and subdirectories - `-type f` - Files only (exclude directories) - `-mtime -7` - Modified within last 7 days - `-mtime +7` - Modified more than 7 days ago - `-mtime 7` - Modified exactly 7 days ago ### Find and Display with Details (Sorted) To see modification times in a sorted list with file details: ```bash find . -type f -mtime -7 -exec ls -lth {} + | sort -k6,7 ``` Or for better sorting by timestamp on macOS: ```bash find . -type f -mtime -7 -print0 | xargs -0 ls -lth ``` ### Find Most Recently Modified Files (Top 10) ```bash find . -type f -exec stat -f "%m %N" {} + | sort -rn | head -10 | cut -d' ' -f2- ``` **macOS version explained:** - `stat -f "%m %N"` - Outputs modification timestamp (seconds since epoch) and filename - `sort -rn` - Sort numerically in reverse (newest first) - `head -10` - Take top 10 results - `cut -d' ' -f2-` - Remove the timestamp, show only filenames **Linux version:** ```bash find . -type f -exec stat -c "%Y %n" {} + | sort -rn | head -10 | cut -d' ' -f2- ``` ### Find Files Modified in Last N Minutes ```bash find . -type f -mmin -60 ``` The `-mmin` flag uses minutes instead of days. This example finds files modified in the last hour. ### Find Recently Modified Files with Human-Readable Output ```bash find . -type f -mtime -1 -exec ls -lth {} + ``` Shows all files modified in the last 24 hours with readable timestamps and file sizes. ## Using stat for Detailed Timestamps View detailed timestamp information for a specific file: ```bash stat filename.txt ``` **macOS output includes:** - Modification time (`Modify`) - Access time (`Access`) - Change time (`Change`) - when metadata was changed - Birth time (`Birth`) - when file was created **Linux equivalent:** ```bash stat -c '%y %n' filename.txt ``` ## Practical Examples ### List 10 Most Recently Modified Files in Current Directory Only ```bash ls -lt | head -11 ``` The `head -11` shows the header line plus 10 files. Remember: this only shows files in the current directory. ### List 10 Most Recently Modified Files Recursively ```bash find . -type f -exec stat -f "%m %N" {} + | sort -rn | head -10 ``` This searches all subdirectories and shows the 10 most recently modified files anywhere in the tree. ### Find Recently Modified Markdown Files ```bash find . -name "*.md" -mtime -7 -exec ls -lth {} + ``` Finds all `.md` files modified in the last 7 days, recursively. ### Find Recently Modified Files in Specific Directory ```bash find "Computer Tech" -type f -mtime -1 -exec ls -lth {} + ``` Searches only within the "Computer Tech" directory and its subdirectories. ### Monitor a Directory for Changes For current directory only: ```bash ls -lt | head -20 ``` For recursive monitoring with real-time updates: ```bash watch -n 5 'find . -type f -mmin -60 | head -20' ``` This updates every 5 seconds, showing files modified in the last hour. ## Comparing Timestamps ### Access vs Modification Time - **Modification time** (`-mtime`, `ls -t`) - When file content was last changed - **Access time** (`-atime`, `ls -tu`) - When file was last read - **Change time** (`-ctime`, `ls -tc`) - When metadata (permissions, ownership) was changed View files by access time: ```bash ls -ltu ``` View files by change time: ```bash ls -ltc ``` ## Related Commands - [[find]] - Powerful file searching tool - [[grep]] - Search within file contents - [[File and Directory Exploration]] - Basic navigation commands ## Common Pitfall: Directory Timestamps **Important:** When you run `ls -lth` and see a directory timestamp, that shows when the **directory itself** was modified (files added/removed), not when files inside it changed. **Example:** ```bash drwxr-xr-x 19 staff 608B Nov 1 23:01 Computer Tech ``` This means something was added/removed from the `Computer Tech` directory on Nov 1, but files inside could have been edited today. To find recently modified files inside, use `find`: ```bash find "Computer Tech" -type f -mtime -1 ``` ## Quick Reference Summary | Task | Command | |------|---------| | Current directory only (sorted) | `ls -lth` | | Recursive, last 7 days | `find . -type f -mtime -7` | | Recursive, top 10 most recent | `find . -type f -exec stat -f "%m %N" {} + \| sort -rn \| head -10` | | Specific file type, last day | `find . -name "*.md" -mtime -1` | | Last 60 minutes | `find . -type f -mmin -60` | ## Tips - Use `-1` flag with `ls` for one file per line: `ls -1t` - Combine with [[grep]] to filter results: `ls -lt | grep ".md"` - On macOS, `ls -lT` shows full timestamp format - Use `tree` command with `-D` flag to show modification times in tree view - **Always use `find` when you need to search recursively through subdirectories**