Created
February 26, 2020 13:27
-
-
Save benwoodward/8873c8ad5517ce255ad04bd082979146 to your computer and use it in GitHub Desktop.
Mac/Zsh-only: List new files recursively by date created within current directory. Displays relative dates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
relative-date() { | |
while read input_string; do | |
local file_path=`echo $input_string | cut -d ' ' -f 5` | |
local ls_date=`echo $input_string | cut -d ' ' -f 1,2,3,4` | |
# Accepts date in format found in `ls` output, and converts to epoch | |
local date="$(date -j -f "%d %b %H:%M:%S %Y" "$ls_date" +"%s")" | |
local now="$(date +"%s")" | |
local time_diff=$((now - date)) | |
if ((time_diff > 24*60*60)); then | |
date_string=`printf "%.0f days ago" time_diff/((24*60*60))` | |
elif ((time_diff > 60*60)); then | |
date_string=`printf "%.0f hours ago" time_diff/((60*60))`; | |
elif ((time_diff > 60)); then | |
date_string=`printf "%.0f minutes ago" time_diff/60`; | |
else | |
date_string=`printf "%s seconds ago" $time_diff`; | |
fi | |
date_string=${(r:18:)date_string} | |
echo "$date:$date_string:\e[32m$file_path\e[m\n" | |
done | |
} | |
zle -N relative-date | |
function list-new-files() { | |
find . -type f \ | |
-not \( -wholename './.git*' -prune \) \ | |
-not \( -wholename './tags*' -prune \) \ | |
-exec ls -lTU {} \; | rev | cut -d ' ' -f 1,2,3,4,5 | rev | relative-date \ | |
| sort -k 1 -r \ | |
| rev \ | |
| cut -d ':' -f 1,2 \ | |
| rev \ | |
| sed 's/://g' | |
} | |
zle -N list-new-files | |
function new-files() { | |
if [ "$1" != "" ] | |
then | |
list-new-files | head -n "$1" | |
else | |
list-new-files | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment