CIS120 Linux Fundamentals by Scott Shaper

wget, ftp and sftp Commands

Think of file transfers on the internet like delivering packages. The wget command is like a delivery service that picks up a package (file) for you with no questions asked - you just give them the address and they bring it back. The ftp command is like going to a warehouse yourself to pick up or drop off packages, but everyone can see what you're carrying. The sftp command is like that same warehouse visit, but with a private, secure tunnel that keeps your packages hidden from prying eyes. These commands help you transfer files between computers over the internet, with varying levels of automation and security.

The course setup.sh script creates ~/playground/chapter7/sftp_practice/ with sample files (important.txt, report.txt) for SFTP get/put practice and urls_to_download.txt listing several URLs for wget -i practice (batch downloads from one file). Run cd ~/playground/chapter7/sftp_practice before trying the wget and SFTP examples that use local files.

Quick Reference

Command What It Does Common Use
wget URL Downloads files from the web Getting files from websites without a browser
wget -c URL Resumes interrupted downloads Continuing large downloads after connection issues
wget -i file Downloads every URL listed in a text file Batch downloads without typing each URL on the command line
ftp hostname Connects to FTP server (insecure) Legacy systems that don't support SFTP
sftp user@hostname Connects to SFTP server (secure) Securely transferring files between computers
get file Downloads a file (in FTP/SFTP) Retrieving files from a remote server
put file Uploads a file (in FTP/SFTP) Sending files to a remote server

When to Use These Commands

The wget Command

Think of wget as your personal delivery service on the internet. You give it an address (URL), and it goes and fetches whatever is at that location without you needing to interact with it further. It's perfect for downloading files from websites directly to your computer or server without using a browser.

Option What It Does When to Use
-O filename Saves the download with a different name When you want to rename the file as it downloads
-i filename Reads download URLs from a text file instead of typing them on the command line When you have many URLs (one URL per line; lines starting with # are comments and are skipped)
-c Resumes a partially downloaded file When your download was interrupted and you want to continue
-q Quiet mode (no output) When running in scripts where you don't need progress info
-r Downloads recursively (follows links) When you want to download an entire directory or website
-l depth Limits the recursion depth When downloading recursively but want to limit how deep it goes
--limit-rate=rate Limits download speed When you need to avoid consuming all available bandwidth

Practical Examples

# Download the college website index page (saves as index.html in current directory)
wget https://www.wccnet.edu/
# Or save with a specific name:
wget -O wcc_homepage.html https://www.wccnet.edu/

cd ~/playground/chapter7/sftp_practice
wget -i urls_to_download.txt
# -i (--input-file) tells wget to read URLs from urls_to_download.txt: one URL per line;
# lines starting with # are treated as comments. Each file is downloaded to this directory.

# Resume a download that was interrupted
wget -c https://www.wccnet.edu/

# Download quietly (no progress information)
wget -q https://www.wccnet.edu/

# Download with a limit on speed (be considerate when practicing)
wget --limit-rate=500k https://www.wccnet.edu/

Understanding wget Output

Sample Output Explained

--2024-07-10 10:00:00--  https://www.wccnet.edu/
Resolving www.wccnet.edu (www.wccnet.edu)... 192.0.2.1
Connecting to www.wccnet.edu (www.wccnet.edu)|192.0.2.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: 'index.html'

index.html                 [  <=>                ]  45.2K  --.-KB/s    in 0.1s

2024-07-10 10:00:01 (450 KB/s) - 'index.html' saved [46280]

Let's break down what this means:

  • --2024-07-10 10:00:00-- https://www.wccnet.edu/: Shows the time and URL being downloaded
  • Resolving www.wccnet.edu...: Converting the domain name to an IP address
  • Connecting to www.wccnet.edu...: Establishing a connection to the server (port 443 for HTTPS)
  • HTTP request sent, awaiting response... 200 OK: Server responded with status 200 (success)
  • Length: unspecified [text/html]: The file is HTML (a web page)
  • Saving to: 'index.html': When you download a URL that ends in /, wget typically saves it as index.html
  • The progress line shows download completion
  • The last line shows completion time, speed, and bytes downloaded

The ftp and sftp Commands

Think of ftp (File Transfer Protocol) as walking into a public storage facility where you can deposit or retrieve your files. Everyone can see what you're carrying in and out. In contrast, sftp (Secure File Transfer Protocol) is like that same facility but with a private, secured tunnel that keeps your files hidden from view. sftp uses SSH encryption to protect your data and login credentials, making it much safer for transferring sensitive files over the internet.

Due to security concerns, sftp should always be your first choice, as ftp transmits passwords and data in plaintext that can be intercepted. Think of it like the difference between shouting your credit card number across a crowded room versus whispering it directly to the cashier.

Option What It Does When to Use
-b batchfile Runs commands from a file When automating multiple file transfers
-C Enables compression When transferring over slow connections to save bandwidth
-P port Specifies a different port When the server uses a non-standard port
-i identity_file Uses a private key file When using key-based authentication instead of a password
-v Verbose mode (shows more details) When troubleshooting connection problems

Common SFTP Interactive Commands

Once connected to an SFTP server, you'll see an sftp> prompt where you can type various commands. Think of this like being in the file storage facility with a walkie-talkie to your computer - you can give instructions to retrieve files from either location.

Command What It Does When to Use
ls Lists files on the remote server When you need to see what files are available on the server
lls Lists files on your local computer When you need to check your local files without leaving SFTP
cd directory Changes directory on the remote server When you need to navigate to different folders on the server
lcd directory Changes directory on your local computer When you need to change where files will be saved locally
get file Downloads a file from server to your computer When you need to retrieve a file from the server
get -r directory Downloads an entire directory When you need to download multiple files at once
put file Uploads a file from your computer to server When you need to send a file to the server
put -r directory Uploads an entire directory When you need to upload multiple files at once
mkdir directory Creates a new directory on the server When you need to organize files on the server
rm file Deletes a file on the server When you need to remove a file from the server
exit or bye Closes the SFTP connection When you're finished transferring files

Practical Examples for SFTP

For the sftp> put report.pdf you can download this file to your local computer and then go into the directory where you downloaded the file and then use the sftp> put command to upload the file to the server.

# From the course practice directory you have important.txt and report.txt to get/put
cd ~/playground/chapter7/sftp_practice

# Connect to an SFTP server
sftp user@example.com
# Prompts for password, then connects to the server

# Connect with a specific port
sftp -P 2222 user@example.com
# Connects using port 2222 instead of the default port 22

# Navigate through directories
sftp> cd /var/www/html
sftp> lcd ~/Downloads
# Changes to /var/www/html on server and ~/Downloads locally

# Download a file
sftp> get important.txt
# Downloads important.txt from server to local computer

# Upload a file
sftp> put report.pdf
# Uploads report.pdf from local computer to server

# Download multiple files
sftp> get *.txt
# Downloads all .txt files from current remote directory

# Download an entire directory
sftp> get -r projects
# Downloads the projects directory and all its contents

# Upload an entire directory
sftp> put -r website
# Uploads the website directory and all its contents

Understanding SFTP Session Output

Sample Session Explained

$ sftp user@example.com
Connecting to example.com...
user@example.com's password: 
sftp> ls
documents  images  index.html  styles.css
sftp> cd images
sftp> ls
logo.png  background.jpg  icon.svg
sftp> lcd ~/Downloads
sftp> get logo.png
Fetching /home/user/images/logo.png to logo.png
/home/user/images/logo.png                   100%   24KB  350.5KB/s   00:00
sftp> put new-image.jpg
Uploading new-image.jpg to /home/user/images/new-image.jpg
new-image.jpg                                100%   58KB  425.2KB/s   00:00
sftp> bye
$

Let's break down what happened:

  • $ sftp user@example.com: Started the SFTP client and tried to connect to example.com as "user"
  • user@example.com's password:: Prompted for the user's password (not shown when typing)
  • sftp> ls: Listed files in the current remote directory
  • sftp> cd images: Changed to the "images" directory on the server
  • sftp> lcd ~/Downloads: Changed to the Downloads directory on local computer
  • sftp> get logo.png: Downloaded logo.png from server to local Downloads folder
  • The system showed progress of the download (24KB at 350.5KB/s)
  • sftp> put new-image.jpg: Uploaded new-image.jpg from Downloads to server's images folder
  • The system showed progress of the upload (58KB at 425.2KB/s)
  • sftp> bye: Closed the SFTP connection

Tips for Success

Common Mistakes to Avoid

Best Practices