TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
-s operator is a unary file test operator in Bash used within conditional expressions to evaluate whether a specified file exists and has a logical size strictly greater than zero bytes. It relies on the stat system call to inspect the st_size attribute of the file’s inode.
Syntax
The operator requires a single string argument representing the file path and can be used with thetest builtin, the POSIX-compliant single bracket [ ], or the Bash-specific double bracket [[ ]] keywords.
Evaluation Mechanics
The operator evaluates the file path and returns a standard Bash exit status:- Returns
0(True): The path resolves to an existing file, and its logical size (st_size) is > 0 bytes. - Returns
1(False): The path resolves to an existing file, but its logical size is exactly 0 bytes. - Returns
1(False): The path does not exist, or the executing user lacks search (+x) permissions on a parent directory in the path.
Technical Characteristics
- Logical vs. Allocated Size: The
-soperator checks the logical file size (st_sizein the Cstatstructure), not the allocated size on disk (st_blocks). A sparse file can have 0 allocated bytes on disk but a logical size > 0, and-swill correctly evaluate to0(True). - Symlink Resolution: The
-soperator dereferences symbolic links. It evaluates the size of the target file, not the size of the symlink itself. If the symlink is broken (points to a non-existent target), the operator returns1. - Directory Handling: When applied to a directory,
-stypically evaluates to0(True). In POSIX-compliant filesystems, directories are special files that consume space to store metadata (inode pointers and filenames), meaning theirst_sizeattribute is inherently greater than zero. - Special Files: For character special files (e.g.,
/dev/null),-sevaluates to1(False) because theirst_sizeis 0. Similarly, on Linux and most POSIX systems, block special files (e.g.,/dev/sda) have anst_sizeof0in theirstatstructure, regardless of the physical device’s capacity. Therefore,-sapplied to a block device evaluates to1(False).
Master Bash with Deep Grasping Methodology!Learn More





