Ubuntu: Copying files and folders with cp with shopt extglob - error is not a directory


I'm trying to copy files from a directory while excluding a folder and a file. My problem is that a get an error with shopt extglob activated. Here is my code:

DEPLOY_SIMULATION_ENV="/home/user/Desktop/fancyproject/"  shopt -s extglob  cp -r ./!(nbproject|zexp.html) "$DEPLOY_SIMULATION_ENV"  shopt -u extglob  

It gives me

‘/home/user/Desktop/myfancyproject/’ is not a directory  

Of course, I want cp to create the directory myfancyproject atuomatically. I have a workaround with mkdir $DEPLOY_SIMULATION_ENV first but is there a way to do it with cp? It behaves strangely when extglob is active.


Are you sure the problem is not the usage of "user" here...


If the account name is not "user" it will fail with the same error as you show. You need $USER if you want the actual user.

Is there a way to do it with cp

Yes... but not how you expect it.


Form the name of each destination file by appending to the target directory a slash and the specified name of the source file. The last argument given to cp must be the name of an existing directory. For example, the command:

      cp --parents dir1/dir2/file1 dir3  

will create /dir1/dir2 inside dir3 (needs to already exist) and copy file1 into it.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »