UNIX has a very powerful command line interface, and one of the things that you can do is remotely access a UNIX server. FTP is one such remote protocol that allows for the transfer of files easily. This FTP example shows how to copy, rename, and delete files.
This just means you want to open a FTP connection to the ftp server at “alvinpoh.com”. Once connected, you’ll be prompted to log in using a username and password. There are public ftp servers which provide anonymous access. This just means that you can log in using the username “anonymous” and your email address as password.
So what do you do once you’re connected?
This command prints the names of the files and subdirectories in the current directory on the remote computer. So let’s say you see that there’s a folder named “public_html” inside.
ftp> cd public_html
Running this command changes the current directory to the subdirectory of “public_html” (this subdirectory must exist, if not, an error would be shown). Now that you’re in “public_html”, let’s go back up again.
ftp> cd ..
So this changes the current directory to the parent directory (where you were at). Now let’s assume we want to transfer a photograph to our remote server. So, first of all, we need to determine where our photos are stored in our local computer (the local computer’s the computer that you’re physically at).
ftp> lcd my_photos
Changes the current directory [em]on your local computer[/em] to “my_photos”.
A ‘!’ in front of the command will execute the specified command on the local computer. So ‘!ls’ lists the file names and directory names of the current directory on your local computer. Let’s imagine that after you run !ls, you find that there’s a file named “photo.jpg” that you want to transfer to the remote computer.
This changes to “ascii” mode for transferring text files. But we’re not transferring text files, so…
So this changes to “binary” mode for transferring all files that are not text files.
ftp> put photo.jpg
Uploads a copy of the file photo.jpg from your local computer (remember, we’re at the “my_photos” directory) to the remote computer. Warning: If a file exists with the same name, it will be overwritten. Say you find a photo on the remote computer that you’d like to download to your local computer – no problems!
ftp> get interesting_photo.jpg
This will download the file interesting_photo.jpg from the remote computer to your local computer (remember, we’re still at the “my_photos” directory). Warning: If a file exists with the same name, it will be overwritten. But what if there’s a lot of files that you want to download? Well, there’s a useful twist on the get command for that!
ftp> mget *.jpg
With the mget command, you can now download multiple files. I suppose the “m” stands for “multiple” or “mass”. In this case, our command downloads all files that end with “.jpg” to our local computer.
ftp> mput *.jpg
Similarly, this command uploads all files in your current local directory that end with “.jpg”.
ftp> mdelete *.jpg
That’s not all – there’s a mdelete command that deletes all files that end with “.jpg”. Neat? But if you tried these commands, you’ll find that if there were 1000 files, you’d had to confirm the action for each of those 1000 files. Here’s a workaround for that.
This toggles interactive mode on or off so that commands on multiple files are executed without user confirmation. You’ll see a message telling you “Interactive mode off” or “Interactive mode on” after running this command.
We’re done, so type that command to exit the ftp program. Or alternatively, try typing “bye” for a friendly version of that command! And if you’re stuck at any point of time, you can always get more help.
This is the help function which lists the commands that you can use to show the directory contents, transfer files, and delete files.
- Recursively Download Folders using FTP with wget in UNIX (even SSH)
- How to find and replace a text string in all files on your cPanel / WHM / Linux / Unix server
- Common Useful UNIX Commands
- Recursively Delete Files and Retain Folders in Windows
- How To Enable Gzip Compression