2

I've been trying to migrate small parts from my media/download center from windows to linux, before making the jump on my media center. Right now i have a Raspberry pi set up, with raspbian, where i will try out different things before moving.

Right now i've got my media center downloading certain files, that needs to be copied from one directory to another. That part i could do with the most basic linux commands, but the real problem lies in the handling of those files when copied.

in windows i've got a batch script running periodically, where it: -copies files from "a" to "b", and creates a log file. -a program monitoring folder "b" moves and renames the files -the log file prevents the script from copying the same files again and thus preventing duplicates in my media folder.

The code is as follows:

ROBOCOPY "location A" "Location B" /NP /M /S /LOG+:c:\batches\Rename.log

Is this possible to accomplish in linux and if so how?

6

rsync is great for this sort of thing, you won't need to do any renaming or anything, it will only copy the new or updated stuff. It has loads of options to change the behaviour to your requirements.

For example:

rsync -av /location/a/* /location/b
  • 1
    Also consider -R and --ignore-existing – jmunsch Dec 4 '14 at 22:47
  • I think to "only copy the new or updated stuff" you would need -u in the switches. however some sources imply this is default, but it's not completely clear whether this is true. might as well put it in... – underscore_d Oct 23 '15 at 21:35
0

In Linux you can do anything!

You can use crond to program a process to execute periodically and a bash script to copy your files.

Let's say you create two directories:

pi@raspberrypi2 ~ $ mkdir tmp
pi@raspberrypi2 ~ $ cd tmp/

pi@raspberrypi2 ~/tmp $ mkdir 1;mkdir 2

then put a file at directory 1:

pi@raspberrypi2 ~/tmp $ touch 1/file1

Then you create your script like:

ej. simple bash script:

#!/bin/bash

for i in $(ls 1/);do
    if [ -e 2/$i ];then
        echo "File already copied to directory 2" >> logfile.log
    else
        cp 1/$i 2/$i
        if [ $? = 0 ];then 
            echo "File $i copied to directory 2" >> logfile.log
        else 
            echo "Error copying file $i to directory 2" >> logfile.log
        fi
    fi
done

which copies files from directory 1 to directory 2. And also writes a message to a log file using the echo command.

test:

pi@raspberrypi2 ~/tmp $ ls 1/ 2/
1/:
file1

2/:
pi@raspberrypi2 ~/tmp $ bash script.sh
pi@raspberrypi2 ~/tmp $ ls 2/
file1
pi@raspberrypi2 ~/tmp $ cat logfile.log 
File file1 copied to directory 2
pi@raspberrypi2 ~/tmp $ bash script.sh 
pi@raspberrypi2 ~/tmp $ ls 2/
file1
pi@raspberrypi2 ~/tmp $ cat logfile.log 
File file1 copied to directory 2
File already copied to directory 2
pi@raspberrypi2 ~/tmp $ 

then you can insert a cronjob in crontab to run the script periodically:

pi@raspberrypi2 ~/tmp $ crontab -e

# run the script at at 5 a.m every day:
0 5 * * * bash /home/pi/tmp/script.sh

save the file and check that the cronjob is installed:

pi@raspberrypi2 ~/tmp $ crontab -l

0 5 * * 1 bash /home/pi/tmp/script.sh

just can the script as much a you like, that example is an starting point.

NOTE: tested in Raspbian runnin in Raspberry Pi A+ :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.