Interview Prep/Linux/Storage, disks and filesystems

Users complain that `ls` in a directory with 3 million files takes forever. What would you change?

AdvancedScenarioStorage, disks and filesystems
Answer

Situation read → Huge flat directories are slow to list and sort.

What I'd do →

  1. Short term: avoid sorting and stat: ls -f or find . -maxdepth 1 -printf '%f\n' | head.
  2. Restructure into hashed subdirectories (a1/b2/file), done by the app.
  3. Move processed files to dated archive dirs or object storage.
  4. Make sure dir_index is on for ext4 (tune2fs -l | grep features).

Why → ls reads and sorts every entry and, with colour or -l, stats each one.

Interview tip → Mention that rm * fails with "Argument list too long"; use find -delete.

All Linux questions Roadmap: Linux

More on Storage, disks and filesystems