Return the largest file size in a directory
Problem
Implement a Bash function or shell snippet that identifies and returns the maximum file size (in bytes) among all regular files within a specified directory and its subdirectories.
Requirements
- Input: A directory path
DIR. - File Type: Consider only regular files. Directories should be ignored. Symlinks may be ignored, or followed if explicitly chosen.
- Traversal: Search recursively through
DIRand all its subdirectories. - Output: A single integer representing the size (in bytes) of the largest regular file found.
- Edge Case: If no regular files are found within
DIRor its subdirectories, the output should be0.
Example
Given a directory DIR containing regular files with sizes 10 bytes, 25 bytes, and 7 bytes (potentially located in various subdirectories), the expected output is 25.
AI interview prep
Core dilemma
Balancing the simplicity and readability of shell commands with robustness against edge cases (special characters in filenames, permissions, empty directories) and performance for very large file systems.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Return the largest file size in a directory
Problem
Implement a Bash function or shell snippet that identifies and returns the maximum file size (in bytes) among all regular files within a specified directory and its subdirectories.
Requirements
- Input: A directory path
DIR. - File Type: Consider only regular files. Directories should be ignored. Symlinks may be ignored, or followed if explicitly chosen.
- Traversal: Search recursively through
DIRand all its subdirectories. - Output: A single integer representing the size (in bytes) of the largest regular file found.
- Edge Case: If no regular files are found within
DIRor its subdirectories, the output should be0.
Example
Given a directory DIR containing regular files with sizes 10 bytes, 25 bytes, and 7 bytes (potentially located in various subdirectories), the expected output is 25.