OT: Bash script help needed
Craig Puchta
craig at craigpuchta.net
Thu Dec 20 17:17:03 UTC 2007
On Fri, 2007-12-21 at 02:06 +1100, Owen Townend wrote:
> Hey,
> A little script like this would do what you want assuming the naming
> conventions as above.
>
> Go through it and make sure you understand what each part is doing and
> edit it for your needs before you try running it.
>
> If you need to get more complex with the base names and paths look
> into 'sed' and 'awk' instead of 'cut'.
> http://www.grymoire.com/Unix/Sed.html is a great sed tut.
>
> #!/bin/sh
> #
> # Sort logs into folders.
> # example log name "rdb-001-001.txt"
> #
>
> WORKINGDIR="/path/to/files"
> BASEPATH="/path/to/new/dir"
>
> for i in `ls $WORKINGDIR`; do
> basename=`echo $i | cut -c -7`
> # test for an existing directory
> if [ ! -d ${BASEPATH}/${basename} ]
> # create the new dir if needed.
> then mkdir -p ${BASEPATH}/${basename}
> fi
>
> # now move the file(s)
> mv ${WORKINGDIR}/${basename}* ${BASEPATH}/${basename}/
>
> done
>
>
> Let me know what you think.
> This will need to be added to if your files aren't all in the same dir
> at the moment or if there are spaces or strange characters in the names.
>
> cheers,
> Owen.
I changed:
WORKINGDIR="/path/to/files"
BASEPATH="/path/to/new/dir"
to:
WORKINGDIR="/home/eldergod/ziso"
BASEPATH="/home/eldergod/ziso"
I kept getting "mv: cannot move `/home/eldergod/ziso/rdb-030' to a
subdirectory of itself, `/home/eldergod/ziso/rdb-030/rdb-030'
Although it seems to have worked. I also tried it with:
WORKINGDIR="/home/eldergod/ziso"
BASEPATH="/home/eldergod/ztest"
and mv said it couldn't stat the directory, but it also seemed to have
worked.
This script more or less confuses me, I guess I need a good "bash
scripting for dummies" book :)
I'll keep messing with it and thanks for the help, I'm going to try the
script that Nils sent now.
More information about the ubuntu-users
mailing list