Created
November 8, 2012 21:22
-
-
Save agile/4041715 to your computer and use it in GitHub Desktop.
Determine modification rate for a file
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
#!/bin/bash | |
# | |
# Hack to extract stats for a file including the actual creation time | |
# | |
# Uses debugfs which includes the crtime value in it's output: | |
# | |
# Inode: 23072101 Type: regular Mode: 0664 Flags: 0x80000 | |
# Generation: 1861227769 Version: 0x00000000:00000001 | |
# User: 1000 Group: 1000 Size: 0 | |
# File ACL: 0 Directory ACL: 0 | |
# Links: 1 Blockcount: 0 | |
# Fragment: Address: 0 Number: 0 Size: 0 | |
# ctime: 0x509a7efd:a0af6024 -- Wed Nov 7 09:32:13 2012 | |
# atime: 0x509a7efd:a0af6024 -- Wed Nov 7 09:32:13 2012 | |
# mtime: 0x509a7efd:a0af6024 -- Wed Nov 7 09:32:13 2012 | |
# crtime: 0x509a7efd:a0af6024 -- Wed Nov 7 09:32:13 2012 | |
# Size of extra inode fields: 28 | |
# EXTENTS: | |
set -e | |
set -o nounset | |
set -o errexit | |
info() { | |
local file="${1:-}" | |
local show_details="${2:-}" | |
if [ ! -e "${file}" ]; then | |
echo "'${file}' not found" | |
exit 1 | |
fi | |
if [ ! -x "/sbin/debugfs" ]; then | |
echo "debugfs not found" | |
exit 1 | |
fi | |
if [ "${EUID}" -ne "0" ]; then | |
super=$(which sudo) | |
else | |
super='' | |
fi | |
full_file=$(readlink -f "${file}") | |
device=$(df "${full_file}" 2>/dev/null|tail -n1|cut -f1 -d ' ') | |
if [ -n "${device:-}" ]; then | |
OFS=${IFS} | |
IFS="$(echo -e "\n\r")" | |
stats=($(${super} /sbin/debugfs -R "stat ${full_file}" ${device} 2>/dev/null|sed -nr 's/.*Inode: ([0-9]+).+/\1/p; s/.*User:.+Group:.+Size: ([0-9]+).*/\1/p;s/.*time:.+([A-Z][a-z]{2} [A-Z][a-z]{2}\s+[0-9]+.+[0-9]{4})/\1/p; T;')) | |
IFS=${OFS} | |
declare -A details | |
c=0 | |
for k in inode size ctime atime mtime crtime; do | |
details[${k}]="${stats[${c}]:-}" | |
c=$(($c+1)) | |
done | |
s1=$(date -d "${details[mtime]}" +%s) | |
s2=$(date -d "${details[crtime]}" +%s) | |
elapsed=$(( s1 - s2 )) | |
if (( elapsed > 0 )) ; then | |
rate=$(printf "%0.3f" $(echo "${details[size]} / ${elapsed}"|bc -l )) | |
else | |
rate=$(printf "%0.3f" "${details[size]}") | |
fi | |
if [ -n "${2:-}" ]; then | |
echo "file: ${full_file}" | |
echo "inode: ${details[inode]}" #${inode}" | |
echo "created at: ${details[crtime]}" | |
echo "modified at: ${details[mtime]}" | |
echo "current size: ${details[size]} Bytes" | |
echo "elapsed mod time/rate: ${elapsed} seconds @ ${rate} Bytes/sec" | |
else | |
echo "${rate} ${full_file}" | |
fi | |
fi | |
} | |
if [ -z "${1:-}" ]; then | |
echo "USAGE: ${0} [file] <any more options will display more details about the file>" | |
echo | |
echo "Returns a value for the number of Bytes/sec were written to the file between the" | |
echo "time it was created and last modification time." | |
echo | |
echo "NOTES: " | |
echo " Assumes you are root or have sudo permission to run the script." | |
echo " Only known to work on ext4 fs." | |
exit 1 | |
else | |
info $* | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment