Copy files from a local to remote folder with scp and script throws "No such file or directory" - bash

Description
I want to copy all files ending on .jpg from my local machine to the remote machine with scp.
For this i have a small "script". It looks like this:
#!/bin/bash
xfce4-terminal -e "scp -r -v /path/to/local/folder/*.jpg <user>#<IP>:/var/path/to/remote/folder/" --hold
Problem
When i open a terminal and enter scp -r -v /path/to/local/folder/*.jpg <user>#<IP>:/var/path/to/remote/directory/ it works.
So SSH is working correct.
When i start the script it doesnt.
The script works, when i copy the whole local folder. It then looks like this (simply the *.jpg is removed):
#!/bin/bash
xfce4-terminal -e "scp -r -v /path/to/local/folder/ <user>#<IP>:/var/path/to/remote/folder/" --hold
But then i have the local folder inside the remote folder, where i only want to have the files.
I dont know, if it is important but currently i use a computer with Linux Mint 19.3, xfce terminal and zshell.
Question
So how do i run a script correctly that copys files from a local folder to remote folder?

It's the shell who expands the wildcard, but when you run -e in xfce4-terminal, it runs the command without letting the shell parse it. You can run a shell to run the command, though:
xfce4-terminal -e "bash -c 'scp -r -v /path/to/local/folder/*.jpg user#ip:/var/path/to/remote'" --hold
Are you sure you need the -r? Directories are usually not named .jpg.

Related

Executing Commands from Windows on Unix via Batchfile

I have a problem that cannot find a solution for quite a while.
I want to execute following line from a Batch file on my windows machine:
ssh %1#%2 "D: && ssh %3#%4 cd /media/usbmsd/ && cp "$(ls -t /media/usbmsd | head -1)" /buffer"
THis batch file will be later executed from a cmd line with the parameters. I am trying to access one system (windows) via ssh and that hop again via ssh to the another system(unix) and there I need to find the newest file in the /media/usbmsd directory and copy it to the folder buffer.
When I excetuting it from my cmd line i am getting following error:
'head' is not recognized as an internal or external command,
operable program or batch file.
I have to say that I am not very experienced with this kind of application and am happy about any help
Greeting Denis
You could connect via a proxy jump, then you don't need a second ssh command.
Then a bit obscure escaping of the quotes is necessary.
(I tested this without the intermediate windows client)
The first caret ^ is necessary to quote from the cp command, the backslashes are necessary to convince the first expansion to leave the quotes untouched.
ssh %3#%4 -o ProxyJump %1#%2 ^"cd /media/usbmsd/ && cp \"$(ls -t /media/usbmsd | head -1)\" /buffer'
But it should be much easier to place a script on the destination host mycopyscript.sh
cd /media/usbmsd/ && cp "$(ls -t /media/usbmsd | head -1)" /buffer
And then use:
ssh %3#%4 -o ProxyJump %1#%2 "./mycopyscript.sh"

i have a part of code that works in git bash but not when i make a script out of it

Hello awesome community i am using git bash to run this command: cp -r thisfolder thatfolder to copy the contents of a folder in another one
now i want to make a script out of this to create a scheduler to run that script and my script looks like this
#!/bin/bash
set -euo pipefail
IFS=$'\n\t
cp -r thisfolder thatfolder && echo done > debug.txt
#the debug.txt is just to know if the script copied stuff
its a .bat script and all folders and script are on the same directory but for some reason it doesnt copy the files as git bash does
any thoughts??

How to make a bash script for mac?

I'm trying to make this bash script but get this: Error reading *.docx. The file doesn’t exist
Here's the script:
#!/bin/bash
textutil -convert txt *.docx
cat *.txt | wc -w
I'm currently running it from the folder but I'd like to make it a global script I can just call from any current folder.
If you want to make it available on your whole system you need to move it to a bin location like so
chmod a+rx yourscript.sh && sudo mv yourscript.sh /usr/local/bin/yourscript
then you can use it like a normal script in any folder

How to rename all files over SSH

I am trying to rename all files in a remote directory over SSH or SFTP. The rename should convert the file into a date extension, for example .txt into .txt.2016-05-25.
I have the following command to loop each .txt file and try to rename, but am getting an error:
ssh $user#$server "for FILENAME in $srcFolder/*.txt; do mv $FILENAME $FILENAME.$DATE; done"
The error I am getting is:
mv: missing destination file operand after `.20160525_1336'
I have also tried this over SFTP with no such luck. Any help would be appreciated!
You need to escape (or single-quote) the $ of variables in the remote shell. It's also recommended to quote variables that represent file paths:
ssh $user#$server "for FILENAME in '$srcFolder'/*.txt; do mv \"\$FILENAME\" \"\$FILENAME.$DATE\"; done"
Try this:
By using rename (perl tool):
ssh user#host /bin/sh <<<$'
rename \047use POSIX;s/$/strftime(".%F",localtime())/e\047 "'"$srcFolder\"/*.txt"
To prepare/validate your command line, replace ssh...bin/sh by cat:
cat <<<$'
rename \047use POSIX;s/$/strftime(".%F",localtime())/e\047 "'"$srcFolder\"/*.txt"
will render something like:
rename 'use POSIX;s/$/strftime(".%F",localtime())/e' "/tmp/test dir"/*.txt
And you could localy try (ensuring $srcFolder contain a path to a local test folder):
/bin/sh <<<$'
rename \047use POSIX;s/$/strftime(".%F",localtime())/e\047 "'"$srcFolder\"/*.txt"
Copy of your own syntax:
ssh $user#$server /bin/sh <<<'for FILENAME in "'"$srcFolder"'"/*.txt; do
mv "$FILENAME" "$FILENAME.'$DATE'";
done'
Again, you could locally test your inline script:
sh <<<'for FILENAME in "'"$srcFolder"'"/*.txt; do
mv "$FILENAME" "$FILENAME.'$DATE'";
done'
or preview by replacing sh by cat.
When using/sending variables over SSH, you need to be careful what is a local variable and which is a remote variable. Remote variables must be escaped; otherwise they will be interpreted locally versus remotely as you intended. Other characters also need to be escaped such as backticks. The example below should point you in the right direction:
Incorrect
user#host1:/home:> ssh user#host2 "var=`hostname`; echo \$var"
host1
Correct
user#host1:/home:> ssh user#host2 "var=\`hostname\`; echo \$var"
host2

Bash script: Attempting to upload datestamped file via SFTP

I'm attempting to write a bash script that I can set as a cronjob to automatically upload a backup file via SFTP to a remote server.
The backup files on the local server are datestamped (e.g. backup-file-YYYY-mm-dd.tar.gz) and I'd like the script to only upload a file from the directory that has the same datestamp as the current date.
Any ideas on where I'm going wrong? I can't help but think I'm missing something basic but I can't think what it is!
Current broken script below:
#!/bin/bash
FILE=$backups/$(date+%Y-%m-%d).tar.gz *<<<<< I'm guessing this is where it's slipping up*
sshpass -p "remoteserverpassword" sftp -o StrictHostKeyChecking=no <user>#<remoteserverip)
cd /directory1/directory2/
put $FILE
exit 0
EOF
You are right about where it is slipping up, date needs to be eval'd prior to passing to here script. Reformatted for clarity but you could plug into the original script too.
#!/bin/bash
backup=/tmp
today=`date +%Y-%m-%d`
FILE=$backup/$today.tar.gz
sshpass -p "remoteserverpassword" sftp -o StrictHostKeyChecking=no <user>#<remoteserverip) <<EOF
cd /directory1/directory2/
put $FILE
exit 0
EOF

Resources