Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bash recursive collection script

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help!
View previous topic :: View next topic  
Author Message
Lancer
LXF regular


Joined: Thu Apr 07, 2005 12:59 pm
Posts: 232

PostPosted: Sat Jun 25, 2005 1:10 am    Post subject: Bash recursive collection script Reply with quote

Probably this can be done with a simple cp command and options...

I have a school fileserver with each student having their own account in /home as shown here...

/home/judy
/home/larry
/home/greg
/home/karen
(etc)

Students are in three class group (junior, middle and senior known as $GROUP1 $GROUP2 and $GROUP3) although I don't really want to add that complication to my script right now except to say that the wanted scripts COULD be formed along the lines of...

for STUDENT in $GROUP3 do { ..... }

...ignore that if you want (it could just do all students in /home dir), as it's a side issue. Just something you can use if you like. Here's the real question:

Students in the SENIOR group (for example) have made a lot of pov ray modeler files (which end in the *.kpm prefix). I want to make a colection.sh script which will go through the senior /home dirs and copy all files in those directories into a new one at /home/collection, RENAMING each file as tom1.kpm tom2.kpm tom3.kpm tom4.kpm and so forth, depending on their username. What this means is that I will have a "collected works" (/home/collection) directory where I can quickly sort through student files to look at the items of the file type I am wanting.

The script must not of course also reproduce stuff from the /home/collection directory itself
The script should be recursive just in case students decided to organise their work into subdirs

Preferably, the script would be executed along the lines of

./colection.sh GROUP3 *.kpm

...to collect all the student files in the list $GROUP3 which end in .kpm

It's the sort of thing I love to nut out myself (for my own learning) although I'm kind of pressured for time at the moment, so your help is appreciated.
Back to top
View user's profile Send private message
Guest






PostPosted: Sat Jun 25, 2005 6:47 am    Post subject: RE: Bash recursive collection script Reply with quote

OK I got you setup but I did it in Perl...

Code:
#!/usr/bin/perl

$returnval = system('find /home | grep -v "collection" | grep -i "\.kpm" > /tmp/xyz');
$infile = '/tmp/xyz';
open(INFILE, $infile);
while (<INFILE>){
        if ($_ =~ m/\/home\/(\w+).*\/(\w+\.[kK][pP][mM])$/)
        {
                $fullpath = $_;
                $username = $1;
                $filename = $2;
                chomp($fullpath);
                chomp($username);
                chomp($filename);
                #print "VALUES: fullpath $fullpath username $username filename $filename\n";
                $args = '/home/collection/' . $username . '_' . $filename;
                #print "COMMAND " . "mv $fullpath $args\n";
                system("mv $fullpath $args");
        }
}


Paul Doherty
http://powerusers.info
Power Tools for Power Users
Back to top
ped



Joined: Sat Jun 25, 2005 6:53 am
Posts: 3

PostPosted: Mon Jun 27, 2005 9:33 pm    Post subject: Reply with quote

Man, for being "pressured for time" you seem to be taking your time getting back to this thread... Rolling Eyes
_________________
Paul Doherty
http://powerusers.info
Power Tools for Power Users
Back to top
View user's profile Send private message
Rhakios
Moderator


Joined: Thu Apr 07, 2005 12:18 am
Posts: 7632
Location: Midlands, UK

PostPosted: Mon Jun 27, 2005 10:35 pm    Post subject: Reply with quote

Obviously so pressured for time that he hasn't had sufficient, yet Wink
_________________
Bye, Rhakios
Back to top
View user's profile Send private message
Lancer
LXF regular


Joined: Thu Apr 07, 2005 12:59 pm
Posts: 232

PostPosted: Tue Jun 28, 2005 5:58 am    Post subject: Reply with quote

Actually, the reason I didn't get back yet was because the perl solution somewhat confused me, as I do not know perl (It does not look harder than other languages, but the /\/\/\/\/\ syntax throws me a bit, which is why I specified BASH). I thereby was in no position to modify the perl script for a few other things I wanted to do, but did not want critique a solution which, if I only right now had the time to study perl, was in all likelyhood a perfectly viable solution (hence lack of a reply). Instead used the following script to get the job done...

Code:

#!/bin/bash
DIRNAME=sximpress

FILELIST='
./kaitb/slideshow.sxi
./miko/my_robot.sxi
./jessf/slideshow.sxi
./alexd/slideshow.sxi
./marp/TUX.sxi
./maxk/slideshow.sxi
./andyw/slideshow.sxi
./chanp/slidshow.sxi
./student/slideshow_yasmin.sxi
./needp/slideshow.sxi
./hang/trees.sxi
./harryg/slideshow.sxi
./chrisp/spt.sxi
./chrisp/slide show.impress.sxi
./troyh/slide show.sxi
./josg/slideshow.sxi
./olivr/slideshow.sxi
./ryanb/slideshow.sxi
./ryanb/myperson.sxi
./harh/slideshow.sxi
./juliah/slideshow.sxi
./morgb/slideshow.sxi
./genec/slideshow.sxi
./zain/slideshow.sxi
./camb/SLIDESHOW.sxi
./levih/levi and .sxi
./jakabc/Happy little monster.sxi
./alexh/slindshow.sxi
./maxt/slideshow.sxi
'

# ----------------- Create new collection and copy objects.
mkdir $DIRNAME
for i in $FILELIST; do
mkdir -p $DIRNAME/$i
rmdir $DIRNAME/$i
cp -rf /home/$i $DIRNAME/$i
done


...where $DIRNAME was given the name of the target "collection" directory for the wanted file type and the $FILELIST were results of a manual [/b]find[/b] for the file type I was wanting to collect in the /home/dirs.

Not an ideal solution, as I had to manually edit my code each run for every file type based on my manual find query. Also, the inability of my own code to handle file names with spaces meant I had to manually read through screeds of "file not found" errors each run and manually mget the misnamed files via ftp from the file server, one by one.

A damned lengthy process which took hours. I still haven't managed to mark the work I collected. Fortunately, the classes were cancelled due to outside courses etc or I would have certainly missed the deadlines altogether.
Back to top
View user's profile Send private message
skecs



Joined: Fri Apr 22, 2005 10:22 am
Posts: 76
Location: Bathurst, NSW Australia

PostPosted: Tue Jun 28, 2005 11:17 am    Post subject: Reply with quote

Lancer wrote:
the /\/\/\/\/\ syntax throws me a bit
- is quite understandable Laughing

The backslashes "\" are escape characters in regular expressions. They mean "use the ASCII character that follows". The special characters "/" (forward slashes) and "." (periods) are used to separate or group strings of characters but they also signify the directory structure "/home/username/filename.ext" or special matching characters like \w ("a-zA-Z0-9_" = alphanumeric characters & _) and \n (newline).

There are a number of online resources to help with Perl (when you get time Wink ) like Learn Perl and The CGI Site. I know LXF is doing some shell scripting but Perl is also very powerful and can also interact with Web Servers very well.
_________________
Regards from
Downunder!
.... _
... (0)>
... / /\
.. / / .)
.. V_/_
Linux Powered!
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help! All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast