0

I'm try to copy files from a location (/home/ppaa/workspace/partial/medium) to another location (/home/ppaa/workspace/complete) using bash shell scripting in Linux.

This is my code:

#!/bin/bash -u

MY_BASE_FOLDER='/home/ppaa/workspace/'
MY_TARGET_FOLDER='/home/ppaa/workspace/complete/'
cp $MY_BASE_FOLDER'partial/medium/*.*' $MY_TARGET_FOLDER
return=$?
echo "return: $return"

The folders exists and the files are copied but the value of return variable is 1. Whats wrong?

5
  • cp returns error code 0 on success and 1 on failure. Commented Sep 15, 2016 at 17:44
  • I know, but the copy is successful. Commented Sep 15, 2016 at 17:49
  • I'm inclined to guess that the copy was not (completely) successful. cp will exit with non-zero status if it is unable to copy any of the specified files. That could happen for any number of reasons, but one reasonably likely one is that your source glob matches one or more directories. Non-recursive cp will not copy directories, and will exit with status 1 if asked to do so. It will still copy the files it can, however. Commented Sep 15, 2016 at 17:49
  • agree with above. And just to confirm an unmentioned bit of information ... Are there any error messages reported i.e. cp: none-such : not found. OR are you redirecting std-err to /dev/null someplace you are not showing us? i.e. .... 2> /dev/null . Good luck. Commented Sep 15, 2016 at 17:52
  • Tried running "cp --verbose" ? Commented Sep 15, 2016 at 18:10

1 Answer 1

2

The files are not copied. cp is most likely giving you an error like:

cp: cannot stat ‘/home/ppaa/workspace/partial/medium/*.*’: No such file or directory

This is because globs (like *.*) are not expanded in quotes. Instead, use:

cp "$MY_BASE_FOLDER/partial/medium"/*.* "$MY_TARGET_FOLDER"
Sign up to request clarification or add additional context in comments.

1 Comment

I would like to add that if you are your setting variables existence checks before execution (i.e check x first ${x:?}) do not use quote at all. Like this cp ${x:?} ${y:?}. Putting quotes around both x and y will cause the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.