Skip to content

hymkor/trash-go

Repository files navigation

trash-go

Go Reference

trash-go is a Go library and command-line tool for moving specified files to the trash (Recycle Bin on Windows, Trash Can on Linux desktop environments).

Installation

As a Library

go get github.com/hymkor/trash-go

As a Command-Line Tool

go install github.com/hymkor/trash-go/cmd/trash@latest

This will install a trash executable in your $GOBIN directory.

Usage

Library

package trash // import "github.com/hymkor/trash-go"

func Throw(filenames ...string) error

Throw accepts one or more file paths and moves them to the trash.

Command-Line Tool

trash [OPTIONS] FILE...

Moves the specified files to the trash.

Options

  • -from-file FILENAME Read a list of files (one per line) from the given file instead of command-line arguments. Use - as the filename to read from standard input.

Examples

trash *.log                 # Move matching files to trash
trash -from-file list.txt   # Move files listed in list.txt
find . -name "*.tmp" | trash -from-file -  # Use with standard input

Example (as library)

See example.go for a complete usage example.

package main

import (
    "fmt"
    "os"
    "path/filepath"

    "github.com/hymkor/trash-go"
)

func mains(args []string) error {
    var filenames []string
    for _, arg := range args {
        if matches, err := filepath.Glob(arg); err != nil {
            filenames = append(filenames, arg)
        } else if len(matches) == 0 {
            return fmt.Errorf("%s: %w", arg, os.ErrNotExist)
        } else {
            filenames = append(filenames, matches...)
        }
    }
    return trash.Throw(filenames...)
}

func main() {
    if err := mains(os.Args[1:]); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

Notes

  • On Windows, files are moved to the Recycle Bin using native shell operations.
  • On Linux, the implementation creates .Trash or .local/share/Trash directories if they don't exist, following the FreeDesktop specification.

See also

Author

hymkor (HAYAMA Kaoru)

License

MIT License

About

The trash-go is the library for golang to move specified files to trashbox

Topics

Resources

License

Stars

Watchers

Forks