
Send in your Unix questions today! |
See additional Unix tips and tricks
Many people who use PuTTY to make secure connections between Windows desktops and Unix servers are unaware that PuTTY also provides a tool for securely moving files between the two platforms. The reason for this oversight is that, while PuTTY provides a terminal-like window on its users desktops, the pscp command (PuTTY's scp command) that PuTTY provides for moving files has to be run from the command line.
If you use PuTTY on a Windows box to connect to your Unix systems, here's what you have to do to use the pscp command. First, you need to open a DOS command prompt. On some versions of Windows, you will find an option for the command prompt in your start menu. On others, the easiest way to get a command prompt is to select Start -> run and then type "cmd" in the text field.
With a DOS prompt window available, you can then issue the pscp command on the command line. If you've customized your Windows environment, adding PuTTY's install directory to your search path, you can just type a command like this:
pscp C:\xfer\myfile.txt user@myserver:/home/user
If you haven't altered your default search path, you can cd to the directory in which pscp is stored (probably C:\Program Files\PuTTY) and issue the command from there.
As you can see, pscp syntax is scp syntax. This command would copy the myfile.txt file from the C: drive to the home directory of "user" on myserver. The "user@" portion of the command allows you to specify the identity of the Unix user whose credentials will be used to accomplish the transfer. The system will prompt you for user's password and will then transfer the file, providing the user has proper permissions for the destination directory.
PuTTY is usually installed in C:\Program Files\PuTTY. To add this directory to your search path on a temporary basis, you can issue this command:
set PATH=C:\Program Files\putty;%PATH%
The %PATH% variable is analogous to $PATH on Unix systems and is appended to the end of this command so that the previously existing contents of the PATH variable are not lost.
To make the change to your path permanent, you would right click on "My Computer" => Properties => Advanced => Environment Variables. At that point, you can make changes to the Path variable.
Of course, you can also use the pscp command to copy files from a Unix system to your Windows box. As with scp, you simply reverse the order in which you specify the files:
pscp user@myserver:/home/user/myfile.txt C:\xfer
The pscp command also has the same options as scp. One of the more useful of these is the recursive option that allows you to copy a group of files with one command. You can also use wildcards. If you have a directory named images and another named more_images that you want to copy from your Windows box to your Unix server, a command like this would recreate both directories complete with their contents on the target system:
pscp -r C:\xfer\*images root@myserver:/opt/apache
After being prompted once for the root password, you would see the contents of both directories being uploaded.
While I'm no Windows geek, I've learned just enough about batch files to automate some repetitive tasks and moving files on a routine basis is a great candidate for automation. I use batch files like the one shown below to download files from one system and upload them to another.
@ECHO OFF
:: Copies files from old to new system
::
:: erase previous files
erase C:\old\*
:: download files to laptop
pscp shs@old:/export/home/shs/2copy/* C:\old
:: upload to new system
pscp C:\old\* shs@new:/home/shs
goto end
:end
Unlike scripts on Unix systems, batch files need to be have the .bat file extension and are invoked by typing the filename without its extension. For example the batch file "movefiles.bat" would be invoked by typing "movefiles". As with the PuTTY command, putting the pathname of your batch files directory into your patch will make it a lot less troublesome to call your scripts.