Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ so they're easy to run when needed.
- See [windows/README.md](windows/README.md) for Windows scripts.

For general usage instructions check [docs/README.md](docs/README.md).

For security guidelines see [docs/SECURITY.md](docs/SECURITY.md).
Troubleshooting tips are available in [docs/TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md).
To verify script syntax run `tests/run_tests.sh`.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ Example for Windows (run from PowerShell as Administrator):
cd windows\Winget
./WingetFix.ps1
```

For security information see [SECURITY.md](SECURITY.md). Troubleshooting tips are in [TROUBLESHOOTING.md](TROUBLESHOOTING.md).
6 changes: 6 additions & 0 deletions docs/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Security Considerations

- Validate all user input to avoid unintended behavior.
- Run scripts with the minimal required privileges.
- Review scripts before execution, especially those requiring administrator rights.
- Log actions to help with auditing and troubleshooting.
5 changes: 5 additions & 0 deletions docs/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Troubleshooting

- Ensure dependencies are installed (e.g., `bash`, `powershell`).
- Check log files mentioned in each script's README for errors.
- Run the tests in `tests/run_tests.sh` to verify syntax.
1 change: 1 addition & 0 deletions linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This folder contains shell scripts for common Linux maintenance tasks. Each scri
- `fix_dnf_lock/` - Clear DNF/Yum lock files.
- `fix_permissions/` - Fix ownership and permissions in the user's home directory.
- `network_restart/` - Restart a network interface.
- `system_health/` - Monitor basic CPU, memory, and disk usage.
- `maintenance/` - Comprehensive maintenance script with many features.

Each subdirectory contains its own `README.md` with detailed usage instructions.
2 changes: 2 additions & 0 deletions linux/fix_permissions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Corrects ownership and permission issues in the current user's home directory.
```bash
sudo ./fix_permissions.sh
```

Logs are written to `/var/log/fix_permissions.log`.
24 changes: 16 additions & 8 deletions linux/fix_permissions/fix_permissions.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#!/bin/bash
echo "Fixing ownership and permission issues in home directory..."
#!/usr/bin/env bash
set -euo pipefail

# Change ownership of all files in the user's home directory to the current user
sudo chown -R $USER:$USER /home/$USER
LOG_FILE="/var/log/fix_permissions.log"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') : $1" | tee -a "$LOG_FILE"
}

# Fix common permission issues
sudo find /home/$USER -type d -exec chmod 755 {} \;
sudo find /home/$USER -type f -exec chmod 644 {} \;
HOME_DIR="/home/$USER"
if [[ ! -d "$HOME_DIR" ]]; then
echo "Home directory $HOME_DIR not found" >&2
exit 1
fi

echo "Permissions and ownership fixed."
log "Fixing permissions in $HOME_DIR"
sudo chown -R "$USER":"$USER" "$HOME_DIR"
sudo find "$HOME_DIR" -type d -exec chmod 755 {} \;
sudo find "$HOME_DIR" -type f -exec chmod 644 {} \;
log "Permissions fixed"
6 changes: 4 additions & 2 deletions linux/network_restart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Brings a specified network interface down and back up.
## Usage

```bash
sudo ./network_restart.sh eth0
sudo ./network_restart.sh <interface>
```

Replace `eth0` with the actual interface name.
Replace `<interface>` with the actual network interface name (e.g., `eth0`, `enp0s3`). The script validates that the interface exists before attempting the restart.

Logs are written to `/var/log/network_restart.log`.
31 changes: 25 additions & 6 deletions linux/network_restart/network_restart.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
#!/bin/bash
echo "Restarting network interface..."
#!/usr/bin/env bash
# Safe network interface restart script
set -euo pipefail

# Replace 'eth0' with the name of your network interface
sudo ifconfig eth0 down
sudo ifconfig eth0 up
LOG_FILE="/var/log/network_restart.log"
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') : $1" | tee -a "$LOG_FILE"
}

echo "Network interface restarted."
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <interface>" >&2
exit 1
fi

iface="$1"
if ! ip link show "$iface" &>/dev/null; then
echo "Error: interface '$iface' not found" >&2
exit 1
fi

log "Restarting network interface '$iface'"
if sudo ip link set "$iface" down && sudo ip link set "$iface" up; then
log "Interface '$iface' restarted"
else
log "Failed to restart '$iface'"
exit 1
fi
11 changes: 11 additions & 0 deletions linux/system_health/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# System Health Monitor

This script collects basic CPU, memory, and disk usage statistics.

## Usage

```bash
sudo ./system_health.sh
```

Logs are stored in `/var/log/system_health.log`.
26 changes: 26 additions & 0 deletions linux/system_health/system_health.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Basic system health monitoring script
set -euo pipefail

LOG_FILE="/var/log/system_health.log"

log_message() {
local msg="$1"
echo "$(date '+%Y-%m-%d %H:%M:%S') : $msg" | tee -a "$LOG_FILE"
}

cpu_usage() {
top -bn1 | awk '/^%?Cpu/{print $2+$4}'
}

mem_usage() {
free -m | awk '/Mem:/ {printf("%.2f", $3/$2*100)}'
}

disk_usage() {
df -h / | awk 'NR==2 {print $5}'
}

log_message "CPU usage: $(cpu_usage)%"
log_message "Memory usage: $(mem_usage)%"
log_message "Disk usage: $(disk_usage)"
3 changes: 3 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Testing Scripts

Run `./run_tests.sh` from the `tests` directory to validate shell and PowerShell script syntax.
36 changes: 36 additions & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash
set -euo pipefail

DIR="$(cd "$(dirname "$0")"/.. && pwd)"

failed=0

for script in $(find "$DIR/linux" -name '*.sh'); do
echo "Checking $script"
if ! bash -n "$script"; then
echo "Shell syntax error in $script"
failed=1
fi
if command -v shellcheck >/dev/null 2>&1; then
shellcheck "$script" || failed=1
fi
done

if command -v pwsh >/dev/null 2>&1; then
for ps_script in $(find "$DIR/windows" -name '*.ps1'); do
echo "Checking $ps_script"
if ! pwsh -NoProfile -Command "[System.Management.Automation.PSParser]::Tokenize((Get-Content -Raw '$ps_script'), [ref]0) > \$null"; then
echo "PowerShell syntax error in $ps_script"
failed=1
fi
done
else
echo "pwsh not available; skipping PowerShell checks"
fi

if [[ $failed -eq 0 ]]; then
echo "All tests passed."
else
echo "Some tests failed." >&2
fi
exit $failed
33 changes: 29 additions & 4 deletions windows/Clean-Disk/Clean-Disk.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
param(
[string]$LogPath = "$env:TEMP\Clean-Disk.log",
[switch]$Confirm
)

Start-Transcript -Path $LogPath -Append | Out-Null
function Clean-Disk {
Write-Host "Cleaning up temporary files and system junk..." -ForegroundColor Cyan
Remove-Item -Path "$env:SystemRoot\Temp\*" -Force -Recurse
Remove-Item -Path "$env:SystemRoot\SoftwareDistribution\Download\*" -Force -Recurse
Remove-Item -Path "$env:USERPROFILE\AppData\Local\Temp\*" -Force -Recurse

$paths = @(
"$env:SystemRoot\Temp",
"$env:SystemRoot\SoftwareDistribution\Download",
"$env:USERPROFILE\AppData\Local\Temp"
)

foreach ($p in $paths) {
if (Test-Path $p) {
try {
Remove-Item -Path "$p\*" -Force -Recurse -ErrorAction Stop
} catch {
Write-Host "Failed to clean $p : $_" -ForegroundColor Red
}
}
}

Write-Host "Disk cleanup completed." -ForegroundColor Green
}

Clean-Disk
if ($Confirm) {
Clean-Disk
} else {
Write-Host "Run with -Confirm to perform disk cleanup." -ForegroundColor Yellow
}
Stop-Transcript | Out-Null
36 changes: 26 additions & 10 deletions windows/Clean-Disk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,37 @@ The `Clean-Disk` PowerShell script is designed to clean up temporary files and s
## Code Explanation

```powershell
param(
[switch]$Confirm
)

