Moving Files Automatically

I'm looking to find/create a script so that when a download is complete, I can run the script in order for it to automatically move a file such as...

'example.avi' into my videos folder

I'm a novice when it comes to scripting, any advice/help would be greatly appreciated!

Thanks
Andrew

What will the constants be? Will the file be moved from /Users/<yourUserName>/Downloads to /Users/<yourUserName>/Movies ?
Will it always be a .AVI file?

Constants will always be the directory. For example file downloaded to Downloads Folder, and be moved to Movies, or Music.

.avi, .mp4, or .mp3

and move to the according folder

Kind of a simple approach, assumes you know how to save a bash shell script and make it executable, and to run it.

#!/bin/bash
for i in ~/Downloads/*.avi
do
mv "$i" ~/Movies
done

for i in ~/Downloads/*.mp4
do
mv "$i" ~/Movies
done

for i in ~/Downloads/*.mp3
do
mv "$i" ~/Music
done

---------- Post updated at 05:53 PM ---------- Previous update was at 05:51 PM ----------

Basically, save the code to a text file on your desktop named Whatever.sh

Open Terminal and do: chmod +x ~/Desktop/Whatever.sh

To run it in Terminal: sh ~/Desktop/Whatever.sh

Thanks for the quick response! I really appreciate it.

It did in fact work, and I must say will save me a lot of silly time.

I am starting to read a book on shell scripting, so this is an awesome little start to it. I am familiar with C++ and Java, but am obviously new to this.

For educational purposes, would you mind explaining what "i' represents in the code?

Cheers!

---------- Post updated at 06:26 PM ---------- Previous update was at 06:08 PM ----------

Did the obvious editing necessary. One last question to make this script simply PERFECT! :smiley:

If I wanted to take a group of folders on my Desktop and move all the content of those folders to the desktop (in a non organized fashion) how would I do that? Or if you can respond with guidance... I'm more then happy to continue the learning process.

Thanks again!

#!/bin/bash
for i in ~/Desktop/*.avi
do
mv "$i" /Volumes/Drobo/Movies
done

#!/bin/bash
for i in ~/Desktop/*.mp4
do
mv "$i" /Volumes/Drobo/Movies
done

for i in ~/Desktop/*.mp3
do
mv "$i" /Volumes/Drobo/iTunes/iTunes Media/Automatically Add to iTunes.localized
done

echo "\nSCRIPT COMPLETE\n"
cd ~/Desktop
mv folder/* ./

Hmm... Didn't seem to work. I'll continue playing, and modifying.

Any other suggestions appreciated!

Ok well assuming you want to move the contents of every folder on your desktop to the actual desktop you could do:

cd ~/Desktop && ls -l |awk '$0 ~ /'^d'/ {print $9}'|while read dir;do cd "$dir" && mv * ~/Desktop && cd ~/Desktop ;done

That Should work, but test it first if you are able.

In the for loop, the i represents a variable that expands to the item found on each respective iteration of the loop, so basically for i in ~/Downloads means that $i represents each individual item in the Downloads folder, for each pass through the loop it goes down the list of every item in that folder. the i can be anything, it is just a variable name.

---------- Post updated 05-15-12 at 11:26 AM ---------- Previous update was 05-14-12 at 10:02 PM ----------

Actually that code will fail if there are spaces in the folder names on your desktop. Try this instead:

cd ~/Desktop && ls -l |awk '$0 ~ /'^d'/ {$1=$2=$3=$4=$5=$6=$7=$8="" ; print $0 }'|while read dir;do cd "$dir" && mv * ~/Desktop && cd ~/Desktop ;done
1 Like

My suggestion was very minimal because you gave very few details.

If it didn't work, in what way did it not work?

And please give more details.

Corona688,
I believe he wanted to loop through every directory on his desktop and move the contents of each directly to his desktop. Your solution would definitely work to move the contents of a folder named folder to the desktop, he must have typed the code exactly as you wrote it, not replacing the work folder for his own directory.

Wow Glev. Absolutely outstanding!

Corona thanks as well. After playing around with your code I was able to make your option work as well!

Thank you so much for the help. Any recommendations on books to learn shell scripting? My college doesn't offer classes on it :frowning:

Thanks again!