How to automatically and permanently remove unused content from Manjaro Linux trash?

I want to automatically and permanently remove files and folders from the trash in Manjaro Linux that have not been modified for a specified number of days, while preserving folders containing recently modified subfolders or files.

Based on this Unix StackExchange answer I’ve thought of using a combination of the find command on a script and a cron job.

Here’s a script that I’ve wrote as the file $HOME/bin/clean_trash.sh and made it executable with chmod +x clean_trash.sh:

#!/bin/bash

# Set the trash directory
TRASH_DIR="$HOME/.local/share/Trash"

# Set the number of days after which files/folders should be deleted
DAYS_TO_KEEP=30

# Find and delete files/folders older than DAYS_TO_KEEP, excluding directories with recently modified content
find "$TRASH_DIR" -mindepth 1 -maxdepth 1 -type d -mtime +$DAYS_TO_KEEP -exec bash -c '
  for entry; do
    if ! find "$entry" -mindepth 2 -mtime -'"$DAYS_TO_KEEP"' -print -quit | grep -q .; then
      rm -rf "$entry"
    fi
  done
' _ {} +

I can then run the script manually from anywhere if the bin directory is in the $PATH or set up a cron job to run it automatically at a specified interval.

To set up a cron job, I’ve opened the crontab editor with crontab -e and added a line like the following:

@daily $HOME/bin/clean_trash.sh

This will run the clean_trash.sh script daily.

I expect the script to remove files and folders from the trash directory ($HOME/.local/share/Trash) that are older than 30 days (DAYS_TO_KEEP=30), while preserving folders that contain files or subfolders modified within the last 30 days.

I just want to make sure the script does what I expect. Thanks in advance for your help.

This is my script utilizing trash-cli 1:

❯ cat ~/bin/empty-trash.sh
#!/bin/sh

# https://github.com/andreafrancia/trash-cli
trash-empty 7

Just change the value to 30 instead of 7 to match if you wish.

Why not create a systemd user service instead?


1 Note that I have ~/bin/ in my $PATH set in my ~/.profile:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ]; then
  PATH="$HOME/bin:$PATH"
fi
2 Likes

With Plasma you can set a date to delete files from the trash.

Install trash-cli the command i use is trash-empty -f ,then set up an autostart program to call the script, then it works at startup.

What is the -f flag supposed to do? When I write man trash-empty I see that the only argument available seems to be the number of days.

ARGUMENTS
       To remove all trashed files, use 'trash-empty' without arguments.

       To remove files that have been in the trash more than a given number of
              days, use 'trash-empty x', 'x' representing the number of days.

I didn’t know about the benefits of using systemd user service and cron job was the first one that popped into my head.

Using a systemd user service instead of a cron job for this task can provide several benefits:

  1. More control: With systemd user services, you have finer control over the service’s behavior, such as specifying dependencies, configuring environment variables, and managing the service’s lifecycle (start, stop, restart, etc.).

  2. Event-driven: Systemd user services can be triggered by events (e.g., system startup, user login, etc.) rather than being limited to time-based scheduling like cron jobs.

  3. Logging: Systemd user services have integrated logging mechanisms, making it easier to monitor and troubleshoot issues compared to cron job output, which is typically sent to the user’s email or a log file.

  4. Resource management: Systemd user services can be configured to manage resources more effectively, such as setting CPU and memory limits, enforcing security policies, and sandboxing.

  5. Parallelization: Systemd user services can be parallelized and synchronized more efficiently than cron jobs, which can be beneficial if you have multiple tasks that depend on each other.

  6. Integration: Systemd user services integrate well with other systemd components, such as systemd-tmpfiles, systemd-timers, and systemd-journald, providing a more cohesive and unified system management experience.

  7. Cron job limitations: Cron jobs have some limitations, such as requiring a running cron daemon, being limited to time-based scheduling, and having less flexibility in handling system events.

That being said, if your use case is straightforward and doesn’t require advanced features or integration with other systemd components, a cron job can still be a viable option for its simplicity.

To set up a systemd user service for your trash cleaning script, you can create a new service file in ~/.config/systemd/user/ directory (create it if it doesn’t exist). For example, ~/.config/systemd/user/clean-trash.service with the following content:

[Unit]
Description=Clean Trash Older Than 30 Days

[Service]
ExecStart=/path/to/clean_trash.sh

[Install]
WantedBy=default.target

Replace /path/to/clean_trash.sh with the actual path to your clean_trash.sh script. Then, you can enable the service with systemctl --user enable clean-trash.service and start it with systemctl --user start clean-trash.service.