LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple move script (https://www.linuxquestions.org/questions/linux-newbie-8/simple-move-script-735075/)

ravedog 06-23-2009 03:31 PM

Simple move script
 
Hi all!

Im trying to setup a download box for myself and along the way learning about or two about linux.

So i have made a script (cover your eyes, its extremely advanced) that looks like this:

Code:

zenity --info --text="Copy of files initiated, please wait until done"
mv /home/user/downloaded/*DVD* /home/user/downloaded/*dvd* /media/misc1/temp/
zenity --info --text="Copy done!"

However, its quite dumb at the moment and pops a dialog box regardless of if there is files to move or not.

Anyone have an idea on how to make it "look" before trying to copy?

Thanks in advance!

(Thanks for a great forum as well!)

Ravedog

EDIT: Spell check :/

nuwen52 06-23-2009 03:44 PM

how about:
Code:

ls /home/user/downloaded/*DVD* >> /dev/null
if [ $? = 0 ]
then
    <do all your stuff>
else
    <display error>
fi

$? is the return code of whatever the previously executed command was.

ravedog 06-23-2009 03:46 PM

Thanks for a quick answer m8! Ill get right on testing it in the morning :)

ravedog 06-24-2009 02:39 PM

Worked like a charm! Thanks!

Follow up question, what is the syntax in the example given above if i like to search for several thing? I.e. *DVD* and *DVDrip* and if any match proceed.

Thanks guys!

colucix 06-24-2009 02:46 PM

If you want to perform an action on any file found, you can use a for loop like this:
Code:

for file in $(ls *DVD* *DVDrip* 2>/dev/null)
do
  echo "$file"
done

this simple example will do an echo of the filename. It will do nothing if no file is found. What exactly do you want to achieve if you find some *DVD* or *DVDrip* file?

ravedog 06-24-2009 02:53 PM

Hi colucix!

Thanks for the tip! I want to scan a download folder after several criteria and move them to the correct folder.

Simple put:

If DVD --> Folder a

If 1080 or 720 --> Folder c

If xvid -> Folder b

Cheers!

/R

colucix 06-24-2009 03:13 PM

You can try a case/esac construct:
Code:

for file in /home/user/downloaded/*
do
  case "$file" in
    *DVD*          ) mv "$file" folder_a ;;
    *xvid*        ) mv "$file" folder_b ;;
    *1080* | *720* ) mv "$file" folder_c ;;
    * ) echo "unknown file" ;;
  esac
done


ravedog 06-24-2009 03:15 PM

That looks really sweet, gonna try it out in the morning! thanks a bunch man! :)

Cheers!

/R


All times are GMT -5. The time now is 03:55 AM.