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 -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 the test command ([), or the Bash extended test construct ([[).
[ "$file1" -nt "$file2" ]
[[ $file1 -nt $file2 ]]
When using the [ 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 file1State of file2ConditionExit Status
ExistsExistsmtime(file1) > mtime(file2)0 (True)
ExistsExistsmtime(file1) <= mtime(file2)1 (False)
ExistsDoes not existN/A0 (True)
Does not existExistsN/A1 (False)
Does not existDoes not existN/A1 (False)

Technical Characteristics

  • Timestamp Resolution: The precision of the -nt comparison is strictly bound by the underlying filesystem. Modern filesystems (like ext4, xfs, or zfs) support nanosecond precision, allowing -nt to differentiate between files created fractions of a second apart. Older filesystems (like FAT32) 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 -nt operator automatically dereferences symbolic links. It compares the mtime of 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 mtime of 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 file1 and file2 have the exact same mtime, -nt evaluates to false (1).
Master Bash with Deep Grasping Methodology!Learn More