Jokes about “format C:” and “rm -rf /” are pretty frequent in the computer universe. These two commands will supposedely remove all data from the hard disk – one on DOS/Windows and another on UNIX operating systems. While widely used, this might not be exactly true. This guy has tried both and documented the results.
Tag: command line
Copying files to remote destinations. With bells and whistles.
If you ever had to copy a file from one machine to another over a network, chances are you know more than one way to do it by now. Especially if you were doing it on some Unix machine. If you are still struggling, here is a short list to get you started:
- Send the file as attachment via email.
- Copy it over the web using http/https or ftp/ftps protocols.
- Use secure copy (scp) provided by many secure shell (ssh) implementations.
Copying a file from one place to another (even remote) is easy. It gets slightly more complex when you need something a bit more smart than just a copy operation. Here is a short list of requirements you might have to your copy process:
- Secure (encrypted) transaction.
- Transfer of only those parts of file which have changed since last copy.
- Support for resuming the transfer if connection was broken, instead of resending the whole thing.
- Limit the bandwidth consumed by the copy process.
If you ever had to satisfy any or all of these requirements in a single solution, you might have scratched your head more than once. Or, at least, spent some quality time on the web looking for the right tool. I know, because I did. In order to improve someone else’s chances of finding such a tool faster, I’ll describe it here.
The tools itself is called rsync. As far as I know, it comes with most Unix boxes and for sure with most Linux installations. rsync has a number of options and a very flexible functionality. Just by itself it can satisfy all of the requirements above, except for secure encrypted transaction. And the beauty of the rsync is that it can be easily integrated with other tools to satisfy even more requirements. In order to provide encrypted transfer it can be easily used with secure shell (ssh) implementation, such as OpenSSH.
Enough of theory, let’s go for an examlpe.
Here is one way of using rsync which satisfies all the requirements above.
rsync -e ssh --bwlimit=50 /path/to/local/file user@remote_host:/path/to/remote/dir
Now, let’s see how it works. rsync itself can transfer only the changed parts of the file. If there is no prior copy of the file at the remote host, than the whole thing will be transfered. rsync by itself supports resuming of transfer. So, if connection between your two machines went down for a while, you can just rerun the same rsync command and it will continue from where it stopped. rsync supports bandwidth limitations. In the example above, I used --bwlimit=50
which will result in rsync transferring at speeds of 50 KiloBytes (not bits) per second or less. The encryption part is done by secure shell and rsync is told to use it by -e ssh
argument. As you could probably guessed, in the example above, /path/to/local/file
should point to the file that you want to transfer. user
should be a username to use when identifying at remote_host
. If there is a need for password input, you will be prompted before the transfer. /path/to/remote/dir
should obviously point to the directory at remote machine where the file must be stored.
Read the manual for rsync and ssh and you will discover a number of other useful options that will make your life easier. To close on a nice note, I’ll suggest you use two other parameters to rsync: -r
which tells rsync to use recursive mode, in case you want to transfer a directory structure and not a single file; and --progress
which tells rsync to show what it is doing in real-time.
Bash prompts
Maintainer of the Bash Prompt HOWTO, it turns out, also keeps a small website with examlpes of different bash prompts. It provides screenshots together with code for some really wicked prompts. If you are into that kind of things, but all of ideas, than check the site out. Maybe you’ll find something for yourself. :)
Port forwarding with Putty
Putty, it seems, is the most popular Telnet/SSH client for MS Windows (although it does run UNIX too). It is small, free, stable, and flexible. It has all the functionality one will ever need (with a few exceptions, of course).
Secure Shell (SSH) provides for a number of interesting uses. One of them is port forwarding, also known as tunneling. It is used when there is a need to pass encrypted information between two hosts while the original protocol for information does not support encryption, or another encryption level is required for some reason.
Apparently, Putty can help poor Windows souls with port forwarding too. This step-by-step tutorial with a lot of pictures shows exactly how to do it. The example used is encrypted email traffic.
Learn UNIX shell
There is a very nice shell tutorial at LinuxCommand.Org.
Not only it covers all the stuff like conditions, loops, and functions, but it also provides a list of resources to continue your education on the matter. The language is simple and the examples are clear.
There is also a nice explanation on how to start writing scripts once you feel more or less comfortable with the command line itself. And, of course, you can find few ready made scripts in the script library over there.
Strongly recommended for shell beginners.