ChrisW91
ChrisW91

Reputation: 55

Bash - Moving files from subdirectories

I am relatively new to bash scripting.

I need to create a script that will loop through a series of directories, go into subdirectories with a certain name, and then move their file contents into a common folder for all of the files.

My code so far is this:

#!/bin/bash

#used to gather usable pdb files
mkdir -p usable_pdbFiles
#loop through directories in "pdb" folder
for pdbDirectory in */
do
    #go into usable_* directory
    for innerDirectory in usable_*/
    do
        if [ -d "$innerDirectory" ] ; then
            for file in *.ent
            do

            mv $file ../../usable_pdbFiles

            done < $file
        fi
    done < $innerDirectory
done
exit 0

Currently I get

    usable_Gather.sh: line 7: $innerDirectory: ambiguous redirect

when I try and run the script.

Any help would be appreciated!

Upvotes: 0

Views: 214

Answers (1)

mhawke
mhawke

Reputation: 87134

The redirections < $innerDirectory and < $file are invalid and this is causing the problem. You don't need to use a loop for this, you can instead rely on the shell's filename expansion and use mv directly:

mkdir -p usable_pdbFiles
mv */usable_*/*.ent usable_pdbFiles

Bear in mind that this solution, and the loop based one that you are working on, will overwrite files with the same name in the destination directory.

Upvotes: 1

Related Questions