bash script for copying files between directories -
i writing following script copy *.nzb files folder queue them download.
i wrote following script
#!/bin/bash #this script copies nzb files downloads folder hellanzb queue folder. ${down}="/home/user/downloads/" ${queue}="/home/user/.hellanzb/nzb/daemon.queue/" in $(find ${down} -name *.nzb) cp ${a} ${queue} rm *.nzb done
it gives me following error saying:
hellanzb.sh: line 5: =/home/user/downloads/: no such file or directory hellanzb.sh: line 6: =/home/user/.hellanzb/nzb/daemon.queue/: no such file or directory
thing directories exsist, have right access them.
any nice.
please , thank you.
variable names on left side of assignment should bare.
foo="something" echo "$foo"
here more improvements script:
#!/bin/bash #this script copies nzb files downloads folder hellanzb queue folder. down="/home/myusuf3/downloads/" queue="/home/myusuf3/.hellanzb/nzb/daemon.queue/" find "${down}" -name "*.nzb" | while read -r file mv "${file}" "${queue}" done
using while
instead of for
, quoting variables contain filenames protects against filenames contain spaces being interpreted more 1 filename. removing rm
keeps repeatedly producing errors , failing copy first file. file glob -name
needs quoted. habitually using lowercase variable names reduces chances of name collisions shell variables.
if files in 1 directory (and not in multiple subdirectories) whole script reduced following, way:
mv /home/myusuf3/downloads/*.nzb /home/myusuf3/.hellanzb/nzb/daemon.queue/
if have files in multiple subdirectories:
find /home/myusuf3/downloads/ -name "*.nzb" -exec mv {} /home/myusuf3/.hellanzb/nzb/daemon.queue/ +
as can see, there's no need loop.
Comments
Post a Comment