Count Files Safely from Bash

Read the full interview experience this question came from →

Quick Overview

Count regular files from Bash without breaking on hidden files, spaces, or newlines. Learn safe find usage, recursion and symlink semantics, and why parsing ls is unreliable.

Count Files Safely from Bash

Company: Squarepoint

Role: Quantitative Researcher

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

Write and explain a Bash approach that counts regular files under a directory recursively. It must handle hidden files and filenames containing spaces or newlines. State how symbolic links are treated, how to count only the immediate directory instead, and what can go wrong with common `ls | wc -l` solutions. ### Constraints & Assumptions - Bash and a `find` implementation supporting `-print0` are available. - By default, symbolic links are not followed and a symlink to a regular file is not counted as a regular file. - Errors such as unreadable subdirectories must not be mistaken for a trustworthy complete count. ### Clarifying Questions to Ask - Should traversal be recursive or limited to one directory level? - Should symbolic links be followed or counted as entries? - Should directories, sockets, and named pipes be excluded? - What should happen if part of the directory tree cannot be read? ```hint Do not parse display output Use a NUL-delimited stream of paths or emit one fixed token per matched file instead of counting text lines from `ls`. ``` ### What a Strong Answer Covers - A filename-safe command or Bash loop based on `find`. - Hidden-file behavior, recursion depth, and symlink semantics. - Error-status handling and the limitations of `ls` parsing. - A clear immediate-directory variant. ### Follow-up Questions - How would you count unique inodes when hard links exist? - How would following symlinks introduce cycles or duplicate traversal? - How could the count change while the command is running?

Overview: Count regular files from Bash without breaking on hidden files, spaces, or newlines. Learn safe find usage, recursion and symlink semantics, and why parsing ls is unreliable.

Read the full Squarepoint Quantitative Researcher interview experience this question came from

|Home/Software Engineering Fundamentals/Squarepoint
Squarepoint logo
Squarepoint
Aug 29, 2026
mediumQuantitative ResearcherTechnical ScreenSoftware Engineering Fundamentals
1
0

Write and explain a Bash approach that counts regular files under a directory recursively. It must handle hidden files and filenames containing spaces or newlines. State how symbolic links are treated, how to count only the immediate directory instead, and what can go wrong with common ls | wc -l solutions.

Constraints & Assumptions

  • Bash and a find implementation supporting -print0 are available.
  • By default, symbolic links are not followed and a symlink to a regular file is not counted as a regular file.
  • Errors such as unreadable subdirectories must not be mistaken for a trustworthy complete count.

Clarifying Questions to Ask Guidance

  • Should traversal be recursive or limited to one directory level?
  • Should symbolic links be followed or counted as entries?
  • Should directories, sockets, and named pipes be excluded?
  • What should happen if part of the directory tree cannot be read?

What a Strong Answer Covers Guidance

  • A filename-safe command or Bash loop based on find .
  • Hidden-file behavior, recursion depth, and symlink semantics.
  • Error-status handling and the limitations of ls parsing.
  • A clear immediate-directory variant.

Follow-up Questions Guidance

  • How would you count unique inodes when hard links exist?
  • How would following symlinks introduce cycles or duplicate traversal?
  • How could the count change while the command is running?
Loading comments...