Bash Script to move Files

Ben Stringer ben at burbong.com
Sat Oct 8 04:48:48 UTC 2005


On Sat, 2005-10-08 at 00:19 -0400, Thom Paine wrote:
> I don't know how to write scripts, but I've been trying to figure this
> out on my own.
> 
> I took an example I had of a temp file deleter and tried to make it
> work to a file mover.
> 
> The example I was starting with was:
> 
> find -name *.tmp -exec rm -rf {} \;
> 
> And I tried to get it to seek out all my *.ogg files and dump them
> into a media folder.
> 
> To prevent it from moving files repeatedly, I changed to the directory
> I wanted to search and moved it to a different directory on another
> mount.
> 
> I can't get it to work though. I thought I could substitute rm -rf
> with mv /media/oggs but that didn't seem to work.

The problem here is that find passes the files as an argument to the "rm
-rf" command. Ie. "rm -rf file1 path/file2 file3" but in your situation,
the mv command will be "mv /media/oggs file1 path/file2 file3" which is
the reverse order of the arguments, to do what you want. To fix this,
you could write a second, simple script to reverse the arguments. Eg.
mover.sh

-- cut here --
#!/bin/bash

mv $* /media/oggs
-- cut here --

Then, set the permissions for mover.sh to "execute" (chmod u+x mover.sh)
and use mover.sh as the command called from your find.

Whilst you are experimenting, you may want to use "cp" rather than "mv",
as an error in a call to "mv" can easily result in lost data. The most
common is if the directory does not exist, the find/mv combination will
happily move every ogg you have to a file of the name "/media/offs",
eahc overwriting the last, until the only ogg you have left is the last
one copied :)

Cheers, Ben





More information about the fedora-list mailing list