Skip to content

Instantly share code, notes, and snippets.

@alexhrescale
Last active June 26, 2020 02:07
Show Gist options
  • Save alexhrescale/a1936269fe826c11759852ed981ddec5 to your computer and use it in GitHub Desktop.
Save alexhrescale/a1936269fe826c11759852ed981ddec5 to your computer and use it in GitHub Desktop.
get directory content information
#!/bin/bash
if [ $# -lt 1 ]; then
cat <<EOF
Usage: $0 <directory> [tabwidth]
This script outputs a table of file information from the contents of <directory> like:
time type perm uid size mime hash path target
4444 file -rw- 0 1111 text/... abcd /x/f
5555 exec -rwx 100 2222 app./x-. a135 /x/e
5566 dir drwx 100 inode/d. /x/d
5577 sym lrwx 0 inode/s. /x/l /x/ltarg
5599 bad l... 101 inode/s. /x/z /x/btarg
Setting [tabwidth] modifies tab spacing for easier reading in the console
EOF
fi
# e.g. bin boot etc home lib lib64 local opt root sbin srv usr var
BASEDIR="$*"
if [ $# -gt 1 ]; then
TABWIDTH=$2 # try 42
out_command="expand -t $TABWIDTH"
else
out_command=cat
fi
printf "time|type|perm|uid|size|mime|hash|path|target\n" | $out_command
find $BASEDIR -print0 |
xargs -0 -P $(( $(nproc)/2 )) \
-i sh -c '
_mtime=$(stat -c %Y "{}");
_mime=$(file -bn --mime-type "{}");
if [ -L "{}" ]; then
if [ -e "{}" ]; then
if [ -x "{}" ]; then
_type=symx;
else _type=symf;
fi;
else
_type=bad;
fi;
printf "%d|%s|%s|%s||%s||%s|%s\n" $_mtime $_type $(stat -c "%A %u" "{}") $_mime "{}" $(readlink "{}");
elif [ -d "{}" ]; then
printf "%d|%s|%s|%s||%s||%s\n" $_mtime dir $(stat -c "%A %u" "{}") $_mime "{}";
else
printf "%d|%s|%s|%s|%d|%s|%s|%s|\n" $_mtime $(if [[ -x "{}" ]]; then echo exec; else echo file; fi) $(stat -c "%A %u %s" "{}") $_mime $(sha1sum "{}"|cut -d" " -f1) "{}";
fi' |
$out_command
function process_stream() {
cat | grep x-executable | cut -d $'\t' -f 6 | xargs -r -P $(($(nproc)/2)) -i sh -c 'echo -e "\"{}\":\n$(ldd "{}"|sed ""s/^[[:space:]]/-" "/"")"'
}
if [ -t 0 ]; then
if [ $# -lt 1 ]; then
echo "Usage: $0 <directory>; outputs in YAML format"
exit
fi
BASEDIR=$1
dircontentinfo $BASEDIR | process_stream
else
cat | process_stream
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment