In Linux systems, the core scenarios for uploading files are divided into uploading local files to a remote Linux server and transferring files between different paths within local Linux. Below are the most commonly used and efficient commands, along with detailed usage and scenario descriptions:
1. Core Scenario 1: Local → Remote Linux Server (Most Common)
Requires network transmission, with the core commands being <span>scp</span> (simple and reliable), <span>rsync</span> (efficient incremental transfer), and <span>sftp</span> (interactive transfer), all based on the SSH protocol (default port 22). You need to know the remote server’s IP, username, password / SSH key.
1. scp Command (Secure Copy, Simple and Direct)
<span>scp</span> is a secure copy command based on SSH, supporting file/directory transfer. It is simple to use and suitable for one-time uploads of a small number of files.
Basic Syntax
bash
# Local file → Remote server<span>scp [options] local_file_path remote_username@remoteIP:remote_target_path</span># Local directory → Remote server (must add -r for recursion)<span>scp -r [options] local_directory_path remote_username@remoteIP:remote_target_path</span>
Common Options
<span>-r</span>: Recursively transfer directories (must add, otherwise cannot upload folders);<span>-P port_number</span>: Specify the remote SSH port (default 22, if the server port is modified, it must be explicitly specified);<span>-i key_file_path</span>: Use SSH private key authentication (password-free, more secure than password login);<span>-l bandwidth_limit</span>: Limit the transfer rate (e.g.,<span>-l 1024</span>means 1MB/s).
Practical Examples
bash
# 1. Upload a single local file (e.g., upload local test.txt to remote /home/user directory)<span>scp /home/local/test.txt [email protected]:/home/user/</span># 2. Upload a local directory (e.g., upload local docs folder to remote /home/user directory)<span>scp -r /home/local/docs [email protected]:/home/user/</span># 3. Upload file with non-default server port (e.g., 2222)<span>scp -P 2222 /home/local/image.jpg [email protected]:/home/user/images/</span># 4. Use SSH key authentication (password-free) to upload a directory<span>scp -r -i ~/.ssh/my_key.pem /home/local/project [email protected]:/opt/</span>
Notes
- If the remote target path is a directory, ensure the directory exists (otherwise an error will occur);
- When logging in with a password, the command will prompt for the remote server’s username and password; for key login, ensure the private key permission is
<span>600</span>(<span>chmod 600 ~/.ssh/my_key.pem</span>), otherwise SSH will refuse to use it.
2. rsync Command (Efficient Incremental Transfer, Suitable for Large Files / Frequent Synchronization)
<span>rsync</span> is the most powerful synchronization tool in Linux, supporting incremental transfer (only transferring changed files/file segments), faster than <span>scp</span>, and supports resuming interrupted transfers, making it suitable for uploading large files and frequently synchronizing directories (e.g., backup scenarios).
Basic Syntax
bash
# Local file/directory → Remote server<span>rsync [options] local_path remote_username@remoteIP:remote_target_path</span>
Core Options (Must Remember)
<span>-a</span>: Archive mode (equivalent to<span>-rlptgoD</span>), recursively transfers directories, preserving file permissions/timestamps/owners, etc. (recommended to add by default);<span>-v</span>: Display detailed transfer logs (helpful for troubleshooting);<span>-z</span>: Compress data during transfer (reducing network bandwidth usage);<span>-P</span>: Resume interrupted transfers (abbreviation for<span>--partial --progress</span>, supports continuing transfers of large files after interruption and displays a progress bar);<span>-e "ssh -p port_number"</span>: Specify SSH port (used when the server port is not 22);<span>--delete</span>: Delete files in the remote target path that are not present in the local during synchronization (use with caution, suitable for full synchronization scenarios).
Practical Examples
bash
# 1. Upload a large file (e.g., 10GB video.mp4), supports resuming interrupted transfers + progress bar<span>rsync -avzP /home/local/video.mp4 [email protected]:/home/user/media/</span># 2. Synchronize local directory to remote, preserving permissions + compressing transfer, server port 2222<span>rsync -avzP -e "ssh -p 2222" /home/local/data [email protected]:/home/user/</span># 3. Incremental synchronization (only transfer changed files), suitable for frequently updated project directories<span>rsync -avzP /home/local/project [email protected]:/opt/project/</span>
Advantages Compared to scp
- Large file transfer:
<span>rsync</span>supports resuming interrupted transfers, while<span>scp</span>requires retransmission after interruption; - Frequent synchronization:
<span>rsync</span>only transfers changed parts, while<span>scp</span>performs full transfers each time, resulting in vastly different efficiency; - Bandwidth usage:
<span>-z</span>option compresses data, showing significant advantages in weak network environments.
3. sftp Command (Interactive Transfer, Suitable for Manually Selecting Files)
<span>sftp</span> stands for SSH File Transfer Protocol, allowing interactive operations (similar to FTP commands), suitable for scenarios where manual browsing of remote directories and selecting files for upload/download is needed (e.g., temporarily uploading a small number of files when the remote path is uncertain).
Basic Usage
bash
# 1. Connect to the remote server (default port 22, use -P for non-default ports)<span>sftp remote_username@remoteIP</span># or sftp -P 2222 remote_username@remoteIP# 2. After successful connection, enter the interactive command line (common commands are as follows)sftp> put local_file_path remote_target_path # Upload local file to remotesftp> put -r local_directory_path remote_target_path # Upload local directory (-r for recursion)sftp> ls # View files in the current remote directorysftp> cd remote_directory_path # Switch to remote directorysftp> lls # View files in the current local directory (l indicates local operation)sftp> lcd local_directory_path # Switch to local directorysftp> exit # Exit sftp connection
Practical Examples
bash
# 1. Connect to the remote server<span>sftp [email protected]</span># 2. Upload local /home/local/test.txt to remote /home/user directorysftp> put /home/local/test.txt /home/user/# 3. Upload local /home/local/docs directory to remote /home/user directorysftp> put -r /home/local/docs /home/user/# 4. Browse remote directory to confirm upload resultssftp> cd /home/usersftp> ls # Check if test.txt and docs directory existsftp> exit # Exit
Features
- Interactive operation, no need to memorize complex commands, suitable for temporary manual transfers;
- Secure transfer based on SSH, supports password/key authentication;
- Does not support resuming interrupted transfers (not recommended for large files).
2. Scenario 2: Transferring Files Within Local Linux (Different Paths on the Same Host)
No network is required, directly using <span>cp</span> command (copy) or <span>mv</span> command (move, equivalent to “cut and upload”) operations.
1. cp Command (Copy Files / Directories)
bash
# Copy a single file to the target path<span>cp /home/local/file.txt /home/user/backup/</span># Copy a directory (recursively, add -r)<span>cp -r /home/local/docs /home/user/backup/</span># Copy while preserving file attributes (permissions, timestamps, etc.), add -a<span>cp -a /home/local/data /home/user/backup/</span>
2. mv Command (Move Files / Directories, Cut + Paste)
bash
# Move a single file to the target path<span>mv /home/local/file.txt /home/user/backup/</span># Move a directory (no need to add -r, move directly)<span>mv /home/local/docs /home/user/backup/</span>
3. Common Issues Troubleshooting
1. Upload Failed: “Permission denied” (Insufficient Permissions)
- Reason: Insufficient write permissions for the remote target path (e.g., the remote directory is owned by
<span>root</span>, ordinary users cannot write); - Solution:
- Switch to a user with permissions (e.g.,
<span>root</span>) to upload; - Grant permissions to the remote directory:
<span>ssh remote_username@remoteIP "chmod 777 /remote_target_path"</span>(temporary testing, not recommended 777 in production); - Select a directory where the remote user has permissions (e.g.,
<span>/home/username/</span>directory).
2. Upload Timeout / Connection Failed
- Reason: The remote server’s SSH service is not started, the port is blocked by a firewall, or the IP/port is incorrect;
- Solution:
- Test if the SSH connection is normal:
<span>ssh remote_username@remoteIP -p port_number</span>(if you can log in, SSH is normal); - Check the firewall: the remote server should open port 22 (or custom port), e.g.,
<span>firewall-cmd --permanent --add-port=22/tcp && firewall-cmd --reload</span>(CentOS); - Confirm that the remote server IP is reachable (
<span>ping remoteIP</span>).
3. Large File Upload Interrupted
- Solution: Use
<span>rsync -P</span>command (resume interrupted transfer), re-execute the same command to continue the transfer without starting over.
4. Command Selection Recommendations
| Scenario | Recommended Command | Core Advantages |
|---|---|---|
| One-time upload of a small number of files/directories | scp | Simple syntax, no need to memorize complex options |
| Large files, frequent synchronization, incremental transfer | rsync | Resume interrupted transfers, efficient incremental, compressed transfer |
| Interactive operation, manual file selection | sftp | Visual directory browsing, flexible operation |
| Local Linux internal file moving/copying | cp/mv | Local operation, no network required |
Mastering <span>scp</span> (for simple scenarios) and <span>rsync</span> (for complex scenarios) can cover 99% of Linux upload needs. In production environments, prioritize using <span>rsync</span> for transferring large files or frequently synchronizing directories, as it offers better efficiency and stability.