Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need some help to make a script to copy files from a local folder to a network drive.
First of all I want to tell you this is my first time in linux environment, so be kind and clear. tongue.gif
My setup: Raspberry Pi with Raspbian wheezy, with a local connected USB-drive and a network drive. I have mounted both drives so they are available from the pi.
/home/pi DOWNLOAD > completed (local USB-drive)
MOVIES > HD (Network Drive part.1)
TV > SERIES > X (Network Drive part.2)
Y (Network Drive part.2)
Z (Network Drive part.2)
I want a script that copy all specific files (.avi etc) from /home/pi/DOWNLOAD/completed which dosen't already exist on the destination and put those in the predetermined folder.
etc. /home/pi/DOWNLOAD/completed/xxxxxYxx.avi goes to /home/pi/TV/SERIES/Y
/home/pi/DOWNLOAD/completed/xxxxxXxx.avi goes to /home/pi/TV/SERIES/X
/home/pi/DOWNLOAD/completed/xxxxxZxx.avi goes to /home/pi/TV/SERIES/Z
/home/pi/DOWNLOAD/completed/xxxxxxxx.avi goes to /home/pi/MOVIES/HD
I've struggled with rsync for a few days now and I think I got a good structure but somehow nothing happens when I run the .sh-script and when I try to run the rsync command manually it just say it is a (dry-run) even though I don't have the parameter -n in the command.
here is my "script" ^^'
#log-file
LOG_FILE=/home/pi/TV/rsynclogg.log
#the header for log-file
echo '-----------------------------------------------------------------------------' >> $LOG_FILE
date >> $LOG_FILE
#!/bin/bash --
# Unset debug when done testing:
set -vxe
# Set default behaviour:
LANG=C; LC_ALL=C; export LANG LC_ALL
RSYNC_ARGS="--dry-run --checksum --itemize-changes --human-readable --stats --log-file=/home/pi/TV/rsync.log"
# Throw in some ways to nice things:
# which nice >/dev/null 2>&1 && nice -n +20 $$ >/dev/null 2>&1
# which ionice >/dev/null 2>&1 && ionice -c3 -p $$ >/dev/null 2>&1
# RSYNC_ARGS="$RSYNC_ARGS --bwlimit=1m"
for ITEM in X Y Z; do
_MYTMPFILE=`mktemp XXXXXXXXXX` && {
find /home/pi/DOWNLOAD/completed/ -type f -name \*${ITEM}\*.avi > "${_MYTMPFILE}"
rsync $RSYNC_ARGS --files-from="${_MYTMPFILE}" "/home/pi/TV/SERIES/${ITEM}/"
}; rm -f "${_MYTMPFILE}"
done
_MYTMPFILE=`mktemp XXXXXXXXXX` && {
find /home/pi/DOWNLOAD/completed/ -type f \( -not -name \*X\*.avi -a -not -name \*Y\*.avi -a -not -name \*Z\*.avi \)
rsync $RSYNC_ARGS --files-from="${_MYTMPFILE}" /home/pi/MOVIES/HD/
}; rm -f "${_MYTMPFILE}"
exit 0
See 'man rsync' for explanation of rsync options.
Remove the "--dry-run" when you think it looks OK.
Looks neat, would love to get that working.
Would you mind explain more? I don't really know what to replace to make it run, if you could highlight or something. ITEM, MYTMPFILE
I don't really know what to replace to make it run, if you could highlight or something. ITEM, MYTMPFILE
If you save the code as script, say "/usr/local/bin/rsync_movies.sh" you can run it as '/bin/bash /usr/local/bin/rsync_movies.sh'.
The output will show you what it does (how it works) but it will essentially move no files.
If you no longer want to see what it does comment out the "set -vxe" line (should look like "#set -vxe" then). You should still see output in "/home/pi/TV/rsync.log".
If you think it's what you want then you have to delete the "--dry-run" switch from the RSYNC_ARGS= line (looks like RSYNC_ARGS="--checksum --itemize-changes --human-readable --stats --log-file=/home/pi/TV/rsync.log" then).
#!/bin/bash
find /home/pi/DOWNLOAD -type f \( -not -iname '*sample*.mkv' -a -iname '*X*.mkv' \) -exec cp -i -n {} "/home/pi/TV/SERIES/X" \;
find /home/pi/DOWNLOAD -type f \( -not -iname '*sample*.mkv' -a -iname '*Y*.mkv' \) -exec cp -i -n {} "/home/pi/TV/SERIES/Y" \;
find /home/pi/DOWNLOAD -type f \( -not -iname '*sample*.mkv' -a -iname '*Z*.mkv' \) -exec cp -i -n {} "/home/pi/TV/SERIES/Z" \;
find /home/pi/DOWNLOAD -type f \( -not -iname '*sample*.mkv' -a -iname '*W*.mkv' \) -exec cp -i -n {} "/home/pi/TV/SERIES/W" \;
find /home/pi/DOWNLOAD -type f \( -not -iname '*sample*.mkv' -a -iname '*V*.mkv' \) -exec cp -i -n {} "/home/pi/TV/SERIES/V" \;
find /home/pi/DOWNLOAD -type f \( -name '*.mkv' -a -not -iname '*X*.mkv' -a -not -iname '*Y*.mkv' -a -not -iname '*Z*.mkv' -a -not -iname '*W*.mkv' -a -not -iname 'V*.mkv' -a -not -iname '*sample*.mkv' \) -exec cp -i -n {} "/home/pi/MOVIES/HD" \;
exit 0
as say "/usr/local/bin/move_movies.sh" and make it executable ('chmod 0755 /usr/local/bin/move_movies.sh'). Then if you execute '/usr/local/bin/move_movies.sh' (shouldn't need sudo if your home is /home/pi/ else use 'sudo /usr/local/bin/move_movies.sh') files should be moved according to your script. IIRC the curly braces need to be escaped BTW.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.