当前位置:Linux教程 - Ftp服务 - ftp服务 - use a text-base ftp

ftp服务 - use a text-base ftp

use a text-base ftp
2004-04-23 15:18 pm
来自:Linux文档
现载:Www.8s8s.coM
地址:无名

Using a text based ftp client is not truly linux specific, however there is a chance that you may need to download a file from an ftp server and don't want the overhead of installing a GUI based ftp client nor using a comparitively bloated webbrowser. Maybe you want to use some of the power and features available in a ftp client that for whatever reasons aren't supported in the webbrowsers.
Most text based ftp clients, regardless of the operating system they run on, use the same command set that you type in to instruct the client on what it is you want it to do - generally speaking you should be able to learn how to use a text based ftp client once and use that knowledge many times. I'm writing this under the assumption that you are using linux to connect to ftp servers.


1. Logging in.
Firstly you will need to open a connection to the ftp server:


$ftp
ftp> open edge

This opens a connection to the ftp server running on my linux box called 'edge' and could also have been done by typing
$ftp edge
After the client has connected to the server you will see a login banner and a username prompt. After typing in the user name you will be prompted for a password - if you want to log in to an 'anonymous' account you should enter youremail address as the password.
To log out, type 'quit'.
If you use ncftp as the ftp client and it has automatic anonymous logins enabled (which it does by default), then you won't be prompted for a username amd password.


2. Directories.
2.1 Initial directory.
If you log in as anonymous then your initial working directory will be the root dierctory, otherwise your initial working directory will be the home directory of the user that you logged in as.
2.2 Changing directories:
You will need to move to the appropiate directory once you have logged in. Doing this is the very same as changing directories within linux - with the 'cd' command. Using 'cd' will move you around the directory structure of the ftp server, if you need to move around the directories of the PC that you are connecting with, then you should use the 'lcd' command.
Depending on which operating system the ftp server is running on it will most likly be case sensitive with regards to file and directory names.
2.3 Directory Listings.
As with Unix, the 'ls' command should be used if you want to get a general listing of what files are in the remote working directory.
There's a particular twist though, if you want a 'long' directory listing similar to what you get with 'ls -l' you can either type 'ls -l' itself or be slightly lazy about it and type 'dir'.
If you would like to keep a local copy of the directory listing, the 'mls' command should be used.
With regard to using mls, you have to specify which remote directory the listing should be of, so if you want a listing of the current directory you have to type '.' as the name of the directory.
You then have to specify the local file that you want the directory listing to be saved to and type 'y' in response to the 'output to local file' question that follows.
2.4 Modification date of a file.
The 'modtime' command is useful for when you need to know when a remote file was last modified.


3. Running a 'shell' command.
It can be useful at times to run a shell command from within an ftp connection. This is done by first typing an exclamation mark (!) followed by the command itself.
I do this quite often to ensure that I am in the correct local directory as it helps to make certain that I am not going to to overwrite a local file. To do this, you should type:

ftp> !pwd /home/ken
You can also use '!' on it's own to get to the command prompt without logging out from the ftp session. You can get back to the ftp prompt by typing 'exit'.


4. Transferring files.
Okay, so now that you know how to log in and change directories the next thing that you need to learn is how to transfer files.
Before you get around to transferring a file though it is important that you are in the correct transfer type.
4.1 Transfer type.
There are two transfer types that can be used to transfer files; ascii and binary.
To see which transfer type is currently active, use the 'type' command.

ftp> type
Using binary mode to transfer files.
ftp> ascii
200 Type set to A.
ftp> type
Using ascii mode to transfer files.
ftp> binary
200 Type set to I.
ftp> type
Using binary mode to transfer files.

As seen in the example above, type 'ascii' to change to the ascii type and 'binary' to change to the binary type.
4.2 Copying files.
To get a copy of a file from the server to the local computer you use the command 'get'.
Tp put a copy of a local file onto the sever you use the command 'put'.
4.3 Copying multiple files.
If you need to copy a number of files of the same transfer type from one remote directory to a local directory you can use the command 'mget', similarly 'mput' should be used to transfer a number of files of the same type onto the server.
You can be as specific or as general in your use of 'mget' or 'mput' as you like.
For example 'mget *' means that all remote files (both binary and ascii) will get copied down from the server. Similarly 'mget foo*' means that all files starting with 'foo' will be copied down from the server.
Each time that you use mget or mput you will be prompted to confirm the transfer. Typing anything other than 'n' means the file will be copied.
You can quit out of mget or mput and remain in the ftp session by pressing ctrl-c.
4.4 Resuming a 'get'.
One of the nice advantages that you get by using an ftp client over a web browser that supports the ftp protocol is that, if your connection to the server breaks for whatever reason, you can get the ftp client to start over again from where it left off. This is done by using the 'reget' command.


4.5 Visual confirmation.
If you are copying a large file and would like some form of visual indication of progress then the 'hash' command, which causes the hash mark (#) to be printed for each segment of the file which has been copied, will be of use to you.
You should use the 'hash' command before copying the file for which you want the visual indication.


5. Deleting files.
Once you have made a local copy of a file from the server you may want to delete the original file that you copied. Naturally, this will only work if you have the appropriate permissions to delete the file.
To do this, use the 'delete' command.
if you have a number of files to delete, I'd suggest you use the 'mdelete' command which can be aborted by pressing ctrl-c.


6. Macros.
I doubt this facility is available in all text based ftp clients but it is far too useful to not mention.
You can use 'macdef' to combine several commands into a macro, so that for example if you find that you always follow a 'pwd' with a '!pwd' then you could combine these into a macro called '2pwd' and use that instead. For example:


ftp> macdef 2pwd
Enter macro line by line, terminating it with a null line
pwd
!pwd
<Enter>
To indicate that you want to use a macro instead of a standard command you have to preceed the name of the macro with a dollar sign ($). For example:
ftp> $2pwd
pwd
257 "/home2/wolfhaven" is current directory.
!pwd
/home/ken

This is a very trivial example but you should be able to see how to apply this to a larger sequence of commands.
X. More Information.
If you would like to learn more of the ftp client's commands, I'd suggest you use the help command and also man ftp at the command prompt. Using 'help' on its own will present you with a list of commands that the ftp client supports, to get details of what a specific command does then add it as a parameter to the help command. For example:

ftp> help verbose
verbose toggle verbose mode