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.
-nt (newer than) operator is a binary file test operator in Bash used to compare the modification timestamps (mtime) of two files. It evaluates to true (exit status 0) if the first file was modified more recently than the second file, or if the first file exists and the second file does not.
Syntax and Variable Quoting
The operator is evaluated using either thetest command ([), or the Bash extended test construct ([[).
[ command, variables must be quoted. If an unquoted variable evaluates to an empty string or contains spaces, word splitting alters the number of arguments passed to [, resulting in a syntax error (e.g., [: -nt: unary operator expected). The Bash extended test construct ([[) safely handles unquoted variables because it suppresses word splitting and pathname expansion during evaluation.
Portability
While the[ command is a standard POSIX utility, the -nt operator is historically a Bash and KornShell (ksh) extension. Although -nt was recently formalized in the POSIX.1-2024 standard, using [ "$file1" -nt "$file2" ] is not universally portable. Executing this operator in older or strictly POSIX-compliant shells (such as yash or older dash/ash environments) may result in an evaluation failure or syntax error.
Evaluation Logic
The operator returns a boolean exit status based on the following matrix:State of file1 | State of file2 | Condition | Exit Status |
|---|---|---|---|
| Exists | Exists | mtime(file1) > mtime(file2) | 0 (True) |
| Exists | Exists | mtime(file1) <= mtime(file2) | 1 (False) |
| Exists | Does not exist | N/A | 0 (True) |
| Does not exist | Exists | N/A | 1 (False) |
| Does not exist | Does not exist | N/A | 1 (False) |
Technical Characteristics
- Timestamp Resolution: The precision of the
-ntcomparison is strictly bound by the underlying filesystem. Modern filesystems (likeext4,xfs, orzfs) support nanosecond precision, allowing-ntto differentiate between files created fractions of a second apart. Older filesystems (likeFAT32) may only support 2-second resolution, which can result in a false negative (1) if two files are modified within the same resolution window. - Symlink Dereferencing: The
-ntoperator automatically dereferences symbolic links. It compares themtimeof the ultimate target file, not the inode of the symlink itself. If a symlink is broken (points to a non-existent target), Bash evaluates it as a non-existent file according to the logic matrix above. - Directory Handling: Directories are treated as standard files. The operator evaluates the
mtimeof the directory’s inode, which is updated only when the directory’s direct contents are modified (e.g., a file is created, deleted, or renamed within it), not when the contents of files within the directory are modified. - Strict Inequality: The operator performs a strictly greater-than comparison. If
file1andfile2have the exact samemtime,-ntevaluates to false (1).
Master Bash with Deep Grasping Methodology!Learn More





