Skip to main content

Search stories

No stories found. Try another word.

Tech

Terminal File Management: Bash vs CMD vs PowerShell Commands Compared

Muhammad Fikri Gunawan · · 7 min read
Terminal File Management: Bash vs CMD vs PowerShell Commands Compared
Photo: Farzad Nazifi, CC0, via Wikimedia Commons

The same folder and file task needs different commands in Bash, Windows CMD and PowerShell. Here is a side-by-side guide to listing, copying, moving, deleting, searching and killing processes.

Short answer: file and folder commands are the one part of the terminal that changes between platforms. Bash (Linux, macOS and Git Bash) uses ls, cp, mv and rm; Windows CMD uses dir, copy, move and del; PowerShell uses cmdlets such as Get-ChildItem, Copy-Item, Move-Item and Remove-Item. This guide lines the three up so you can translate any command in seconds.

Git Bash runs on Windows but imitates a Unix shell, so everything in the Bash column works there too. PowerShell also accepts many Bash-style aliases like ls, cd and pwd, but the full cmdlet names are what you will see in scripts and documentation.

How do I see where I am and move between folders?

  • Show the current path: pwd in Bash and PowerShell, cd with no argument in CMD.
  • List folder contents: ls -la in Bash, dir in CMD, Get-ChildItem (alias ls) in PowerShell.
  • Go into a folder: cd folder_name in all three.
  • Go up one level: cd .. in all three.
  • Go back to the previous folder: cd - in Bash.

How do I create folders and files?

  • One folder: mkdir folder_name in Bash and CMD, New-Item -ItemType Directory folder_name in PowerShell.
  • Nested folders at once: mkdir -p a/b/c in Bash, mkdir a\b\c in CMD, New-Item -ItemType Directory -Path a\b\c -Force in PowerShell.
  • An empty file: touch file.txt in Bash, type nul > file.txt in CMD, New-Item file.txt in PowerShell.

How do I copy, move, rename and delete?

  • Copy a file: cp file.txt target/, copy file.txt target\ or Copy-Item file.txt target\.
  • Copy a folder with its contents: cp -r folder target/, xcopy folder target\ /E /I or Copy-Item folder target\ -Recurse.
  • Move a file or folder: mv source target, move source target or Move-Item source target.
  • Rename: mv old.txt new.txt, ren old.txt new.txt or Rename-Item old.txt new.txt.
  • Delete a file: rm file.txt, del file.txt or Remove-Item file.txt.
  • Force-delete a folder and everything in it: rm -rf folder_name, rmdir /s /q folder_name or Remove-Item folder_name -Recurse -Force.

Warning: these delete commands skip the recycle bin. Double-check the path, and never run rm -rf with a path you have not read twice.

How do I read and search inside files?

  • Show a file in the terminal: cat file.txt, type file.txt or Get-Content file.txt.
  • Search for text inside a file: grep "keyword" file.txt, findstr "keyword" file.txt or Select-String "keyword" file.txt.
  • Find files by name or pattern: find . -name "*.js", dir /s /b *.js or Get-ChildItem -Recurse -Filter *.js.
  • Clear the screen: clear in Bash, cls in CMD and PowerShell (Clear-Host).

How do I set environment variables and aliases?

  • Set a temporary variable: export NAME=value in Bash, set NAME=value in CMD, $env:NAME="value" in PowerShell.
  • Print a variable: echo $NAME, echo %NAME% or echo $env:NAME.
  • Create a shortcut command: alias ll='ls -la', doskey ll=dir or Set-Alias ll Get-ChildItem.
  • See command history: history, doskey /history or Get-History.

Which commands help with ports, processes and permissions?

When a dev server refuses to start because port 3000 is busy, these are the commands you need.

  • Find what uses port 3000: lsof -i :3000 in Bash, netstat -ano | findstr :3000 in CMD, Get-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess in PowerShell.
  • Kill a process by PID: kill -9 <PID>, taskkill /PID <PID> /F or Stop-Process -Id <PID> -Force.
  • Make a script executable: chmod +x file.sh in Bash. It does not apply on Windows.
  • Create a symbolic link: ln -s target link, mklink link target or New-Item -ItemType SymbolicLink -Path link -Target target.
  • Check a folder size: du -sh folder_name in Bash, dir folder_name /s in CMD, Get-ChildItem folder_name -Recurse | Measure-Object Length -Sum in PowerShell.

Frequently asked questions

Why does touch not work in Windows CMD or PowerShell?

touch is a Unix command. In CMD use type nul > file.txt, and in PowerShell use New-Item file.txt. In Git Bash, touch works as usual.

Is Git Bash the same as Linux Terminal?

For everyday work, yes. Git Bash emulates a Unix shell on top of Windows, so the commands in the Bash column work there. System-level tools such as lsof may be missing.

Which shell should a web developer on Windows choose?

Pick one and learn it well. Git Bash gives you the same commands as macOS and Linux, which helps when you follow tutorials or deploy to servers. PowerShell is the better choice when you script Windows itself.

How can I remember all these commands?

Learn the Bash set first, since it is the most widely documented, then map each one to its CMD or PowerShell twin using the lists above. Aliases such as ls, cd and pwd in PowerShell soften the switch.

Previous in this series: Web developer commands for Node.js, NPM, PHP and WP-CLI. Next: the Git and GitHub CLI cheat sheet.