The following command finds the newest 10 files -
$ find . -type f -printf '%T@ %P\n' | sort -nr | head -n 10 | cut -d' ' -f2-
$ find . -type f -printf '%T@ %P\n' | sort -nr | head -n 10 | cut -d' ' -f2-
This Linux command finds and lists the **10 most recently modified files** (not directories) in the current directory (`.`) and all its subdirectories, showing their relative paths in order from newest to oldest.
Let's break it down step by step:
1. `find . -type f -printf '%T@ %P\n'`
- `find .`: Starts searching from the current directory (`.`) and recurses into all subdirectories.
- `-type f`: Restricts the search to regular **files only** (excludes directories, symlinks, etc.).
- `-printf '%T@ %P\n'`:
- `%T@`: Prints the file's **last modification time** as a Unix timestamp (seconds since epoch, with decimal fraction for sub-second precision).
- Space: Adds a literal space separator.
- `%P`: Prints the file's **path relative** to the starting directory (`.`), without the leading `./`.
- `\n`: Newline, so each file is on its own line.
- Output example:
```
1734621234.5678901234 path/to/recent/file.txt
1734600000.0000000000 path/to/older/file.jpg
...
```
2. `| sort -nr`
- Pipes the output to `sort`.
- `-n`: **Numeric** sort (treats the timestamp as a number, not as text).
- `-r`: **Reverse** order (descending, so highest/newest timestamps first).
- Result: Lines are now sorted from most recently modified to least recently modified.
3. `| head -n 10`
- Takes only the **first 10 lines** of the sorted output.
- This gives you the 10 newest files.
4. `| cut -d' ' -f2-`
- Pipes to `cut`.
- `-d' '`: Uses a **space** as the field delimiter.
- `-f2-`: Outputs fields **2 and everything after** (i.e., skips the timestamp and keeps only the file path).
- Result: Clean list of the 10 most recently modified file paths, one per line.
### Final output example:
```
documents/report_final.docx
pictures/vacation/photo123.jpg
src/main.py
logs/app.log
...
```
### Summary
This is a common and efficient way to get the **top 10 recently modified files** in a directory tree. It's more precise than using `-ls` or `-exec stat` because `%T@` provides sub-second timestamp resolution for accurate sorting.
### Alternatives
- If you want access time instead of modification time: Replace `%T@` with `%A@`.
- If you want human-readable dates instead of just paths: Remove the final `cut` and adjust the `sort`/`head` accordingly.
- On macOS (BSD find), `-printf` isn't available by default — you'd need to use `stat` or install GNU find via Homebrew.
Let's break it down step by step:
1. `find . -type f -printf '%T@ %P\n'`
- `find .`: Starts searching from the current directory (`.`) and recurses into all subdirectories.
- `-type f`: Restricts the search to regular **files only** (excludes directories, symlinks, etc.).
- `-printf '%T@ %P\n'`:
- `%T@`: Prints the file's **last modification time** as a Unix timestamp (seconds since epoch, with decimal fraction for sub-second precision).
- Space: Adds a literal space separator.
- `%P`: Prints the file's **path relative** to the starting directory (`.`), without the leading `./`.
- `\n`: Newline, so each file is on its own line.
- Output example:
```
1734621234.5678901234 path/to/recent/file.txt
1734600000.0000000000 path/to/older/file.jpg
...
```
2. `| sort -nr`
- Pipes the output to `sort`.
- `-n`: **Numeric** sort (treats the timestamp as a number, not as text).
- `-r`: **Reverse** order (descending, so highest/newest timestamps first).
- Result: Lines are now sorted from most recently modified to least recently modified.
3. `| head -n 10`
- Takes only the **first 10 lines** of the sorted output.
- This gives you the 10 newest files.
4. `| cut -d' ' -f2-`
- Pipes to `cut`.
- `-d' '`: Uses a **space** as the field delimiter.
- `-f2-`: Outputs fields **2 and everything after** (i.e., skips the timestamp and keeps only the file path).
- Result: Clean list of the 10 most recently modified file paths, one per line.
### Final output example:
```
documents/report_final.docx
pictures/vacation/photo123.jpg
src/main.py
logs/app.log
...
```
### Summary
This is a common and efficient way to get the **top 10 recently modified files** in a directory tree. It's more precise than using `-ls` or `-exec stat` because `%T@` provides sub-second timestamp resolution for accurate sorting.
### Alternatives
- If you want access time instead of modification time: Replace `%T@` with `%A@`.
- If you want human-readable dates instead of just paths: Remove the final `cut` and adjust the `sort`/`head` accordingly.
- On macOS (BSD find), `-printf` isn't available by default — you'd need to use `stat` or install GNU find via Homebrew.
On Ubuntu (and Linux in general), there are several reliable ways to find newly created or recently changed files/directories, depending on how precise and how far back you want to look.
Below are the most useful and practical methods, from simple to advanced.
1️⃣ Using find (most common & flexible)
🔹 Files changed in the last N days
Example: files changed in the last 2 days
🔹 Files changed in the last N minutes
🔹 Newly created files (best approximation)
Linux doesn’t always store true “creation time”, but you can check change time:
| Option | Meaning |
|---|---|
-mtime | File content modified |
-ctime | Metadata changed (permissions, ownership, creation on some FS) |
-mmin | Modified minutes ago |
2️⃣ Only directories (not files)
Example: directories changed today.
3️⃣ Sort files by newest first
🔹 Files
🔹 Directories only
🔹 Recursive, newest last
4️⃣ Using stat (detailed timestamps)
Shows:
-
Access time
-
Modify time
-
Change time
-
Birth time (if filesystem supports it)
Example:
5️⃣ Find files changed since a specific date/time
🔹 Create a reference file
🔹 Later, find newer files
Very useful for tracking changes during installs or scripts.
6️⃣ Real-time monitoring (advanced)
🔹 Monitor directory changes live
(Requires package)
7️⃣ GUI (Files app / Nautilus)
-
Open Files
-
Go to the folder
-
Click ⋮ → Sort by → Modified
-
Choose Descending
🔑 Which method should you use?
| Scenario | Best choice |
|---|---|
| “What changed today?” | find -mtime -1 |
| “What changed in last 10 minutes?” | find -mmin -10 |
| “What did a script change?” | find -newer |
| “Watch changes live” | inotifywait |
| “Quick visual check” | GUI sort |
No comments:
Post a Comment