Last active
February 26, 2026 14:43
-
-
Save TipsyTheCat/8c2fa31dc582f858088e3ffa6d93fc9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/bash | |
| # this script will check and defrag a specified partition via user input on the vast majority of unix-like systems | |
| # it can be ran on a mounted partition or an unmounted one | |
| # warn and inform user | |
| echo "$(tput bold)WARNING!$(tput sgr0)" | |
| echo "inputting the wrong mount point or partition/device path could harm your system!" | |
| echo "To know the mount point of a partition or the device path of a mounted partition" | |
| echo "run the \"mount\" command and look for your partition. or on some desktop environments" | |
| echo "open the context menu on your mounted partition in your file manager." | |
| echo " " | |
| # get partition | |
| read -p "Enter partition you wish to defrag (eg. sda1): /dev/" devDiskPartDirty | |
| devDiskPartLower=$(tr '[:upper:]' '[:lower:]' <<< $devDiskPartDirty) | |
| devDiskPart=$(tr -d ' ' <<< $devDiskPartLower) | |
| # get mount point | |
| read -r -p "Enter the current mount point for this partition (eg. /run/media/user/coolDrive/): " mountPoint | |
| sudo umount "$mountPoint" | |
| # sanity check | |
| sudo fsck.ext4 -y -f /dev/$devDiskPart | |
| read -p "$(tput bold)Sanity check!$(tput sgr0) is the drive fucked? no? then press [Enter] to start defrag :P" | |
| # start optimize and defrag of HDDGames drive | |
| sudo fsck.ext4 -y -f -D /dev/$devDiskPart | |
| # proper defrag | |
| sudo mount /dev/$devDiskPart "$mountPoint" | |
| sudo e4defrag /dev/$devDiskPart | |
| # show current fragmentation stats and remount drive | |
| sudo umount "$mountPoint" | |
| echo "current fragmentation stats:" | |
| sudo fsck.ext4 -y -f /dev/$devDiskPart | |
| sudo mount /dev/$devDiskPart "$mountPoint" | |
| echo "DONE ^w^" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment