Recursive search for files and copy to new directories

So I have extremely limited experience with shell scripting and I was hoping someone could point out a few commands I need to use in order to pull this off with a shell script like BASH or whatnot (this is on OS X).

I need to search out for filenames with account numbers in the name itself (potentially more than one file with an account number plus other characters in the name), and copy them to a temporary folder, either in their own subfolders (named after the account number), or all in the same place. Basically I'm just trying to extract an copy some PDF files out of one location and put them in another. The search of the source location should be a recursive search. The filenames are basically "textstring12345689textstring.pdf"

I had this working at one point a while back using PHP, though it would sometime crap out on me after a larger number of runs through the loop. Regardless, I'd ideally like to just do this in a shell script anyway as the PHP script is pretty slow (it's searching through like a million files in a bunch of folders). Here is basically what I did in PHP:

<?php

$handle = @fopen('/Users/me/www/nexus/media/encore1.csv', "r");
mkdir ("/Users/me/www/temp");

if ($handle) {
	while (!feof($handle)) {
		$lines[] = fgets($handle);
	}
	fclose($handle);
}

for ($a=0; $a<count($lines); $a++)
{
	$cardnumber = trim($lines[$a]);
	print "\"" . $cardnumber . "\"<br>";
	$searchStr = "find . -name \"*" . $cardnumber . "*\"";
	
	print $searchStr . "<br>";    // just watching the progress
	
	$tempRoot = "/Users/me/www/temp/";
	$tempFolder = $tempRoot . $cardnumber;
	
	if (strlen($cardnumber) >= 16) {
		exec($searchStr, $output, $return);
	
		mkdir($tempFolder);
		
		foreach ( $output as $file) {
	
			$destinationFile = $tempFolder . "/" . basename($file);
			copy($file, $destinationFile);
			print basename($file) . " copied to " . $destinationFile . "<br>";   // again progress check
			
		}
	} else {
		print "Invalid Account Number ";
	}
	unset($output); 
}
?>

Pardon the lack of indentation in that code, it doesn't pass through when pasting in it.

The CSV file is just a single column list of account numbers. I'm sure this can be easily done with some kind of search and copy and make directory commands in a shell script, but I'm not really familiar with any.

Would anyone one have some pointers on which commands that are counterparts to the PHP ones above?

Put code inside [CODE] tags, not

[quote]
tags.

(You can edit your post to fix it.)

1 Like

In POSIX shell, like bash or ksh you can do something like this:

 
for n in $(cat your_acct_list); do 
        echo "Processing $n"; 
        find . -name "*${n}*" -type f | xargs -i ... {} target_here;    
done

The for construct will loop through the lines in the list, I put echo for you to see.

Put your copy/mv command here in xargs. The {} construct will be substituted with the actual names find command did find
You can run it without piping to xargs and see what exactly will happen, which files are found by find command.

What does the "-i" do with xargs?