Hardlinking script on Linux

Information and Tutorials on features in Mylar and how to use it
Post Reply
Gauntlet
Posts: 6
Joined: Sun Dec 28, 2014 10:40 pm

Hardlinking script on Linux

Post by Gauntlet » 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

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
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

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
EDIT: Fixed some errors in the script! :oops:

User avatar
evilhero
Site Admin
Posts: 2002
Joined: Sat Apr 20, 2013 3:43 pm
Contact:

Re: Hardlinking script on Linux

Post by evilhero » Thu Oct 13, 2016 2:10 pm

This is nice!

I have a python script that does hardlinking, but while it was working for linux - it was failing miserably on windows mainly due to how the architecture is different between the OS'.

I do plan on implementing a hardlinking option, and it's a shame this in bash - but it's also a good short script for me to go through to see the steps you've taken to get it working against your current filesystem and maybe I can use that workflow when I redesign the python hardlinking script!

Post Reply