Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active August 29, 2015 13:56
Show Gist options
  • Save mjf/8974107 to your computer and use it in GitHub Desktop.
Save mjf/8974107 to your computer and use it in GitHub Desktop.
getfinfo - Get file information formatted
#! /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
@mjf
Copy link
Author

mjf commented Feb 13, 2014

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:

  • XML
  • JSON
  • shell

set the GETFINFO_FORMAT environment variable, for example:

$ GETFINFO_FORMAT=json getfinfo /var/log/messages

Output Format Examples

For file /var/log/messages and run as root to get all values.

Generic

object /var/log/messages
type regular file
target /var/log/messages
size 22500
user root
group root
uid 0
gid 0
perm -rw-------
mode 600
attr -------------e--
context system_u:object_r:var_log_t:s0
atime 1392518164
mtime 1392724099
ctime 1392724099
md5sum bd41656399724659530434b32f4a351f
sha1sum ad94c9d7dceaab156e41fc4ab66ccd364a8ea7f2

XML

<object>/var/log/messages</object>
<type>regular file</type>
<target>/var/log/messages</target>
<size>22500</size>
<user>root</user>
<group>root</group>
<uid>0</uid>
<gid>0</gid>
<perm>-rw-------</perm>
<mode>600</mode>
<attr>-------------e--</attr>
<context>system_u:object_r:var_log_t:s0</context>
<atime>1392518164</atime>
<mtime>1392724099</mtime>
<ctime>1392724099</ctime>
<md5sum>bd41656399724659530434b32f4a351f</md5sum>
<sha1sum>ad94c9d7dceaab156e41fc4ab66ccd364a8ea7f2</sha1sum>

JSON

"object":"/var/log/messages",
"type":"regular file",
"target":"/var/log/messages",
"size":22500,
"user":"root",
"group":"root",
"uid":0,
"gid":0,
"perm":"-rw-------",
"mode":600,
"attr":"-------------e--",
"context":"system_u:object_r:var_log_t:s0",
"atime":1392518164,
"mtime":1392724099,
"ctime":1392724099,
"md5sum":"bd41656399724659530434b32f4a351f",
"sha1sum":"ad94c9d7dceaab156e41fc4ab66ccd364a8ea7f2"

Shell

object='/var/log/messages'
type='regular file'
target='/var/log/messages'
size=22500
user='root'
group='root'
uid=0
gid=0
perm='-rw-------'
mode=600
attr='-------------e--'
context='system_u:object_r:var_log_t:s0'
atime=1392518164
mtime=1392724099
ctime=1392724099
md5sum='bd41656399724659530434b32f4a351f'
sha1sum='ad94c9d7dceaab156e41fc4ab66ccd364a8ea7f2'

TODO

  • POSIX.1e ACL support
  • script setfinfo for batch setting file information in given format
  • script editfinfo for editing file information using $EDITOR in given format and setting it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment