It will be helpful to you if you understand how and when the shell expands the * wildcard.
When you create a command line such as:
the shell replaces the *
with all possible path components that exist under the directory /path
and that also have a directory or file named stuff
. This is done before the shell actually runs the command. So if the following existed:
the shell would expand the * into only the first three items (marked in blue), but not the fourth item (marked in red). The command the shell would actually execute would be:
ls /path/1/stuff /path/2/stuff/anything /path/3/stuff/morestuff
command sees 3 command line arguments. It never sees the * that you typed.
loop in jschiwal's response:
for dir in /home/susee/databkp/A/*/C/
works in exactly the same way; the shell expands the * wildcard, so (assuming the sub-directories named XXX, YYY, and ZZZ) the command is expanded (globbed) to become:
for dir in /home/susee/databkp/A/XXX/C/ /home/susee/databkp/A/YYY/C/ /home/susee/databkp/A/ZZZ/C/
Again, the for
loop never sees the * wildcard.
The problem with your original find command is that you were providing the cp
command with too many destination directories. Your command looked essentially like this:
cp source_files targetDir1 targetDir2 ... targetDirN
only supports copying to a single target directory. To copy to multiple target directories, you must run the cp
command multiple times, each time copying to one of your target directories.
This is where the loop comes in handy. You copy many source files into one directory, and do it again to the next, etc.
Now, your job is to explain what your source paths look like and what your target directories look like. This will help you see the pattern required to create a loop. The code provided to you assumes a simple pattern of names ranging from 1 to 4.