Hardlinking script on Linux
Posted: Mon Sep 12, 2016 8:41 pm
I like to hardlink my comic files so that I can continue seeding but also have them organised. Mylar does not do this yet so I wrote a simple script.
Requirements:
incrontab -this service will launch the script when it detects changes to the comics folder, I followed a tutorial (http://www.cyberciti.biz/faq/linux-inot ... rectories/) to set it up.
Basically just apt-get install incron.
I added root and my user to /etc/incron.allow.
Then used incrontab -e to launch comiclinker.sh script when files are moved into it
The following script finds all cbz and cbr files at the path passed to it by incrontab then hardlinks them to the specified destination folder
comiclinker.sh
EDIT: Fixed some errors in the script!
Requirements:
incrontab -this service will launch the script when it detects changes to the comics folder, I followed a tutorial (http://www.cyberciti.biz/faq/linux-inot ... rectories/) to set it up.
Basically just apt-get install incron.
I added root and my user to /etc/incron.allow.
Then used incrontab -e to launch comiclinker.sh script when files are moved into it
Code: Select all
"directory to monitor" FLAGS "path to script / command" OPTIONS
path/to/where/comics/are/moved/to/after/downloading IN_MOVED_TO /path/to/script/comiclinker.sh $@ $# /path/to/directory/mylar/will/check/for/new/comics /path/to/log/file/comiclinker.log
comiclinker.sh
Code: Select all
#! /bin/bash
dirin=$1
filein=$2
dirout=$3
logfile=$4
maxlogsize=1000;
logsize=$(wc -c <"$logfile")
if [[ $logsize -ge $maxlogsize ]]; then
rm $logfile;
fi
date_time=`date "+%Y-%m-%d %T"`;
echo -e "\n"$date_time $filein >> $logfile
fullpath="$dirin/$filein"
echo -e $date_time $fullpath >> $logfile
files=()
if [[ -d $fullpath ]]; then
echo -e $date_time "DIR" $filein >> $logfile;
while IFS= read -r -d $'\0'; do
number_of_links=`stat -c "%h" "$REPLY"`
echo -e $date_time "$REPLY" "Number of Links: $number_of_links" >> $logfile
if [[ 1 == $number_of_links ]]; then
files+=("$REPLY")
fi
done < <(find "$fullpath"/* -type f -regex ".*/.*\.\(cbr\|cbz\|zip\|rar\)" -print0)
elif [[ -f "$fullpath" ]]; then
echo -e $date_time "FILE" $filein >> $logfile
files+=("$fullpath")
fi
echo -e $date_time "Number of Files: ${#files[@]}" >> $logfile
for (( i = 0; i < "${#files[@]}"; i++ ))
do
file="${files[$i]}"
filename=$(basename "${file}")
echo -e $date_time $i $file $filename >> $logfile
ln "$file" "$dirout/$filename"
done