When moving files between Windows and Linux systems via FTP, the transfer mode selection directly impacts file integrity. The mysterious "one continuous line" phenomenon occurs when ASCII mode incorrectly handles line endings across platforms.
Binary Mode performs a raw byte-for-byte copy:
# Python example of binary transfer with open('file.txt', 'rb') as f: ftp.storbinary('STOR file.txt', f)
ASCII Mode attempts line ending conversion:
# Python example of ASCII transfer with open('file.txt', 'r') as f: ftp.storlines('STOR file.txt', f) # Converts \r\n to \n
Many FTP clients default to Auto mode which:
- Guesses file type by extension (.txt = ASCII, .exe = Binary)
- Often misclassifies files with uncommon extensions
- May apply incorrect line ending conversions
For Windows-to-Linux transfers:
# Safe transfer method in Python import ftplib ftp = ftplib.FTP('host') ftp.login('user', 'pass') # Explicit binary transfer with open('script.sh', 'rb') as f: ftp.storbinary('STOR script.sh', f) # Preserves original line endings # Alternative using LF-only conversion with open('file.txt', 'r', newline='\n') as f: # Force Unix line endings ftp.storlines('STOR file.txt', f)
Special scenarios requiring attention:
- Mixed-format files (e.g., .csv with binary headers)
- Configuration files with platform-specific line endings
- Version control files (.gitattributes overrides FTP behavior)
For critical transfers, consider checksum verification:
import hashlib def verify_transfer(local, remote): local_hash = hashlib.md5(open(local,'rb').read()).hexdigest() remote_hash = ftp.md5(remote) return local_hash == remote_hash
When transferring files from Windows to Linux servers via FTP, many developers encounter this frustrating scenario:
Original Windows file: Line 1\r\n Line 2\r\n Line 3\r\n Becomes on Linux: Line 1Line 2Line 3
FTP clients typically offer three transfer modes:
- ASCII: Performs end-of-line conversion
- Binary: Exact byte-for-byte transfer
- Auto: Client attempts to detect file type
Windows uses \r\n
(CR+LF) while Linux uses \n
(LF) alone. Here's how each transfer mode handles this:
// Binary mode transfer: Windows: "Hello\r\nWorld" → Linux: "Hello\r\nWorld" // ASCII mode transfer: Windows: "Hello\r\nWorld" → Linux: "Hello\nWorld"
Binary Mode (recommended for most cases):
- Executables (.exe, .dll)
- Compressed files (.zip, .tar.gz)
- Images/media files
ASCII Mode (use with caution):
- Plain text files (.txt, .csv)
- Source code (.py, .js, .php)
- Configuration files (.conf, .ini)
If you've already transferred files incorrectly, here's how to fix them in Linux:
# Convert Windows line endings to Unix: dos2unix corrupted_file.txt # Or using sed: sed -i 's/\r$//' corrupted_file.txt
For WinSCP:
Session → Preferences → Transfer → Transfer Mode → Binary
For FileZilla:
Transfer → Transfer Type → Binary
For critical transfers, convert files before sending:
# PowerShell command to convert to Unix line endings: Get-Content input.txt | Out-File -Encoding ASCII -NoNewline output.txt
The "Auto" mode tries to detect file types but often gets confused by:
- Files with mixed content
- Uncommon file extensions
- Files with binary data but text extensions