Skip to content

Instantly share code, notes, and snippets.

@4piu
Created October 19, 2025 00:57
Show Gist options
  • Save 4piu/85770ca30ab16533a98043ed26e900fb to your computer and use it in GitHub Desktop.
Save 4piu/85770ca30ab16533a98043ed26e900fb to your computer and use it in GitHub Desktop.
Update timestamps (touch) for files under specified directory.
#!/bin/bash
# Global variable for verbose mode
VERBOSE=false
# Function to update timestamps recursively
update_timestamps() {
local dir="$1"
local full_path
# Use absolute path if directory starts with /, otherwise treat as relative to pwd
if [[ "$dir" = /* ]]; then
full_path="$dir"
else
full_path="$(pwd)/$dir"
fi
if [[ ! -d "$full_path" ]]; then
echo "Warning: Directory '$full_path' does not exist"
return 1
fi
echo "Updating timestamps in: $full_path"
# Update timestamps for all files and directories recursively
if [[ "$VERBOSE" == "true" ]]; then
find "$full_path" -exec sh -c 'printf "\r\033[KProcessing: %s" "$1"; touch "$1"' _ {} \;
echo "" # Print newline at the end
else
find "$full_path" -exec touch {} \;
fi
}
# Check if arguments are provided
if [[ $# -eq 0 ]]; then
echo "Usage: $0 [-v] <directory1> [directory2] [directory3] ..."
echo "Options:"
echo " -v Verbose mode - print each file being processed"
echo "Example: $0 -v src data config"
exit 1
fi
# Parse options
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=true
shift
;;
-*)
echo "Unknown option: $1"
exit 1
;;
*)
break
;;
esac
done
# Check if any directories are specified after parsing options
if [[ $# -eq 0 ]]; then
echo "Error: No directories specified"
echo "Usage: $0 [-v] <directory1> [directory2] [directory3] ..."
exit 1
fi
# Process each directory argument
for dir in "$@"; do
update_timestamps "$dir"
done
echo "Timestamp update completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment