Skip to content

Instantly share code, notes, and snippets.

@muath-ye
Created October 20, 2024 16:59
Show Gist options
  • Save muath-ye/8ae91b8137d5edcb1f1c63951b57074a to your computer and use it in GitHub Desktop.
Save muath-ye/8ae91b8137d5edcb1f1c63951b57074a to your computer and use it in GitHub Desktop.
Bash script to securely overwrite and optionally delete files on Windows.
@echo off
setlocal
REM Function to securely overwrite a file, including binary files
:SecureOverwriteFile
echo Overwriting file: %1
set FILE_SIZE=0
for %%I in (%1) do set FILE_SIZE=%%~zI
REM Check if file size is greater than 0
if %FILE_SIZE% gtr 0 (
REM Overwrite with random data
powershell -Command "[System.IO.File]::WriteAllBytes('%1',(1..%FILE_SIZE% | ForEach-Object {Get-Random -Minimum 0 -Maximum 255}))"
REM Overwrite with zeros
powershell -Command "[System.IO.File]::WriteAllBytes('%1',(0..(%FILE_SIZE%-1) | ForEach-Object {0}))"
)
echo File securely overwritten: %1
goto :EOF
REM Function to delete a file
:SecureDeleteFile
echo Deleting file: %1
del /f /q "%1"
echo File securely deleted: %1
goto :EOF
REM Parse input arguments
set RECURSIVE=false
set DELETE_FILES=false
set TARGET=
if "%1"=="" (
echo Usage: secure_delete.bat [path_to_file_or_directory] [-R] [-D]
echo -R: Recursively overwrite files in directories
echo -D: Securely delete files after overwriting
exit /b 1
)
set TARGET=%1
REM Handle options
shift
:ProcessOptions
if "%1"=="" goto :StartProcessing
if /I "%1"=="-R" (
set RECURSIVE=true
) else if /I "%1"=="-D" (
set DELETE_FILES=true
)
shift
goto :ProcessOptions
:StartProcessing
REM If recursive option is enabled and target is a directory
if "%RECURSIVE%"=="true" (
if exist "%TARGET%" (
for /r "%TARGET%" %%F in (*) do (
call :SecureOverwriteFile "%%F"
if "%DELETE_FILES%"=="true" (
call :SecureDeleteFile "%%F"
)
)
echo All files in directory %TARGET% have been securely processed.
) else (
echo Directory does not exist: %TARGET%
)
) else (
REM If target is a single file
if exist "%TARGET%" (
call :SecureOverwriteFile "%TARGET%"
if "%DELETE_FILES%"=="true" (
call :SecureDeleteFile "%TARGET%"
)
) else (
echo File does not exist: %TARGET%
)
)
endlocal
exit /b 0

Secure Delete Script

The secure_delete.bat bash script is designed to securely overwrite and optionally delete files on Windows, ensuring that the files cannot be easily recovered using basic recovery tools. It can handle all file types, including binary files such as images, videos, documents, and more.

Features:

File Overwriting: The script overwrites file contents with random data and zeros, making it difficult to recover the original data.

Supports All File Types: It securely processes binary files (images, videos, etc.) and text-based files alike.

Recursive Operation: When using the -R option, the script traverses directories and securely processes all files.

Optional File Deletion: If the -D option is provided, files are deleted after being securely overwritten.

Usage:

To securely overwrite a single file without deletion:

secure_delete.bat C:\path\to\file.txt

To securely overwrite and delete a single file:

secure_delete.bat C:\path\to\file.txt -D

To securely overwrite all files in a directory recursively, without deletion:

secure_delete.bat C:\path\to\directory -R

To securely overwrite and delete all files in a directory recursively:

secure_delete.bat C:\path\to\directory -R -D

Notes:

  • The -D option controls whether the file is deleted after being securely overwritten.
  • The -R option ensures the script runs recursively on all files in a directory.

Always be cautious when using this script as it permanently overwrites and deletes files, making recovery difficult or impossible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment