Skip to content

Latest commit

 

History

History
166 lines (126 loc) · 7.39 KB

CLEAN_SH_DOCS.md

File metadata and controls

166 lines (126 loc) · 7.39 KB

Script documentation for clean.sh

Index

Overview

The clean.sh script is designed to clean cache and temporary files for 42 students using Linux/Ubuntu. It helps to free up disk space and maintain system performance by removing unnecessary files from various directories. The script supports various modes including verbose, dry run, interactive, force, and safe modes.

Features

  • Clean cache and temporary files from multiple applications.
  • Interactive mode to confirm deletions.
  • Dry run mode to simulate the cleaning process without actually deleting any files.
  • Verbose mode to display detailed information about deleted files.
  • Safe mode to ensure safe operation when force mode is enabled.
  • Customizable paths to clean.

How to Use

Command-line Options

General:

  • -h, --help: Display the help message.
  • -u, --update: Update the script from the repository.
  • --color [true|1|false|0]: Enable or disable color output. Valid values are true, 1, false, 0.
  • --set-default-color [true|1|false|0]: Set the default color output in the configuration file. Valid values are true, 1, false, 0.

Operation Modes:

  • -v, --verbose: Verbose mode. Show files deleted/to delete and their sizes.
  • -n, --dry-run: Dry run mode. Only show what would be deleted without actually deleting anything. Enables verbose mode.
  • -i, --interactive: Interactive mode. Ask for confirmation before deleting each file or directory.
  • -l, --list: List mode. ONLY list all directories and files to be cleaned without deleting.
  • -f, --force: Force mode. Delete cache without asking for confirmation of running processes.
  • -s, --safe: Safe mode. Temporarily disables force mode and checks the running processes.

Default Configuration:

  • -D [mode]: Set default mode of the script to the provided mode (e.g., -D v to enable verbose mode by default).
  • -U [mode]: Unset default mode of the script for the provided mode (e.g., -U v to disable verbose mode by default).
  • -R: Reset default modes of the script to the original values.

Examples

  • To clean caches in verbose mode:

    clean -v
  • To perform a dry run (no files will be deleted):

    clean -n
  • To clean caches in interactive mode:

    clean -i
  • To list all directories and files to be cleaned without deleting:

    clean -l
  • To forcefully clean caches without confirmation:

    clean -f

Safe Mode Example

Safe mode is particularly useful if you have set force mode as the default in your configuration file but want to run the script while checking for running processes without having to change the default configuration. This can be achieved by using the -s option.

For example, if you want to run the cleaning script in safe mode to ensure it checks for running processes despite force mode being enabled by default, you can use:

clean -s

This command will temporarily disable force mode and check for running processes, ensuring safe operation for this execution.

Detailed Explanation of the Script

Initial Setup

  • Default Paths: The script starts by declaring an associative array DEF_PATHS_TO_CLEAN that holds the default paths to be cleaned along with their associated process names.

  • Colors and Formatting: Various color and bold formatting options are set up for better readability of the output.

  • Configuration: The script initializes several variables and loads default configuration values from clean.conf if it exists.

The default behavior of the script can be configured by editing the clean.conf file located in ~/.42cleaner/clean.conf. For more detailed information, refer to the CLEAN_CONF_DOCS.md

Functions

Function Name Description
update_script Updates the script from the repository.
update_config_file Updates the configuration file with new default values.
update_color_variables Updates color variables based on user configuration.
print_help Displays the help message with usage instructions and options.
get_size_color Determines the color based on the size of the files.
print_size_color Prints the size in a readable format with color.
get_storage_usage Gets the storage usage of the home directory in a readable format.
print_storage_usage Prints the storage usage of the home directory.
get_path_size Gets the size of a given path.
print_paths_sorted Sorts and prints an array of paths by their size from biggest to smallest.
sort_paths_by_size Sorts given array of paths by their size from biggest to smallest.
check_running_process Checks for running processes and handles them according to user input.
clean_paths Deletes files and folders and calculates the freed space.

Adding Custom Paths

You can add custom paths to the DEF_PATHS_TO_CLEAN array in the clean.sh script. This allows you to specify additional directories to be cleaned. Here’s how to add a custom path:

  1. Locate the DEF_PATHS_TO_CLEAN array:
declare -A DEF_PATHS_TO_CLEAN=(
  # (...)
  ["$HOME/.cache"]="none"
  ["$HOME/.var/app/com.google.Chrome/cache/"]="google-chrome"
  # Add more paths here
)
  1. Add your custom path(s):
declare -A DEF_PATHS_TO_CLEAN=(
  ["$HOME/custom/path"]="process-name"
)
  • Replace $HOME/custom/path with the actual path you want to clean.
  • Replace process-name with the name of the process that uses this path. If no specific process is associated, use "none".

How to Determine the Process Name of a Path

To find the process name associated with a specific path, you can use the process_name.sh script included in the utils folder of this repository. This script helps identify which processes are accessing files within a specified directory.

For detailed instructions on how to use the process_name.sh script, please refer to the PROCESS_NAME_DOCS.md documentation.

Using the Process Name in DEF_PATHS_TO_CLEAN

Once you have identified the process name using the process_name.sh script, you can add it to the DEF_PATHS_TO_CLEAN array in the clean.sh script.

Example:

declare -A DEF_PATHS_TO_CLEAN=(
  ["$HOME/.config/Code/"]="code"
  ["$HOME/.config/google-chrome/Default/Cache"]="chrome"
  # Add more paths here
)

Conclusion

By identifying the processes that are actively using specific directories, you can accurately configure clean.sh to manage cache and temporary files for a wider range of applications.