badpart_safe.sh is a destructive Linux shell script that scans an entire disk with badblocks, isolates defective areas with a safety margin, creates partitions only in the remaining good regions, and formats them as NTFS using quick format mode (mkfs.ntfs -Q). The script is intended for temporary or low-trust reuse of problematic HDDs, not for reliable long-term storage.
The script performs these steps automatically:
- Reads the target disk size and basic SMART data when
smartctlis available. - Runs a full read-only scan with
badblocks -sv, showing native progress percentage in the terminal. - Writes only discovered bad block numbers to a text file via
badblocks -o. - Groups adjacent bad blocks into bad regions and adds a 500 MiB safety offset around each region.
- Merges excluded regions that are closer than 20 GiB.
- Builds a GPT partition layout only from the remaining good regions using
parted. - Creates the partitions and formats them with NTFS quick format using
mkfs.ntfs -Q.
This script destroys all existing data on the target disk.
Use it only if the entire disk may be repartitioned and reformatted. The user is fully responsible for selecting the correct disk device, for example /dev/sda or /dev/sdb. Running the script against the wrong disk will erase that disk's partition table and filesystems.
No warranty is provided. No responsibility is accepted for data loss, filesystem corruption, hardware failure, target disk misidentification, or any other consequence of running this script.
This script is suitable for:
- Temporary transfer disks.
- Low-value scratch storage.
- Testing disks with known media defects.
- Isolating bad areas on large HDDs for short-term reuse.
This script is not suitable for:
- Backups.
- Long-term storage.
- Important personal or business data.
- Production systems.
- RAID / NAS use.
Disks that already show pending sectors, uncorrectable sectors, or repeated SMART read failures should still be considered unreliable even if this script completes successfully.
The script expects the following tools to be available on the Linux system:
bashbadblocksblockdevlsblkawksedsortpartedteestdbufdatepartprobemkfs.ntfsormkntfs
Optional but recommended:
smartctlfromsmartmontoolsfor a quick SMART summary before scanning.udevadmto help detect newly created partition nodes after repartitioning.
Run locally:
sudo bash badpart_safe.sh /dev/sdXExample:
sudo bash badpart_safe.sh /dev/sdbThe script will automatically:
- Scan the entire disk.
- Compute bad regions.
- Create a GPT layout from safe regions only.
- Format resulting partitions as NTFS with quick format.
Each run creates a work directory in /tmp/, for example:
/tmp/badpart-sdb-20260527-070000-12345/
Typical files inside that directory:
badpart.logbadblocks.txtexcluded_ranges_mib.txtgood_ranges_mib.txtparted_commands.txt
To watch the live log:
tail -f /tmp/badpart-*/badpart.logOr for the newest run:
tail -f "$(ls -1dt /tmp/badpart-* | head -1)/badpart.log"- Every discovered bad region receives an additional 500 MiB exclusion margin on both sides.
- If two excluded regions are less than 20 GiB apart, they are merged into one larger excluded region.
- Partitions are created only from the good gaps left between excluded regions.
- If no bad blocks are found, the script creates one full-disk NTFS partition.
This behavior is intentionally conservative so unstable areas are not used too closely.
Review the script before running it. Piping remote shell code directly into bash is convenient but risky.
Recommended two-step method:
curl -fsSL https://raw.githubusercontent.com/tehnium/bad-disk-partitioner/main/badpart_safe.sh -o badpart_safe.sh
chmod +x badpart_safe.sh
sudo ./badpart_safe.sh /dev/sdXDirect one-liner:
curl -fsSL https://raw.githubusercontent.com/tehnium/bad-disk-partitioner/main/badpart_safe.sh | sudo bash -s -- /dev/sdX- The scan can take many hours on multi-terabyte HDDs.
badblocks.txtmay remain empty until actual bad blocks are found.- NTFS quick format (
-Q) is used to avoid another long full initialization pass. - Avoid USB disconnects, cable movement, or power loss during scanning and formatting.
Use at your own risk. The operator must understand that the whole selected disk will be scanned, repartitioned, and reformatted.