In Cygwin I can execute commands like this:
ls -l d:/folder
mkdir d:/folder/anotherfolder
But for some reason scp doesn't want to work like this:
$ scp -r username#ipaddr:d:/remote/folder d:/folder
ssh: Could not resolve hostname d: Name or service not known
But it works like this:
$ scp -r username#ipaddr:d:/remote/folder /cygdrive/d/folder
Any other command works with paths like "d:/folder", only scp don't.
Does anyone know why?
/cygdrive/d/folder is the default POSIX way. All cygwin programs recognize it.
So use it as your default.
d:/folder is an old compatilbility trick, but most of the current Cygwin programs do NOT recognize it.
It is usually implemented as Cygwin specific patch of upstream source.
Related
Is there any option to tell scp command - not copy file from current machine in case file exists on remote machine
For example
On my machine I have the file -
/etc/secret-pw.txt
On Remote machine I have also the file -
/etc/secret-pw.txt
So
scp /etc/secret-pw.txt $remote_machine:/etc
Will destroy the secret-pw.txt, and scp will not ask questions about: overwrite the target file
Is there any option to avoid copy if file exist on target machine by scp?
Update: I can't install rsync or any other program.
You should be using rsync instead of scp. It will give you what you need.
If you can't install rsync (as you mentioned in the comments) you need to run a script beforehand to check if file exists and run it with ssh.
SCP does not offer any option, unfortunately.
But you can resort standard tools, like this:
ssh $remote_machine -- cp --no-clobber /dev/stdin /etc/secret-pw.txt < /etc/secret-pw.txt
Note that with this trick you gain all the functionalities of cp.
My cygwin used to work properly before, but suddenly it starts to complain: "file does not exist" under /cygdrive/d ...
The problem looks like this: if I cd to that folder I can saw that file and cat/ls afile without any problem, but if I use something like ls /cygdrive/d/afile it will complain "file does not exist".
So I traced backwards on the file system, and found there's some issue with the virtual path /cygdrive
/cygdrive
cd /cygdrive/
Error: Current working directory is a virtual Cygwin directory which
does not exist for a native Windows application. Can't start native
Windows application from here.
Seems this /cygdrive is broken, does anyone know how to fix it?
Adding more detail:
$grep cygdrive /etc/fstab
# none /cygdrive cygdrive binary,posix=0,user 0 0
$type -a cd
#cd is a shell builtin
I get that error message if I do a cd /cygdrive and then try to execute a (non-Cygwin) Windows command. I don't get it if I just cd /cygdrive.
The only explanation I can think of is that you've assigned a value to the bash special variable $PROMPT_COMMAND.
Quoting the bash documentation:
'PROMPT_COMMAND'
If set, the value is interpreted as a command to execute before
the printing of each primary prompt ('$PS1').
For example:
$ PROMPT_COMMAND=cmd.exe ; cd /cygdrive
Error: Current working directory is a virtual Cygwin directory which does
not exist for a native Windows application.
Can't start native Windows application from here.
-bash: /cygdrive/c/Windows/system32/cmd.exe: Not a directory
move to the right directory by replacing (if you are in C drive for example)
cd /cygdrive
by
cd c:/cygdrive
also suitable:
cd /c/<rest of your path>
I tried running a script file using bash but it showed an error
bash-3.2$ example.sh : command not found
I also tried
ls -l example.sh
I found that it was not executable, so I used
sudo chmod 777 example.sh
I again tried running it but same error was coming. I double checked that I am in the same folder as the file using ls. But still I am not able to execute the script file.
I finally tried making a dummy script file and running it , and found the same error
I think there is some problem with BASH. Can some one help me with what is the problem?
I am working on redhat, bash was already installed in my system
Since I am newbie on linux any help would be appreciated
bash search for commands in your $PATH. Apparently the current directory, ., is not in your $PATH. (This is a good thing; having . in your $PATH is insecure.)
You'll need to specify a directory name. Just type:
./example.sh
Incidentally, doing:
sudo chmod 777 example.sh
is two kinds of overkill. First, you don't need to use sudo; use sudo only when you actually need to. Presumably your personal account owns the file, so you can just use chmod directly.
Second, 777 is way too permissive. It allows anyone on the system to read, execute, or modify example.sh. (If you're the only person on the system it may not matter much, but it's still a bad habit.) Typically you should use 755 for directories and for files that need to be executable, and 644 for files that don't need to be executable.
Or just use
chmod +x example.sh
to set execute permission (your umask will prevent that from setting the permissions too loosely).
. (the current directory) is probably not on your path. Try ./example.sh or bash example.sh. You could also add . to your PATH environment variable, but that's generally frowned upon.
Your bash PATH probably doesn't include ., try running it by typing:
./example.sh
When you type a command, your shell searches your path to try to find the command, if the current directory (e.g. .) isn't part of the path, the script that you are trying to run won't be found. You'd have to explicitly give it the path to where this command is. And since it's in your current directory, you can just add ./ in front of the command.
first confirm the bash path
to check the path of bash use:
which bash
if you get "/bin/bash"
then add
#!/bin/bash
...
...
or whatever is the path on first line of your bash script
I have tried put -r directory/*, which only uploaded the files and not folders. Gave me the error, cannot Couldn't canonicalise.
Any help would be greatly appreciated.
For people actually wanting a direct answer to this question (instead of being told to use something other than sftp)...
put -r local/path/to/directoryName
The uploaded directory must already exist in the working directory on the server, so you might need to create it first.
mkdir directoryName
Here you can find detailed explanation as how to copy a directory using scp. In your case, it would be something like:
$ scp -r foo your_username#remotehost.edu:/some/remote/directory/bar
This will copy the directory "foo" from the local host to a remote host's directory "bar".
Here -r is -recursively copy entire directories.
You can also use rcp with similar syntax. The only difference between them is that scp uses secure shell and rcp uses remote shell.
BTW The "Couldn't canonicalise" error you mentioned appear when sftp server is unable to access the file/directory mentioned in the command.
UPDATE: For users who want to use put specifically, please refer to Ben Thielker answer here.
sftp> mkdir source
sftp> put -r source
Uploading source/ to /home/myself/source
Entering source/
source/file1
source/file2
if you have issues using sftp, you can use ncftp
For centos
yum install ncftp
To copy a whole directory recursively
ncftpput -R -v -u username -P 21 ftp.server.dev /remote-path/ /localdirectory
Use scp instead. It uses SSH too and can easily handle recursion.
I need to do a script to extract a tar at a specified location.
I did something simple like:
cp test.tar /var/www/html
cd /var/www/html
tar xvf test.tar
If I execute the commands by hand everything is OK. If I save them in a .sh then use #bash script.sh, I get the following error ": Not a directory cd: /var/www/html". Any ideea why?
Ty for your time.
Notes: I tried the script version on a virtual machine (CentOS 5.5) and the script worked fine, the problem occurs on the real machine where I want to use it (I used same OS disk image, same configurations as on the virtual machine... this makes it really really odd for me).
Added: Also I try invoking something like service mysqld start... this also fails saying that a dir doesn't exist (still if I run by hand it works.).
I solved the problem - it is quite interesting).
I created the script on a virtual machine running on windows with a centos os, the enter in windows is "\r\n" while in linux is "\n".
The script worked on the vm because the code for enter was correct, while on the second computer, with native linux it was incorrect. I created exactly the same script on linux and everything went back 2 normal ;).
Note... the mkdir part worked because I used another, simplified script written on linux.
On a related note, I have found that the "~" character does not seem to work in bash, so if you are using that, try replacing it with the full path.
It looks like your cp might be coping test.jar to the file html under the www directory. Make sure that html exists and is a directory before you try to cp.
mkdir -p /var/www/html
cp test.tar /var/www/html
cd /var/www/html
tar xvf test.tar