Last active
August 29, 2015 14:01
-
-
Save smikhalevski/87bb873491d871782d3b to your computer and use it in GitHub Desktop.
Handy bash utilities.
This file contains hidden or 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
# Prints msec in format "[d days ]hh:mm:ss.SSS" | |
function format_duration #(msec=0) | |
{ | |
local t=${1:-0} | |
local days=$((t/60/60/24)) | |
[ $days -gt 0 ] && echo -ne "$days days " | |
printf "%d:%02d:%02d" $((t/60/60%24)) $((t/60%60)) $((t%60)) | |
} | |
# Prints file permissions in decimal mode, ex. 755. | |
# $ perm ~ | |
# $ 750 | |
function perm #(path=.) | |
{ | |
[ -e "${1:-.}" ] && stat -c %a "${1:-.}" | |
} | |
# Prints timestamp of elderly modified file matching mask in provided directory. | |
# If directory does not exist or no files matching mask present then nothing is output. | |
function eldest_content_mod_ts #(path=., filename_mask=.*) | |
{ | |
#if [ -n "`basename '$1'`" ]; then | |
if [ -d "${1:-.}" ]; then | |
#find "`dirname '$1'`" -type f -maxdepth 1 -name "`basename '$1'`" -printf "%C@ %T@ %P\n" | |
find "${1:-.}" -maxdepth 1 -name "${2:-.*}" -printf "%C@ %T@ %P\n" \ | |
| awk '{ print ($1>$2?$1:$2) }' \ | |
| sort -bgk 1 \ | |
| tail -n 1 | |
fi | |
} | |
# Prints names of files which match. | |
# perm - exact match | |
# -perm - greater or equal | |
# +perm - less or equal | |
function match_perm #(filename_mask, perm) | |
{ | |
find "`dirname $1`" -maxdepth 1 -name "`basename $1`" -printf "%P\n" -perm +`printf %03d "${2:-0}"` | |
} | |
# Prints directory content age in msec. | |
function content_age #(path=., filename_mask=.*) | |
{ | |
local mod_ts="`eldest_content_mod_ts \"$1\" \"$2\"`" | |
if [ -n "$mod_ts" ]; then | |
expr "`date +%s` - $mod_ts" | |
fi | |
} | |
# Returns 0 iff provided path is mounted, otherwhise 1. | |
function mounted #(path=.) | |
{ | |
[ -d "${1:-.}" ] && [[ ! `stat -f -L -c %T "${1:-.}"` =~ "ext.*" ]] && return 0 | |
return 1 | |
} | |
# Returns 0 iff permissions for path are greater or equal to perm. | |
function enough_perm #(path, perm) | |
{ | |
local act=`perm "$1"` | |
local exp=`printf %03d "${2:-0}"` | |
[ "${act:0:1}" -ge "${exp:0:1}" ] && [ "${act:1:1}" -ge "${exp:1:1}" ] && [ "${act:2:1}" -ge "${exp:2:1}" ] && return 0 | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment