On Mon, 13 Nov 2000, DaZZa wrote:

> I know this is a little off topic, but I need some help.

Don't know how scripting is off topic.

> I need a script which will basically run through a directory, and copy all
> files which start with the same letter of the alphabet to a destination
> directory.
>
> For example, soemthing like this
>
> cp a*.doc /home/ftp/pub/doc/a/
>
> except I need it for the entire possible alphabet - AaBbCc etc.

Well, most things look like a perl script to me

---start script---
#!/usr/bin/perl

$path = shift @ARGV;
die "The path $path should exist." unless -d $path;

foreach $file (@ARGV) {
        ($fc) = $file =~ /^([A-Za-z])/;
        if ($fc) {
                `mkdir $path/$fc` unless -d "$path/$fc";
                `cp $file $path/$fc/$file`;
        }
}
---end script---

You execute it like this if you called the script movefiles

./movefiles /my/new/root/path *.html

The shell expands the *.html into a list of files. The script takes the
new path to move the files as the first argument. It then checks the first
character of the filename is an alpha and if so copies the file. So if you
have two files in the directory called 1JAN2000.html and another abc.html
it woud

cp abc.html /my/new/root/path/a/abc.html

It checks the new path exists and creates the new directories if they do
not exist.

It only works on files in the current directory if you wanted to copy
files out of another directory you would have to enhance the script.

Rodos



-- 
SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/
More Info: http://slug.org.au/lists/listinfo/slug

Reply via email to