Uploading and Downloading Files in Linux

lrzsz

1.Introduction

In Linux, <span>lrzsz</span> is a tool for file transfer in the terminal. It is an open-source software package that provides a set of command-line tools to easily upload files from a local computer to a remote computer or download files from a remote computer to a local computer.

2.Installation

Before installation, we can use the following commands to check if the software we want to install exists in the yum repository:

# centos
yum list installed |grep lrzsz

# ubuntu
apt list |grep lrzsz

If it is not installed, execute the installation operation

# centos
yum install lrzsz

# ubuntu
apt install lrzsz

3.Usage Example

# Upload file
rz

# Download file (default downloads to the terminal's download directory)
sz file_path

scp

1.Introduction

SCP provides higher security and confidentiality for file transfers by using the SSH protocol for encryption and authentication; the SCP command is mainly used for file transfer between the local host and the remote host.

2.Example

# Transfer file from local host to remote host
scp [local_file_path] [username]@[remote_host_IP_address]:[target_path]

# Transfer file from remote host to local host
scp [username]@[remote_host_IP_address]:[remote_file_path] [local_target_path]

# Transfer directory (r for recursive directory and its contents)
scp -r [local_directory_path] [username]@[remote_host_IP_address]:[target_path]

# Transfer with specified port number (if the SSH port of the remote host is not the default 22, use the -P option to specify the port number)
scp -P [port_number] [local_file_path] [username]@[remote_host_IP_address]:[target_path]

3.Common Parameters

-p: Preserve the original file’s creation time when copying files.-q: Do not display any prompt messages during file copying.-r: Copy the entire directory /home/upload.-v: Display prompt information while copying files.-P: Specify the port.

wget

1.Introduction

<span>wget</span> is a very powerful command-line tool in Linux and Unix-like systems for downloading files from the web.

2.Basic Syntax

wget [options] URL

3.Common Options

-O, –output-document=FILE: Save the downloaded file with the specified filename instead of the default filename from the URL.-c, –continue: Resume download functionality; if the file has been partially downloaded, wget will attempt to continue from where it was interrupted.-q, –quiet: Silent mode, no output information displayed.-v, –verbose: Verbose mode, displays more download progress information.-t, –tries=NUMBER: Set the number of retries; if the download fails, wget will attempt to download again the specified number of times.-T, –timeout=SECONDS: Set the timeout; if the server response exceeds this time, wget will abandon the download.-b, –background: Background download mode, wget will execute the download task in the background.-P, –directory-prefix=PREFIX: Specify the directory prefix for storing downloaded files.–no-clobber: If the remote file already exists locally and the file sizes are the same, it will not download again.-r, –recursive: Recursive download, used to download an entire website or directory structure.-l, –level=NUMBER: When used with <span>-r</span>, limit the depth of recursive downloads.-U, –user-agent=AGENT: Set the user agent string to simulate different browsers or other HTTP clients.–limit-rate=RATE: Limit the download speed, format can be <span>Kb/s</span>, <span>Mb/s</span>, etc.-i, –input-file=FILE: Read the list of URLs from a file for downloading.

3.Example

# Download file
wget http://example.com/file.zip

# Download file and rename
wget -O new_file.zip http://example.com/file.zip

curl

1.Introduction

<span>curl</span> is another widely used command-line tool for data transfer, supporting various protocols including HTTP, HTTPS, FTP, FTPS, etc. Similar to <span>wget</span>, <span>curl</span> is also very powerful, but its application scenarios are more flexible, not limited to downloading files, but also supports sending various types of HTTP requests (GET, POST, etc.), uploading data, handling cookies, authentication, etc.

2.Basic Syntax

curl [options] URL

3.Common Options

-O, –remote-name: Save the file based on the remote filename instead of using the default output.-o, –output : Write output to the specified filename or file descriptor.-L, –location: Follow redirects returned by the server.-I, –head: Only fetch HTTP header information, do not download actual content.-X, –request COMMAND: Specify the HTTP request method, such as <span>GET</span>, <span>POST</span>, etc.-d, –data or -F, –form : Send POST data or form data. <span>-d</span> is used for sending simple key-value pairs, while <span>-F</span> is used for MIME-encoded data, such as file uploads.-H, –header : Customize HTTP headers, such as adding <span>User-Agent</span> or <span>Cookie</span>.-u, –user <user[:password]>: For HTTP basic authentication, provide username and password.–insecure: (For HTTPS) Ignore certificate verification, which may be useful in testing environments but is not secure.-s, –silent: Silent mode, reduces output.-v, –verbose: Display detailed communication information.

3.Example

# Download file
curl -O http://example.com/file.zip

# Send POST request
curl -X POST -d "key=value" http://example.com/api

# Get HTTP request header information
curl -I http://example.com

# Request with custom header
curl -H "User-Agent: MyCustomAgent" http://example.com

# Download and save as custom filename
curl -o myfile.txt http://example.com/document.txt

Leave a Comment