Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created March 24, 2017 07:46
Show Gist options
  • Save jsfaint/445d12344add8710d201512a313b3fa2 to your computer and use it in GitHub Desktop.
Save jsfaint/445d12344add8710d201512a313b3fa2 to your computer and use it in GitHub Desktop.
Get type and RPM owner of files under a given path.
#!/bin/bash
# -----------------------------------------------
#
# rpm_file.sh
#
# Description: Get type and RPM owner of files under a given path.
#
# usage:
# ./rpm_file.sh [PATH]
# param:
# PATH: Path for mounted yorkville image.
# if PATH=/, we consider that it check for the running system. It's
# different with other path.
#
# For loging purpose, the output should be redirected to a file, like this:
# ./rpm_file.sh /mnt/yorkville/ > yorkville.txt
#
# -----------------------------------------------
# note:
# author: [email protected]
# -----------------------------------------------
ignore_list="dev proc sys tmp lost+found"
if [ x$1 == x"" ]; then
echo -e " Usage: $0 [path]"
echo -e " Path can be /, for check the running OS.\n"
echo " Example: $0 /mnt/yorkville/"
exit 2
else
DIR=$1
fi
# get file type
# input: $1 file name
# output: $type
get_type() {
local file="$1"
if [ -b "$file" ]; then
type="Block Dev"
elif [ -c "$file" ]; then
type="Char Dev"
elif [ -h "$file" ]; then
type="Symbol Link"
elif [ -p "$file" ]; then
type="Pipe file"
elif [ -S "$file" ]; then
type="Socket File"
elif [ -f "$file" ]; then
tmp=`ls -l "$file" | awk '{print $2}'`
if [[ $tmp -eq 1 ]]; then
type="Generic File"
else
type="Hard Link"
fi
fi
}
# get file rpm owner
# input:
# $1: file name
# output:
# $rpm_own
get_owner() {
local file="$1"
if [[ x"$DIR" == x"/" ]]; then
rpm_own=`rpm -qf "$file"`
else
rpm_own=`chroot $DIR rpm -qf "${file/$DIR//}"`
fi
}
# output the result.
# input:
# $1: file name
# $2: file type
# $3: rpm owner
output() {
local file="$1"
local file_type="$2"
local rpm_own="$3"
echo "$file"
echo -e " $file_type\t\t$rpm_own"
echo ""
}
for_each() {
for file in "$1"/*
do
type=""
#skip the folders listed in ignore_list.
tmp=`basename "$file"`
for i in $ignore_list
do
flag=0
if [[ x"$tmp" == x"$i" ]]; then
flag=1
break;
fi
done
if [[ flag -eq 1 ]]; then
continue
fi
if [[ -d "$file" ]]; then
if [[ -d "$file" ]]; then
type="Directory"
fi
get_owner "$file"
output "$file" "$type" "$rpm_own"
for_each "$file"
else
tmp=`basename "$file"`
if [ x"$tmp" == x"*" ]; then
continue
fi
get_type "$file"
get_owner "$file"
output "$file" "$type" "$rpm_own"
fi
done
}
for_each $DIR
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment