[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to check a file's modify time is at epoch 0?
From: |
Stephane Chazelas |
Subject: |
Re: How to check a file's modify time is at epoch 0? |
Date: |
Mon, 28 Oct 2019 07:48:03 +0000 |
User-agent: |
NeoMutt/20171215 |
2019-10-27 18:29:36 -0500, Peng Yu:
> Thanks. But I am looking for something native to bash. Involvement
> with external programs will slow down the run time.
[...]
bash, contrary to zsh has no builtin interface to
stat()/lstat(), however it's [ builtin has a -nt operator to
compare modification time of two files (not standard yet but
very widespread)
If you have many files for which you want to test whether the
mtime is 0, you could create a reference one like:
touch -d @0 epoch.reference # GNU syntax
TZ=UTC0 touch -t 197001010000.00 epoch.reference # standard syntax
Then:
for file in *; do
if [ ! "$file" -nt epoch.reference ]; then
printf '%s\n' "$file's modification time is 0"
fi
done
(note that for symlinks, that's the mtime of the target that is
being considered)
But if you have more than one file to consider, maybe you should
consider using find instead or any programming language with a
stat() API
find . ! -newer epoch.reference ! -path ./epoch.reference # standard
find . ! -newermt @0 # GNU
With GNU find, you can also print the mtime of files in epoch
time with:
find . -printf '%Ts %p\n'
Or store it in a bash associative array with:
typeset -A mtime
while IFS=/ read -rd '' t file; do
mtime[$file]=$t
done < <(
find . -printf '%Ts/%p\0'
)
--
Stephane