Re: Post your .bashrc!
I use this .bashrc function to find regular files and symbolic links whose last modification or last status time is within a certain number of minutes. It prints last modification and status times, highlighting where those times are different, as well as file name, file size, and a guess (!) at the file type. Headers are printed, and numeric fields (file size, in this case) are right-adjusted. Useful for investigating what is going on in your system!
QDIRS is an whitespace-delimited environment variable holding the directories to be searched. BLUE and RESET are, respectively, '\033[1;34m' and '\033[0m'.
rf () {
# Find regular files and symbolic links whose last modification or last status time is within $1 minutes,
# highlighting where those two times are different.
# File names containing a tab character will not be processed correctly.
# File names containing a newline will be processed correctly but will print oddly!
# Any error messages other than "find: ... permission denied" (which is suppressed) will appear before the main report.
declare t=${1:-2}
declare -i i
declare -a a
( (( $# <= 1 )) && [[ "$t" =~ [[:digit:]]+ ]] ) || return $(die "Usage: $FUNCNAME [number of minutes]")
{
while IFS=$'\t' read -d $'\x0' -a a; do
for ((i = 0; i < ${#a[@]}; i++)); do
# Edit timestamps.
if ((i <= 1)); then
echo -n "${a[i]:0:10} ${a[i]:11:8}"
else
echo -n "${a[i]}"
fi
echo -ne '\t'
done
# Append a (sometimes not very good!) guess at what type of file this is.
echo -n $(file -pb "${a[2]}")
echo -ne '\x0'
done < <(find -O2 $QDIRS -xdev \( -mmin -"$t" -o -cmin -"$t" \) \( -type f -o -type l \) -printf '%T+\t%C+\t%p\t%s\0') |
sort -z -t $'\t' -s -k 1,1r -k 2,2r -k 3,3 |
gawk -v RS='\0' -v ORS='\0' -v header='Last mod\tLast status\tFile name\tSize\tFile type' -f ~/scripts/gawk/format |
gawk -v blue=$BLUE -v reset=$RESET 'BEGIN {RS="\0"; FS=OFS=" "} NR > 1 && $1 != $2 {$2 = blue $2 reset} {print}'
} 2>&1 | grep -v '^find.*Permission denied$'
}The function uses this bash error-handling helper function...
die () {
declare -i r=${2:-9}
echo "$1" 1>&2
return $r
}... and a general-purpose gawk program, ~/scripts/gawk/format, for formatting tab-delimited output, optionally adding a header:
BEGIN {
FS = "\t"; magenta = "\033[1;35m"; reset = "\033[0m"
if (header != "") {
nr = 1
nf = split(header, h, "\t")
for (f = 1; f <= nf; f++) {
line[nr,f] = h[f]
len[f] = length(h[f])
dec[f] = 1
}
}
}
{
nr++
for (f = 1; f <= NF; f++) {
sub(/ +$/, "", $f)
line[nr,f] = $f
if (length($f) > len[f])
len[f] = length($f)
if (nr == 1) {
nf = NF
dec[f] = 1
}
else {
if (dec[f] != 0 && $f !~ /^ *[[:digit:]]+$/)
dec[f] = 0
}
}
}
END {
printf magenta
for (f = 1; f <= nf; f++) {
if (dec[f] != 0) {
format[f] = "%" (len[f]) "s "
printf(format[f], line[1,f])
format[f] = "%" (len[f]) "d "
}
else {
format[f] = (f < nf) ? "%-" (len[f]) "s " : "%s"
printf(format[f], line[1,f])
}
}
print reset
for (r = 2; r <= nr; r++) {
for (f = 1; f <= nf; f++)
printf(format[f], line[r,f])
print ""
}
}A screenshot:
Last edited by qi (2009-11-15 01:19:01)