#!/bin/bash #FILES=$(ls *.gmi) SAVE_EXTENSION='original_time' show_usage() { echo "Usage: $0 OPTION FILE..." echo "Save or restore the original timestamps of a collection of files." echo echo " save - save the original timestamps of files" echo " restore - restore the original timestamps of files" echo } save_timestamps() { for file in "$@" do touch -amr $file $file.$SAVE_EXTENSION echo "$file timestamps saved to $file.$SAVE_EXTENSION" done } restore_timestamps() { ls *.$SAVE_EXTENSION > /dev/null 2>&1 if [[ $? -gt 0 ]]; then echo "error: could not find stored timestamps" exit 1 fi for file in $(ls *.$SAVE_EXTENSION) do if [[ ${file::-$((${#SAVE_EXTENSION} + 1))} -nt $file ]]; then touch -amr $file ${file::-$((${#SAVE_EXTENSION} + 1))} echo "${file::-$((${#SAVE_EXTENSION} + 1))} timestamps restored from $file" fi rm $file done } if [[ -z $1 || -z $2 ]]; then show_usage else if [[ $1 == "save" ]]; then shift save_timestamps "$@" elif [[ $1 == "restore" ]]; then restore_timestamps else show_usage fi fi