You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
1.9 KiB
86 lines
1.9 KiB
2 years ago
|
#!/usr/bin/env bash
|
||
2 years ago
|
log_file=/var/log/mdlspipe.log
|
||
2 years ago
|
keys_of_interest=("kMDItemFSCreationDate" "kMDItemFSName")
|
||
2 years ago
|
md5_bin=$(which md5sum)
|
||
|
mktemp_bin=$(which mktemp)
|
||
2 years ago
|
DateTimeStamp=$(date +\%D_\%T)
|
||
|
|
||
2 years ago
|
IFS=$'\n'
|
||
|
|
||
2 years ago
|
set -a
|
||
|
source .env 2>&1 > /dev/null
|
||
|
set +a
|
||
|
|
||
|
## Functions
|
||
|
############
|
||
|
|
||
|
write_log () {
|
||
2 years ago
|
## Use on systems managed with systemd
|
||
2 years ago
|
# printf 'mdlspipe: %s\n' "$1" | systemd-cat
|
||
|
printf '%s mdlspipe: %s\n' "$DateTimeStamp" "$1" >> $log_file
|
||
2 years ago
|
}
|
||
|
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
mdls_per_file () {
|
||
|
file=$1
|
||
2 years ago
|
unset mdls_properties
|
||
2 years ago
|
for line in $(mdls $file); do
|
||
|
old_IFS=$IFS
|
||
|
unset IFS
|
||
|
mdls_key=$(printf "%s" $line | cut -d '=' -f 1)
|
||
|
mdls_value=$(printf "%s" $line | cut -d '=' -f 2)
|
||
|
if [[ " ${keys_of_interest[*]} " =~ " ${mdls_key} " ]];then
|
||
|
IFS=$old_IFS
|
||
2 years ago
|
mdls_properties+=( $mdls_value )
|
||
2 years ago
|
unset IFS
|
||
|
else
|
||
2 years ago
|
: # "Did not print!"
|
||
2 years ago
|
fi
|
||
|
IFS=$old_IFS
|
||
2 years ago
|
done
|
||
2 years ago
|
IFS=',';echo "${mdls_properties[*]}";IFS=$old_IFS
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
write_per_file () {
|
||
2 years ago
|
# "=== Write data to a .csv file. ===
|
||
2 years ago
|
#===================================="
|
||
|
# cat $temp_csv_file
|
||
|
# write_log "$temp_csv_file written successfully."
|
||
2 years ago
|
printf 'File: %s\nHash: %s\nMDLS Properties:\t%s\n' $1 $2 $3
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
cleanup () {
|
||
2 years ago
|
write_log "Cleanup completed successfully."
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
## Main
|
||
|
#######
|
||
|
|
||
2 years ago
|
trap cleanup EXIT
|
||
|
|
||
2 years ago
|
## Create temp file
|
||
|
temp_csv_file=$($mktemp_bin)
|
||
|
## Initialize column names
|
||
|
csv_columns=( "File Path" "File Hash" )
|
||
|
## Populate Columns from keys of interest
|
||
|
for key in ${keys_of_interest[@]};do
|
||
2 years ago
|
csv_columns+=( $key )
|
||
2 years ago
|
done
|
||
|
## Print columns to first line of csv
|
||
2 years ago
|
# "======= CSV Column Headers ========"
|
||
2 years ago
|
{ IFS=',';echo "${csv_columns[*]}";IFS=$'\n'
|
||
2 years ago
|
## Loop through files, writing to csv
|
||
2 years ago
|
for file in $(find $Working_Fullpath -maxdepth 1 -type f); do
|
||
2 years ago
|
## Set our hash value
|
||
2 years ago
|
hash=$($md5_bin $file | awk '{ print $4 }')
|
||
2 years ago
|
# Print csv row output
|
||
|
# "============= CSV Row =========="
|
||
2 years ago
|
echo "$file,$hash,$(mdls_per_file $file)"
|
||
2 years ago
|
# write_per_file $file $hash $mdls_properties
|
||
2 years ago
|
done
|
||
2 years ago
|
} >> $temp_csv_file
|
||
2 years ago
|
unset IFS
|
||
2 years ago
|
mv $temp_csv_file $PWD/mdlspipe_$(date +%\D_\%T|sed 's/[:|\/]/-/g').csv
|