FAO bash scripting gurus...

Associate
Joined
5 May 2006
Posts
1,251
Location
York
Ello
If anyone could help me with this it'd be much appreciated. I know my way around linux and the shell but am pretty rubbish when it comes to actual scripts.
I need a script that will look through every subdirectory in its parent and any files it finds inside said subdirectories it needs to match with a file in the parent with a different file extension, and then copy into the respective subdirectory.
Every file has a match as far as I'm aware. Any that don't can just be ignored.
I started doing it by hand but I have in excess of 2000 files to go through and I value my time :D

I realise it's a bit cheeky to ask this for free on the internet, but if anyone can take the time to throw one together you'd have my thanks :)
 
I could probably help you with this if you explain what you are trying to do a bit more clearly. The explanation doesn't fully make sense to me as it is...

Cheers
 
Last edited:
As far as I could tell, op has something like this:

dir1
dir2
dir3
file0.xxx
file1.xxx
file2.xxx
file3.xxx
file4.xxx
file5.xxx

Then in the directories randomly (from the sounds of things)
file0.yyy
file1.yyy
file2.yyy
ect

And he needs to match file0.xxx to whichever directory contains file0.yyy and move xxx into the directory.

Guess the easiest way to do it, would be foreach file in the "parent" dir, recursive find of cut -1 (think the first field is 1, to get the file name, or any other string manipulation). Then you would need to do a regular expression match to get the subdirectory and run mv/cp command into that directory.

Or something like that.
 
This should work for you, it's a Perl script. Be sure to change the $baseDirectory value appropriately


Code:
my %baseFiles;
my %baseFilesFull;
my %baseDirs;

my $baseDirectory = "/home/dangerstat/test";
opendir(my $directory, $baseDirectory) or die "Can't open the base directory\n";
while(my $file = readdir($directory)){
    next if $file =~ /^\./;
    if(-d $file){
	print "Found directory = $file\n";
	$baseDirs{$file}=1; 
    }else{
	print "Found File = $file\n";
	#Extract name 
	if($file =~ /(.+?)\./){
	    $baseFiles{$1} = 1;
	    $baseFilesFull{$1} = $file;
	}
    }
}


foreach $dir (keys %baseDirs){
    opendir(my $newDir, "$baseDirectory/$dir") or die "Couldn't open sub directory\n";

    while(my $f = readdir($newDir)){
	next if $f =~ /^\./;
	next if -d $f;
	if($f =~ /(.+?)\./){
	    if($baseFiles{$1} ==1){
	     print "Copying file: $baseDirectory/$baseFilesFull{$1} to $baseDirectory/$dir\n";
	     `cp $baseDirectory/$baseFilesFull{$1} $baseDirectory/$dir/.`;
	    }
	}
    }
}
 
I could probably help you with this if you explain what you are trying to do a bit more clearly. The explanation doesn't fully make sense to me as it is...

Cheers

Sorry. I'm a bit rubbish at explaining stuff sometimes :o

dangerstat that is exactly what I was after and it's doing its stuff as we speak. Thanks very much!
 
Liking the perl, as I'm rubbish at perl I would have dumped the contents of a find . to a file and the contents of the top level dir to another file and then grepped out the top level file names from the other file, then used some awk on the path to move it there, the above is way better tho and mine is a really rubbish explaination!
 
Back
Top Bottom