Last active
August 29, 2015 13:56
-
-
Save mjf/8974107 to your computer and use it in GitHub Desktop.
getfinfo - Get file information formatted
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
#! /bin/sh | |
# getfinfo - Get file information formatted | |
# Copyright (C) 2014 Matous J. Fialka, <http://mjf.cz/> | |
# Released under the terms of The MIT License | |
# Variable GETFINFO_FORMAT can be used to c output | |
# formatting from generic to one of: XML, JSON or shell. | |
export LC_ALL=C | |
object="$1" | |
if [ ! -e "$object" ] | |
then | |
echo 'Error: No such object `'"$object'" 1>&2 | |
exit 1 | |
fi | |
if [ ! -r "$object" ] | |
then | |
echo 'Warning: Insufficient permissions on object `'"$object'" 1>&2 | |
fi | |
read attr rest <<- EOT | |
`lsattr "$object" 2>/dev/null` | |
EOT | |
read md5sum rest <<- EOT | |
`md5sum "$object" 2>/dev/null` | |
EOT | |
read sha1sum rest <<- EOT | |
`sha1sum "$object" 2>/dev/null` | |
EOT | |
stat_format=" | |
type='%F' | |
size=%s | |
target='%N' | |
link='%N' | |
user='%U' | |
group='%G' | |
uid=%u | |
gid=%g | |
perm='%A' | |
mode=%a | |
context='%C' | |
atime='%x' | |
atime='%X' | |
mtime='%y' | |
mtime='%Y' | |
ctime='%z' | |
ctime='%Z' | |
" | |
eval `stat "$object" --printf="$stat_format"` | |
type="${type:-?}" | |
target="${target#*-> }" target="${target:-?}" | |
size=${size:-?} | |
user="${user:-?}" | |
group="${group:-?}" | |
uid=${uid:-?} | |
gid=${gid:-?} | |
perm="${perm:-?}" | |
mode=${mode:-?} | |
attr="${attr:-?}" | |
context="${context:-?}" | |
atime=${atime:-?} | |
mtime=${mtime:-?} | |
ctime=${ctime:-?} | |
md5sum="${md5sum:-?}" | |
sha1sum="${sha1sum:-?}" | |
format_generic() | |
{ | |
cat <<- EOT | |
object $object | |
type $type | |
target $target | |
size $size | |
user $user | |
group $group | |
uid $uid | |
gid $gid | |
perm $perm | |
mode $mode | |
attr $attr | |
context $context | |
atime $atime | |
mtime $mtime | |
ctime $ctime | |
md5sum $md5sum | |
sha1sum $sha1sum | |
EOT | |
} | |
format_xml() | |
{ | |
cat <<- EOT | |
<object>$object</object> | |
<type>$type</type> | |
<target>$target</target> | |
<size>$size</size> | |
<user>$user</user> | |
<group>$group</group> | |
<uid>$uid</uid> | |
<gid>$gid</gid> | |
<perm>$perm</perm> | |
<mode>$mode</mode> | |
<attr>$attr</attr> | |
<context>$context</context> | |
<atime>$atime</atime> | |
<mtime>$mtime</mtime> | |
<ctime>$ctime</ctime> | |
<md5sum>$md5sum</md5sum> | |
<sha1sum>$sha1sum</sha1sum> | |
EOT | |
} | |
format_json() | |
{ | |
cat <<- EOT | |
"object":"$object", | |
"type":"$type", | |
"target":"$target", | |
"size":$size, | |
"user":"$user", | |
"group":"$group", | |
"uid":$uid, | |
"gid":$gid, | |
"perm":"$perm", | |
"mode":$mode, | |
"attr":"$attr", | |
"context":"$context", | |
"atime":$atime, | |
"mtime":$mtime, | |
"ctime":$ctime, | |
"md5sum":"$md5sum", | |
"sha1sum":"$sha1sum" | |
EOT | |
} | |
format_shell() | |
{ | |
cat <<- EOT | |
object='$object' | |
type='$type' | |
target='$target' | |
size=$size | |
user='$user' | |
group='$group' | |
uid=$uid | |
gid=$gid | |
perm='$perm' | |
mode=$mode | |
attr='$attr' | |
context='$context' | |
atime=$atime | |
mtime=$mtime | |
ctime=$ctime | |
md5sum='$md5sum' | |
sha1sum='$sha1sum' | |
EOT | |
} | |
case "${GETFINFO_FORMAT:-generic}" in | |
[gG][eE][nN][eE][rR][iI][cC]) | |
format_generic | |
;; | |
[xX][mM][lL]) | |
format_xml | |
;; | |
[jJ][sS][oO][nN]) | |
format_json | |
;; | |
[sS][hH][eE][lL][lL]) | |
format_shell | |
;; | |
*) echo 'Error: No such format `'"$GETFINFO_FORMAT'" 1>&2 | |
exit 1 | |
esac | |
# vi:ft=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
Overview
For all of the following example runs file
/var/log/messages
taken as an example object.Default output format is "generic". To switch to one of:
set the
GETFINFO_FORMAT
environment variable, for example:Output Format Examples
For file
/var/log/messages
and run asroot
to get all values.Generic
XML
JSON
Shell
TODO
setfinfo
for batch setting file information in given formateditfinfo
for editing file information using$EDITOR
in given format and setting it