rmdir command working on linux and windows

299 Views Asked by At

I have a deployment script that should work on windows and linux.

I need to delete a directory if it exists. The cmd command on windows is:

if exist BackendFrontendShare rmdir somedir /s /q

Is there some way to write a command that supports both platforms?

I tried

rm -rf somedir || if exist somedir rmdir somedir /s /q

which works on windows, but not on linux

2

There are 2 best solutions below

0
lit On

PowerShell runs on Linux, Mac, and Windows. The same code works on all platforms. https://github.com/PowerShell/PowerShell

$thedir = 'somedir'
if (Test-Path -Path $thedir) { Remove-Item -Recurse -Path $thedir -Force }

Use the -WhatIf switch to find out what would be done if the items were removed.

Remove-Item -Recurse -Path 'somedir' -Force -WhatIf
0
Shaqil Ismail On

You can use many programming languages, but Python is a good choice or powershell core, as you can install and run on Windows and Linux, and would allow your command to be OS independent.