15 December 2025

how to find newly created/changed files/directories on ubuntu?

The following command finds the newest 10 files -

$ 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. 



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

find /path/to/search -mtime -N

Example: files changed in the last 2 days

find ~ -mtime -2

🔹 Files changed in the last N minutes

find ~ -mmin -30

🔹 Newly created files (best approximation)

Linux doesn’t always store true “creation time”, but you can check change time:

find ~ -ctime -1
OptionMeaning
-mtimeFile content modified
-ctimeMetadata changed (permissions, ownership, creation on some FS)
-mminModified minutes ago

2️⃣ Only directories (not files)

find ~ -type d -mtime -1

Example: directories changed today.


3️⃣ Sort files by newest first

🔹 Files

ls -lt

🔹 Directories only

ls -lt --group-directories-first

🔹 Recursive, newest last

ls -ltrR

4️⃣ Using stat (detailed timestamps)

stat filename

Shows:

  • Access time

  • Modify time

  • Change time

  • Birth time (if filesystem supports it)

Example:

stat myfile.txt

5️⃣ Find files changed since a specific date/time

🔹 Create a reference file

touch /tmp/ref_time

🔹 Later, find newer files

find ~ -newer /tmp/ref_time

Very useful for tracking changes during installs or scripts.


6️⃣ Real-time monitoring (advanced)

🔹 Monitor directory changes live

inotifywait -m -r ~/somefolder

(Requires package)

sudo apt install inotify-tools

7️⃣ GUI (Files app / Nautilus)

  1. Open Files

  2. Go to the folder

  3. Click ⋮ → Sort by → Modified

  4. Choose Descending


🔑 Which method should you use?

ScenarioBest 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