How to copy all of the files from one directory to another in a bash script

bash, linux

Solution

Expanding on devnull's comment:

Quotes of any kind around a wildcard, like `*`, will prevent the shell from expanding the wildcard. Thus, you should only write `"/*"` if you want a slash followed by a literal star.

An unquoted variable will be subject to word splitting. So, if pck_dir had the value `my dir`, then `$pck_dir"/*"` would be expanded to two words `my` and `dir/*` and both words would be passed to `cp` as separate arguments. Unless you want word splitting, shell variables should always be in double quotes.

Thus, to get what you want, use:

cp -r "$pck_dir"/* "$TAR_DIR/pck/"

Problem

I want to copy all of the files from a subdirectory into another directory without copying the original folder. In a terminal I would just do this: ``` cp -r dir1/* dir2 ``` and then dir2 will contain all of the files from dir1 without containing dir1 itself. I am trying to replicate this in a bash script and I am getting an error. Here is my bash code: ``` cp -r $pck_dir"/*" $TAR_DIR"/pck/" ``` I get this error: ``` cp: cannot stat ‘./mailman/lists/mailman/*’: No such file or directory ``` This is strange because I can verify that the directory in question exists. I believe bash is complaining about the '*' but I am not sure why. Can someone enlighten me as to what I am doing wrong?

Original source