Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The -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 the test builtin, the POSIX-compliant single bracket [ ], or the Bash-specific double bracket [[ ]] keywords.
test -s FILE
[ -s FILE ]
[[ -s FILE ]]

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 -s operator checks the logical file size (st_size in the C stat structure), not the allocated size on disk (st_blocks). A sparse file can have 0 allocated bytes on disk but a logical size > 0, and -s will correctly evaluate to 0 (True).
  • Symlink Resolution: The -s operator 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 returns 1.
  • Directory Handling: When applied to a directory, -s typically evaluates to 0 (True). In POSIX-compliant filesystems, directories are special files that consume space to store metadata (inode pointers and filenames), meaning their st_size attribute is inherently greater than zero.
  • Special Files: For character special files (e.g., /dev/null), -s evaluates to 1 (False) because their st_size is 0. Similarly, on Linux and most POSIX systems, block special files (e.g., /dev/sda) have an st_size of 0 in their stat structure, regardless of the physical device’s capacity. Therefore, -s applied to a block device evaluates to 1 (False).
Master Bash with Deep Grasping Methodology!Learn More