Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (9.5k points)

To copy all files from a subdirectory into another directory without have to copy the original folder I do this:

cp -r dir1/* dir2

So dir2 has all the files of dir1 but it doesn’t contain the dir1. I want to replicate it in bash script. But I get an error when I run the below code: 

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

This is the error I got:

cp: cannot stat ‘./mailman/lists/mailman/*’: No such file or directory

Can anyone tell me how to copy all the files from a directory to another using bash script? 

1 Answer

0 votes
by (19.7k points)

  1. * will prevent the shell from expanding the wildcard. If you want a slash followed by literal star, you should write like this:  "/*" 

  2. Word splitting occurs with unquoted variables. For example, when pck_dir has dir, then $pck_dir"/*" split into two words as my and dir/*. If you want to avoid splitting , shell variables should only be in double quotes. 

See the below code implementation: 

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

Interested in Linux? Check out this Linux Certification by Intellipaat.   

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Apr 9, 2021 in Linux by sheela_singh (9.5k points)

Browse Categories

...