Windows 10命令提示符:如何在CMD中执行unzip、sed、cat及Ubuntu命令?
Hey there! Let's break down how to handle unzip, sed, cat operations in Windows 10 CMD, plus the best way to run your original Ubuntu terminal commands. I've got two solid approaches for you—using native Windows tools/alternatives, or leaning on WSL for a full Ubuntu environment that plays nice with Windows.
If you want to stick strictly to CMD without installing extra stuff, here's how to replicate those Linux commands:
cat Equivalent in CMD
Windows CMD has the type command that does almost exactly what cat does in Ubuntu:
- Display the contents of a single file:
type filename.txt - Concatenate multiple files into a new one:
type file1.txt file2.txt > combined_output.txt
Just like cat, you can use >> to append to an existing file instead of overwriting it.
unzip Equivalent in CMD
Starting with Windows 10 version 1709, Microsoft added a native tar command that handles zip files perfectly:
- Extract a zip archive to the current directory:
tar -xf your_archive.zip
If you're on an older Win10 build, you can use the less flexible expand command:
expand your_archive.zip -F:* destination_folder
sed Equivalent in CMD
CMD doesn't have a native sed utility, but you've got two good workarounds:
Call PowerShell from CMD:
PowerShell has built-in string replacement that mimicssed's core functionality. Run this directly in CMD:powershell -Command "(Get-Content your_file.txt) -replace 'old_text', 'new_text' | Set-Content your_file.txt"This replaces all instances of
old_textwithnew_textinyour_file.txt, just likesed -i 's/old/new/g' your_file.txtin Ubuntu.Install GNU Sed for Windows:
Grab a standalone binary of GNU Sed (the same one used in Ubuntu) from official GNU tools for Windows, add its folder to your systemPATH, and then you can usesedexactly like you do in Ubuntu:sed -i 's/old_text/new_text/g' your_file.txt
If you want to run your exact Ubuntu terminal commands without any tweaks, WSL (Windows Subsystem for Linux) is the way to go—it lets you run a full Ubuntu environment right alongside Windows. Here's how to set it up:
Enable WSL:
Open CMD as an administrator, then run:wsl --installThis automatically enables the WSL feature and installs the latest Ubuntu distro. If you're on an older Win10 build (pre-2004), run this instead:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestartThen restart your computer to apply the changes.
Set Up Ubuntu:
After restarting, open the Start Menu, search for "Ubuntu", and launch it. Follow the prompts to create a Linux username and password (this is separate from your Windows account).Access Your Windows Files:
Your Windows drives are mounted in WSL under/mnt/—so your C: drive is at/mnt/c/. To navigate to your Windows documents, for example:cd /mnt/c/YourWindowsUsername/DocumentsRun Your Original Ubuntu Commands:
Now you can useunzip,sed,cat, and any other Ubuntu commands exactly as you did before. For example:unzip archive.zip sed -i 's/foo/bar/g' script.sh cat logfile.txtAll your existing Linux scripts will work without modification—no need to rewrite anything for Windows.
内容的提问来源于stack exchange,提问作者Philipp M

