Do you need to use BASH? What about Perl or Kornshell? The problem here is that Bash does not have hash key arrays (Kornshell and Perl do). This means that there is no easy way to track where files go. Imagine in Perl:
my %directoryB; #Hash that contains files to move to Directory B my %directoryC; #Hash that contains files to move to Directory C open (TEXT_FILE_B, "textFileB") or die qq(Can't open "textFileB"); while (my $line = <TEXT_FILE_B>) { chomp $line; $directoryB{$line} = 1; } close TEXT_FILE_B; open (TEXT_FILE_C, "textFileC") or die qq(Can't open "textFileC"); while (my $line = <TEXT_FILE_C>) { chomp $line; $directoryC{$line} = 1; } close TEXT_FILE_C;
The above lines create two hashes. One for files that need to be moved to directory B, and one for files that need to be moved to directory C. Now I only need to look at my hash and decide:
foreach my $file (@directory) {
My if statements can now see if a key has been defined in this hash. If so, I know that the file can be moved to this directory. I need to read two text files only once. After that, my two hashes will store files that move to one directory and the other.
However, we have no hashes, so we will use grep to find out if the file name is in the directory. I assume that you have one file name on each line.
ls | while read file do if grep -q "^${file}$" textFileB then mv $file $directoryB elif grep -q "^${file}$" textFileC then mv $file $directoryC fi done
grep -q will search your two text files to see if there is a corresponding file. If so, it will move the file to this directory. It is not very efficient, as it has to search the entire text file every time. However, it is quite effective, and you are only talking about 10,000 files, so the whole operation should only take a few minutes.
David W.
source share