function Clean-Disk {
Write-Host "Cleaning up temporary files and system junk..." -ForegroundColor Cyan

# Remove system temporary files
Remove-Item -Path "$env:SystemRoot\Temp\*" -Force -Recurse

# Remove Windows Update cache files
Remove-Item -Path "$env:SystemRoot\SoftwareDistribution\Download\*" -Force -Recurse

# Remove user-specific temporary files
Remove-Item -Path "$env:USERPROFILE\AppData\Local\Temp\*" -Force -Recurse
$paths = @(
"$env:SystemRoot\Temp",
"$env:SystemRoot\SoftwareDistribution\Download",
"$env:USERPROFILE\AppData\Local\Temp"
)

foreach ($p in $paths) {
if (Test-Path $p) {
try {
Remove-Item -Path "$p\*" -Force -Recurse -ErrorAction Stop
} catch {
Write-Host "Failed to clean $p : $_" -ForegroundColor Red
}
}
}

Write-Host "Disk cleanup completed." -ForegroundColor Green
}

Clean-Disk
if ($Confirm) {
Clean-Disk
} else {
Write-Host "Run with -Confirm to perform disk cleanup." -ForegroundColor Yellow
}
```

- **Key Directories Cleaned**:
Expand All @@ -71,4 +86,5 @@ Clean-Disk

1. **Irreversible Deletion**: The script permanently deletes files in the specified directories. Ensure there are no important files in these locations before running the script.
2. **Targeted Cleanup**: The script is limited to standard temporary and cache directories, minimizing the risk of affecting critical system files.
3. **Recommended Usage**: Use this script periodically to maintain system cleanliness and performance.
3. **Recommended Usage**: Use this script periodically to maintain system cleanliness and performance.
Logs are stored in `$env:TEMP\Clean-Disk.log`.
12 changes: 12 additions & 0 deletions windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ This repository contains a collection of PowerShell scripts designed to simplify

---

### 11. **System Health Monitor**
- **Description**: Logs basic CPU, memory, and disk usage.
- **Key Features**:
- Uses built-in PowerShell commands to gather statistics.
- Saves output to a configurable log file.

### 12. **Registry Cleanup**
- **Description**: Removes invalid startup entries from the registry after creating a backup.
- **Key Features**:
- Backs up the HKCU hive before deleting entries.
- Requires the `-Confirm` flag to run.

## How to Use

1. Navigate to the subfolder containing the desired script.
Expand Down
12 changes: 12 additions & 0 deletions windows/Registry-Cleanup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Registry-Cleanup.ps1

Cleans invalid startup entries from the Windows registry.

## Usage

```powershell
./Registry-Cleanup.ps1 -Confirm
```

A backup of the HKCU hive is saved to `$env:TEMP\registry_backup.reg` before any changes are made.
Logs are written to `$env:TEMP\registry_cleanup.log`.
29 changes: 29 additions & 0 deletions windows/Registry-Cleanup/Registry-Cleanup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
param(
[string]$LogPath = "$env:TEMP\registry_cleanup.log",
[string]$BackupPath = "$env:TEMP\registry_backup.reg",
[switch]$Confirm
)

Start-Transcript -Path $LogPath -Append | Out-Null

if ($Confirm) {
reg export HKCU "$BackupPath" /y | Out-Null
$runPaths = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce"

foreach ($path in $runPaths) {
Get-ItemProperty $path | ForEach-Object {
$value = $_.PSChildName
$cmd = $_.$value
if ($cmd -and !(Test-Path ($cmd -split ' ')[0])) {
Write-Host "Removing invalid entry $value from $path" -ForegroundColor Yellow
Remove-ItemProperty -Path $path -Name $value
}
}
}
Write-Host "Registry cleanup completed. Backup stored at $BackupPath" -ForegroundColor Green
} else {
Write-Host "Run with -Confirm to perform registry cleanup." -ForegroundColor Yellow
}

Stop-Transcript | Out-Null
11 changes: 11 additions & 0 deletions windows/SystemHealth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SystemHealth.ps1

Checks basic system statistics and logs the output.

## Usage

```powershell
./SystemHealth.ps1
```

By default logs are stored in `$env:TEMP\system_health.log`.
11 changes: 11 additions & 0 deletions windows/SystemHealth/SystemHealth.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
param(
[string]$LogPath = "$env:TEMP\system_health.log"
)

Start-Transcript -Path $LogPath -Append | Out-Null

Write-Host "CPU Usage:" (Get-CimInstance -ClassName Win32_Processor | measure -Property LoadPercentage -Average | select -ExpandProperty Average) "%"
Write-Host "Memory Usage:" (Get-CimInstance -ClassName Win32_OperatingSystem | select -ExpandProperty FreePhysicalMemory) "KB free"
Write-Host "Disk Usage:" (Get-PSDrive -Name C | select -ExpandProperty Used) "bytes used"

Stop-Transcript | Out-Null