Script that moves itself

Hello all, I'm new here, so if this is in the wrong place please feel free to move it. :slight_smile:

As a challenge to myself, I've decided I want to make a "alive" script. Well not really, but almost.

I want to make a script that will rove around my hard drive in the background (actually "moving" itself), and record the files it sees. Sorta like a mini mars rover... in my computer.

I'm on Mac OS 10.4.4, so I'm on UNIX/Darwin underneath, and my general question is:

Can I make a script that can actually move itself? I can see the obvious difficulties; "Item in use, permission denied!" etc...

I'd like to be able to do it without using another script, but somehow I think I already know the answer, but please tell me I'm wrong. :slight_smile:

Have you considered the "cd" command?

If you want to use your model, then maybe something like this...

..............
cat $0 > /new/path/myfile.sh
chmod +x /new/path/myfile.sh
/new/path/myfile.sh
exit

Never finishes ...

Perhaps something like that :

..............
target_file=/new/path/myfile.sh
if [ $0 != $target_file ]
then
   cp $0 $target_file
   chmod +x $target_file
   $target_file
fi
exit