Error copying files in Linux shell bash script - linux

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?

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"

Related

How to execute from any directory Bash script that sources other Bash scripts (not using path variable)

I am not trying to execute a Bash script from any directory by adding the script to my Path variable.
I want to be able to execute the script from any directory using the directory path to that file ... but the file I want to execute sources other files, that is the problem.
If I am in directory file with two scripts myFunctions.sh and sourceFunctions.sh
sourceFunctions.sh
#!/bin/bash
source ./myFunctions.sh
echoFoo
myFunctions.sh
function echoFoo()
{
echo "foo"
}
I can run myFunctions.sh and foo will print to console, but If I go up a directory and run myFunctions.sh I get error
cd ..
file/sourceFunctions.sh
-bash: doFoo.sh: command not found
Unless I changed source file/myFunctions.sh to source file/myFunctions.sh in sourceFunctions.sh.
So how can I source independent of my working directory so I can run sourceFunctions.sh from any working directory I want?
Thanks
You have the right idea. Doesn't need to be that complicated though:
source `dirname $0`/myFunctions.sh
I often compute "HERE" at the top of my script:
HERE=`dirname $0`
and then use it as needed in my script:
source $HERE/myFunctions.sh
One thing to be careful about is that $HERE will often be a relative path. In fact, it will be whatever path you actually used to run the script, or "." if you provided no path. So if you "cd" within your script, $HERE will no longer be valid. If this is a problem, there's a way (can't think of it off hand) to make sure $HERE is always an absolute path.
I ended up just using a variable of the directory path to the script itself for the source directory
so
#!/bin/bash
source ./myFunctions.sh
echoFoo
becomes
#!/bin/bash
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
source ${SCRIPTPATH}/myFunctions.sh
echoFoo
source

cp command from tcl script

Hi I am writing a tcl script to automate the task in linux. In that I want to copy files.
The command is
cp -r source destination. I have tried using
puts [cp -rf source destination]. But I am getting error saying invalid command cp. How will I write it in tcl script.
To run an external program from your script, you should use the exec command:
puts [exec cp -r $source $destination]
However, for the specific case of copying a directory from one place to another you can use the built-in file copy command (which works with directories as well as files):
file copy $source $destination

shell script mv is throwing unhelpful error "No such file or directory" even though i see it

I need to use a shell script to move all files in a directory into another directory. I manually did this without a problem and now scripting it is giving me an error on the mv command.
Inside the directory I want to move files out of are 2 directories, php and php.tmp. The error I get is cd: /path/to/working/directory/php: No such file or directory. I'm confused because it is there to begin with and listed when I ls the working directory.
The error I get is here:
ls $PWD #ensure the files are there
mv $PWD/* /company/home/directory
ls /company/home/directory #ensure the files are moved
When I use ls $PWD I see the directories I want to move but the error afterward says it doesn't exist. Then when I ssh to the machine this is running on I see the files were moved correctly.
If it matters the directory I am moving files from is owned by a different user but the shell is executing as root.
I don't understand why I would get this error so, any help would be great.
Add a / after the path to specify you want to move the file, not rename the directory.
You should try this:
mv $PWD/\* /home/user/directory/
Are your variables properly quoted? You could try :
ls "$PWD" #ensure the files are there
mv "$PWD"/* "/company/home/directory"
ls "/company/home/directory" #ensure the files are moved
If any of your file or directory names contains characters such as spaces or tabs, your "mv" command may not be seeing the argument list you think it is seeing.

Recursively copy contents of directory to all target directories

I have a directory containing a set of subdirectories and files. I need to recursively copy all the content of this directory to all the subdirectories of another directory, also recursively.
How do I achieve this, preferably without using a script and only with the cp command?
You can write this in a script but you don't have to. Just write it line by line in the terminal:
# $TARGET is the directory containing subdirectories where you want to STORE the copies
# $SOURCE is the directory containing the subdirectories you want to COPY
for dir in $(ls $TARGET); do
cp -r $SOURCE/* $TARGET/$dir
done
Only uses cp and runs on both bash and zsh.
You can't. cp can copy multiple sources but will only copy to a single destination. You need to arrange to invoke cp multiple times - once per destination - for what you want to do; using, as you say, a loop or some other tool.
The first part of the command before the pipe instruct tar to create an archive of everything in the current directory and write it to standard output (the – in place of a file-name frequently indicates stdout).
tar cf - * | ( cd /target; tar xfp -)
The commands within parentheses cause the shell to change directory to the target directory and untar data from standard input. Since the cd and tar commands are contained within parentheses, their actions are performed together.
The -p option in the tar extraction command directs tar to preserve permission and ownership information, if possible given the user executing the command. If you are running the command as superuser, this option is turned on by default and can be omitted.
Also you can use the following command, but it seems to be quite slower than tar;
cp -a * /target

cp command simply not copying

I am working on a shell script and for some reason when I say
cp full_path/* full_path_directory/
I get an error. I have echoed out the command and when I run what it echos in an interactive shell it works. I can't figure out why it won't work in a shell script. I'm using full paths rather than absolute. I have tried to putting a slash at the end of the destination directory and then not putting a slash...what else could it be?
Error:
cp: /opt/local/apache2/htdocs/baseline/*: No such file or directory
So when I echo it out I get:
/opt/local/apache2/htdocs/baseline/* /opt/local/apache2/htdocs/test/
It means what it says. There are no files in /opt/local/apache2/htdocs/baseline/ directory, or you don't have permissions to read the directory. What does ls show you?

Resources