How to List Files Recursively with Full Modified Timestamps in Tree-Like Format on Linux


7 views

While the tree command is excellent for displaying directory structures, its timestamp display has limitations. The -D flag only shows full date+time for current year files, truncating older files to just dates. This becomes problematic when you need precise modification times across all files for auditing or synchronization purposes.

For RHEL 5 (which typically comes with tree v1.5.0), we can combine find with tree to achieve our goal:

find /path/to/directory -printf "%TY-%Tm-%Td %TT %p\n" | tree --fromfile

Breakdown of components:

  • -printf "%TY-%Tm-%Td %TT %p\n" - Formats output as: YYYY-MM-DD HH:MM:SS.SSSSSSSSS path/to/file
  • | tree --fromfile - Pipes this formatted output into tree's file-reading mode

For regular use, consider adding this function to your .bashrc:

function treets() {
    local dir=${1:-.}
    find "${dir}" -printf "%TY-%Tm-%Td %TT\t%p\n" | \
    awk -F'\t' '{
        split($1, d, " ");
        printf "%-19s ", d[1]" "d[2];
        system("echo \""$2"\" | sed -e \"s#'\"$(pwd)\"/'##\" -e \"s#'\"$(pwd)\"'#.#\" | tree --fromfile -n");
    }' | tail -n +2
}

Example usage showing precise timestamps for all files:

$ treets /project/src
2023-12-15 14:25:31.000000000 .
├── 2023-12-10 09:15:42.000000000 main.c
├── 2023-11-28 16:42:03.000000000 utils/
│   ├── 2023-11-28 16:42:03.000000000 helpers.c
│   └── 2022-08-14 11:33:17.000000000 legacy.c
└── 2021-05-09 08:07:29.000000000 README.md

For directories with thousands of files, this approach might be slow. In such cases, consider:

find /path -type f -printf "%TY-%Tm-%Td %TT\t%p\n" > filelist.txt
# Then process in batches if needed

The -printf format is flexible. Some useful variations:

# ISO 8601 format
find . -printf "%TY-%Tm-%TdT%TT%Tz\t%p\n"

# Human-readable with timezone
find . -printf "%Ta %Tb %Td %TT %TY %Tz\t%p\n"

# Epoch seconds
find . -printf "%T@\t%p\n"

While the tree command is excellent for visualizing directory structures, its timestamp display has limitations. The -D flag shows incomplete timestamp information:

tree -D /path/to/directory

This outputs dates without times for previous years, making it unsuitable for detailed file auditing or synchronization tasks.

Here are three approaches to get comprehensive timestamp information in a tree-like format:

Method 1: Combining find and tree

First generate timestamps with find, then format with tree:

find /path/to/dir -printf "%TY-%Tm-%Td %TT %p\n" | awk '{print $1" "$2" "$3}'

For a more tree-like output:

find /path/to/dir -printf "%TY-%Tm-%Td %TT %p\n" | \
awk '{
    depth = gsub(/[^\/]\/[^\/]/,"");
    printf("%"depth*2"s", "");
    print $1" "$2" "$3
}'

Method 2: Enhanced tree with Custom Script

Create a wrapper script for richer output:

#!/bin/bash
DIR=${1:-.}
find "$DIR" -printf "%TY-%Tm-%Td %TT\t%p\n" | \
while IFS=$'\t' read -r date path; do
    indent=$(echo "$path" | grep -o / | wc -l)
    printf "%$((indent*2))s%s %s\n" "" "$date" "${path##*/}"
done

Method 3: Using ls with Recursive Option

For a quick solution with color support:

ls -lRt --time-style=full-iso /path/to/dir | \
awk '/^total/{next} /^d/{dir=$NF; next} {print dir"/"$8,$6,$7}'

For large directories:

  • The find method is generally fastest
  • Avoid multiple stat calls by using -printf in one pass
  • Consider -maxdepth if you only need certain levels

Customize timestamp display with these find format specifiers:

%TY-%Tm-%Td  # Year-Month-Day
%TT         # Time with seconds
%TFT%TZ     # ISO 8601 format