1
0
Fork 1

Compare commits

..

1 Commits
main ... main

Author SHA1 Message Date
Your Name 585f477db1 Initial release: Disk space reporting tool for Ubuntu/Linux systems 2025-06-07 19:30:52 -04:00
4 changed files with 305 additions and 94 deletions

114
README.md
View File

@ -1,44 +1,106 @@
# Swap File Creation Script
# Disk Space Report Script
This script automatically creates a swap file on your system, allowing you to increase the available virtual memory. It calculates the optimal swap file size based on the available disk space, ensuring that the swap file does not exceed 30GB or 5% of the free disk space, whichever is smaller.
A comprehensive disk space analysis tool for Ubuntu/Linux systems that helps identify storage usage patterns and suggests cleanup opportunities.
## Features
- Detects the main disk partition used by the system.
- Calculates the optimal swap file size (up to 30GB or 5% of the free space).
- Creates and activates a swap file at `/swapfile`.
- Optionally persists the swap file across reboots using the `--persist` flag.
- Displays the status of the created swap file.
- **APT Cache Analysis**: Estimates size of package cache that can be cleared
- **Package Management**: Identifies old installed packages taking up space
- **Journal Log Analysis**: Checks system journal logs for cleanup opportunities
- **Temporary Files Check**: Analyzes temporary directories for cleanup
- **Docker Volume Analysis**: Identifies unused Docker volumes consuming space
- **Docker Log Management**: Finds large Docker log files and suggests rotation
- **Large Directory Discovery**: Lists directories consuming significant space
- **Automatic Report Upload**: Uploads results to Hastebin for easy sharing
## Requirements
- Ubuntu/Linux system
- `sudo` privileges for comprehensive analysis
- `docker` (optional, for Docker-related analysis)
- `jq` for JSON parsing (for Hastebin upload)
- `curl` for report uploading
## Installation
```bash
# Clone the repository
git clone https://github.com/yourusername/disk-space-report.git
cd disk-space-report
# Make the script executable
chmod +x disk-check.sh
```
## Usage
To use this script, you can execute it directly from the URL without downloading it.
### Execute Directly from URL
Run the script directly from the URL using `curl` and piping to `bash`:
```bash
curl -sSL https://git.nixc.us/colin/swap-increase/raw/branch/main/swap.sh | bash
./disk-check.sh
```
### Persist the Swap File Across Reboots
The script will:
1. Analyze various system components for space usage
2. Generate a comprehensive report
3. Save the report to `/tmp/disk_space_report.log`
4. Upload the report to Hastebin and provide a shareable URL
To ensure the swap file is persisted across reboots, run the script with the `--persist` flag:
## What It Analyzes
```bash
curl -sSL https://git.nixc.us/colin/swap-increase/raw/branch/main/swap.sh | bash -s -- --persist
### System Components
- **APT Cache** (`/var/cache/apt`): Downloaded package files
- **Package Lists** (`/var/lib/apt/lists`): Package index files
- **Journal Logs**: System logging data
- **Temporary Files** (`/tmp`, `/var/tmp`): Temporary system files
### Docker Components (if Docker is installed)
- **Unused Volumes**: Dangling Docker volumes
- **Large Log Files**: Docker container logs over 1GB
### Directory Analysis
- **Large Directories**: Directories consuming more than 5GB and 10GB
- **Home Directory Analysis**: Large directories in user home folders
## Sample Output
The script provides estimates like:
```
Estimating the size of the apt cache:
1.2G /var/cache/apt
Estimating the size of journal logs:
Archived and active journals take up 2.1G in the file system.
Directories in / consuming more than 5GB:
7.2G /var/lib/docker
12G /usr/share
```
## Output
## Cleanup Suggestions
The script will provide the following output:
Based on the analysis, you can:
- The calculated size of the swap file.
- Confirmation of swap file creation and activation.
- Whether the swap file will persist after reboot (based on the `--persist` flag).
- Information on the current swap status using `swapon --show`.
1. **Clear APT cache**: `sudo apt clean`
2. **Remove old packages**: `sudo apt autoremove`
3. **Clean journal logs**: `sudo journalctl --vacuum-time=30d`
4. **Clear temporary files**: `sudo rm -rf /tmp/*` (use with caution)
5. **Remove unused Docker volumes**: `docker volume prune`
6. **Truncate Docker logs**: Use the provided command in the script output
## Note
## macOS Version
This script requires `sudo` privileges to create and activate the swap file, as well as to modify the `/etc/fstab` file for persistence across reboots when using the `--persist` flag.
For macOS users, there's also a `disk-check-macos.sh` version that provides similar functionality adapted for macOS systems.
## Safety Notes
- This script only **analyzes** disk usage - it doesn't delete anything
- Always review suggested cleanup commands before running them
- Some cleanup operations cannot be undone
- The script requires `sudo` for comprehensive system analysis
## Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
## License
This project is licensed under the MIT License.

108
disk-check-macos.sh Executable file
View File

@ -0,0 +1,108 @@
#!/bin/bash
set -x # debug mode
# Create a temporary file to store the report
TEMP_FILE=$(mktemp)
# Write report header
echo "macOS Fast Disk Usage Analysis" >> "$TEMP_FILE"
echo "Generated on $(date)" >> "$TEMP_FILE"
echo "Finding where your 256GB is actually going..." >> "$TEMP_FILE"
echo "=============================================" >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# Overall disk usage
echo "OVERALL DISK USAGE:" >> "$TEMP_FILE"
df -h / >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# Quick size check of major directories (no sudo needed)
echo "MAJOR DIRECTORY SIZES (accessible without sudo):" >> "$TEMP_FILE"
echo "Applications: $(du -sh /Applications 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo "Users: $(du -sh /Users 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo "Your Home: $(du -sh "$HOME" 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# User directories breakdown
echo "YOUR HOME DIRECTORY BREAKDOWN:" >> "$TEMP_FILE"
du -sh "$HOME"/* 2>/dev/null | sort -hr >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# User Library breakdown (often huge)
echo "YOUR LIBRARY DIRECTORY - often contains huge files:" >> "$TEMP_FILE"
du -sh "$HOME/Library"/* 2>/dev/null | sort -hr | head -n 15 >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# Application sizes
echo "INSTALLED APPLICATIONS - largest first:" >> "$TEMP_FILE"
du -sh /Applications/* 2>/dev/null | sort -hr | head -n 15 >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# Time Machine local snapshots (major space hog)
echo "TIME MACHINE LOCAL SNAPSHOTS - can be huge:" >> "$TEMP_FILE"
snapshots=$(tmutil listlocalsnapshots / 2>/dev/null | wc -l | tr -d ' ')
echo "Number of local snapshots: $snapshots" >> "$TEMP_FILE"
if [ "$snapshots" -gt 0 ]; then
echo "Recent snapshots:" >> "$TEMP_FILE"
tmutil listlocalsnapshots / 2>/dev/null | head -n 5 >> "$TEMP_FILE"
fi
echo >> "$TEMP_FILE"
# Large files in user area
echo "LARGEST FILES IN YOUR HOME - over 500MB:" >> "$TEMP_FILE"
find "$HOME" -type f -size +500M 2>/dev/null | head -n 10 | xargs ls -lh 2>/dev/null | awk '{print $5 "\t" $9}' >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# Common space hogs
echo "COMMON SPACE HOGS:" >> "$TEMP_FILE"
echo "Downloads folder: $(du -sh "$HOME/Downloads" 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo "Desktop: $(du -sh "$HOME/Desktop" 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo "Documents: $(du -sh "$HOME/Documents" 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo "Movies: $(du -sh "$HOME/Movies" 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo "Pictures: $(du -sh "$HOME/Pictures" 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo "Music: $(du -sh "$HOME/Music" 2>/dev/null | cut -f1)" >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# Development files that can be safely removed
echo "DEVELOPMENT FILES - safe to remove, will rebuild:" >> "$TEMP_FILE"
echo "node_modules directories:" >> "$TEMP_FILE"
find "$HOME" -name "node_modules" -type d 2>/dev/null | xargs du -sh 2>/dev/null | sort -hr | head -n 10 >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# Xcode files
echo "XCODE FILES - safe to delete, will regenerate:" >> "$TEMP_FILE"
if [ -d "$HOME/Library/Developer/Xcode/DerivedData" ]; then
echo "Xcode DerivedData: $(du -sh "$HOME/Library/Developer/Xcode/DerivedData" | cut -f1)" >> "$TEMP_FILE"
fi
if [ -d "$HOME/Library/Developer/CoreSimulator" ]; then
echo "iOS Simulators: $(du -sh "$HOME/Library/Developer/CoreSimulator" | cut -f1)" >> "$TEMP_FILE"
fi
echo >> "$TEMP_FILE"
# Caches
echo "CACHE DIRECTORIES - safe to clear:" >> "$TEMP_FILE"
du -sh "$HOME/Library/Caches"/* 2>/dev/null | sort -hr | head -n 10 >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# System info
echo "SYSTEM SPACE ESTIMATE:" >> "$TEMP_FILE"
echo "To get exact system usage, run: sudo du -sh /System /Library /private 2>/dev/null" >> "$TEMP_FILE"
echo >> "$TEMP_FILE"
# Post the report to Hastebin
RESPONSE=$(curl -s -X POST -d "$(cat "$TEMP_FILE")" "https://haste.nixc.us/documents")
# Extract the key from the Hastebin response
KEY=$(echo "$RESPONSE" | grep -o '"key":"[^"]*"' | cut -d'"' -f4)
if [ -n "$KEY" ]; then
echo "Fast disk usage report uploaded to Hastebin!"
echo "See where your 256GB is going: https://haste.nixc.us/$KEY"
else
echo "Error: Failed to upload report to Hastebin."
echo "Local disk usage report:"
cat "$TEMP_FILE"
fi
# Clean up the temporary file
rm -f "$TEMP_FILE"

109
disk-check.sh Normal file
View File

@ -0,0 +1,109 @@
#!/bin/bash
LOG_FILE="/tmp/disk_space_report.log"
HASTE_URL="https://haste.nixc.us/documents"
# Clear the log file if it exists
> "$LOG_FILE"
echo "Starting disk space report... This may take a few minutes." | tee -a "$LOG_FILE"
# Function to estimate the size of the apt cache
estimate_apt_cache_size() {
echo "Estimating the size of the apt cache:" | tee -a "$LOG_FILE"
sudo du -sh /var/cache/apt | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
}
# Function to estimate the size of old installed packages
estimate_old_packages_size() {
echo "Estimating the size of old installed packages:" | tee -a "$LOG_FILE"
sudo du -sh /var/lib/apt/lists /var/lib/apt/lists/partial | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
}
# Function to estimate the size of journal logs
estimate_journal_size() {
echo "Estimating the size of journal logs:" | tee -a "$LOG_FILE"
sudo journalctl --disk-usage | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
}
# Function to estimate the size of temporary files
estimate_tmp_size() {
echo "Estimating the size of temporary files:" | tee -a "$LOG_FILE"
sudo du -sh /tmp /var/tmp | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
}
# Function to estimate the size of unused Docker volumes
estimate_docker_volumes_size() {
echo "Estimating the size of unused Docker volumes:" | tee -a "$LOG_FILE"
docker volume ls -qf dangling=true | xargs -I {} docker volume inspect --format '{{ .Mountpoint }}' {} | xargs -I {} sudo du -sh {} | awk '{ sum += $1 } END { print sum "B" }' | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
}
# Function to check and suggest logrotate for large Docker logs
check_docker_logs() {
echo "Checking Docker logs for large files..." | tee -a "$LOG_FILE"
large_logs=$(docker ps -q --filter "status=exited" | xargs -I {} docker inspect --format '{{.LogPath}}' {} | xargs -I {} sudo find {} -type f -size +1G)
if [ -n "$large_logs" ]; then
echo "The following Docker logs are larger than 1GB:" | tee -a "$LOG_FILE"
echo "$large_logs" | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
echo "Consider setting up logrotate to manage Docker logs." | tee -a "$LOG_FILE"
echo "To truncate all Docker logs, run:" | tee -a "$LOG_FILE"
echo 'sudo find /var/lib/docker/containers/ -type f -name "*.log" -exec truncate -s 0 {} \;' | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
else
echo "No large Docker logs found." | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
fi
}
# Function to list directories consuming more than a specified size
list_large_directories() {
local directory=$1
local size_limit=$2
echo "Directories in $directory consuming more than ${size_limit}GB:" | tee -a "$LOG_FILE"
sudo du -ahx "$directory" 2>/dev/null | awk -v limit="$size_limit" '{
size=$1; unit=substr(size, length(size));
size_val=substr(size, 1, length(size)-1);
if ((unit=="G" && size_val+0 > limit) || (unit=="T" && size_val*1024 > limit)) {
print
}
}' | tee -a "$LOG_FILE"
echo | tee -a "$LOG_FILE"
}
# Estimate storage savings
echo "Estimating potential storage savings..." | tee -a "$LOG_FILE"
estimate_apt_cache_size
estimate_old_packages_size
estimate_journal_size
estimate_tmp_size
estimate_docker_volumes_size
# Check Docker logs
check_docker_logs
# List large directories
echo "Listing directories consuming more than 5GB and 10GB:" | tee -a "$LOG_FILE"
list_large_directories / 5
list_large_directories /home 5
list_large_directories / 10
list_large_directories /home 10
echo "Storage savings estimation and large directory listing completed." | tee -a "$LOG_FILE"
# Upload the log file to Hastebin
echo "Uploading report to Hastebin..." | tee -a "$LOG_FILE"
response=$(curl -s -X POST -T "$LOG_FILE" "$HASTE_URL")
if [[ $response == *"key"* ]]; then
key=$(echo "$response" | jq -r '.key')
# Remove /documents/ and use the key to form the correct URL
report_url="https://haste.nixc.us/$key"
echo "Report available at: $report_url" | tee -a "$LOG_FILE"
else
echo "Failed to upload report to Hastebin. Response: $response" | tee -a "$LOG_FILE"
fi

68
swap.sh
View File

@ -1,68 +0,0 @@
#!/bin/bash
# Function to create swap file
create_swapfile() {
# Detect the partition mounted on root ('/')
mount_point=$(df / | tail -1 | awk '{print $6}')
# Get available space on the root partition in MB
available_space=$(df -BM $mount_point | tail -1 | awk '{print $4}' | sed 's/M//')
# Calculate 5% of the available space in MB
swap_size=$((available_space * 5 / 100)) # 5% of available space in MB
# Convert swap size from MB to GB
swap_size=$((swap_size / 1024)) # Convert MB to GB
# Set upper limit of 30GB for the swap file
if [ $swap_size -gt 30 ]; then
swap_size=30
fi
# Abort if the calculated swap size is less than 1GB
if [ $swap_size -lt 1 ]; then
echo "Not enough disk space to create a swap file."
exit 1
fi
echo "Creating a ${swap_size}GB swap file at /swapfile..."
# Remove existing swapfile if it exists
if [ -f /swapfile ]; then
sudo swapoff /swapfile
sudo rm /swapfile
echo "Existing swapfile removed."
fi
# Create the swap file
sudo fallocate -l ${swap_size}G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "Swap file of ${swap_size}GB created and activated."
}
# Function to persist the swap file across reboots
persist_swapfile() {
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
echo "Swap file added to /etc/fstab."
}
# Check if --persist flag is passed
persist=false
if [ "$1" == "--persist" ]; then
persist=true
fi
# Main script execution
create_swapfile
if [ "$persist" = true ]; then
persist_swapfile
else
echo "Swap file will not persist after reboot."
fi
# Show current swap status
sudo swapon --show