You are not logged in.

#1 2009-11-18 05:55:38

overlord_manny
Member
Registered: 2009-10-16
Posts: 11

Interact with trashcan through cli easier (.bashrc)

Here's a snippit of changes I made in my .bashrc to make interacting with the trashcan through the cli a bit easier. Pretty simple stuff but I figured it may be of use to other noobies.

Just put this before the alias settings in your ~/.bashrc

## Location of trash folder
TRASHLOC="$HOME/.local/share/Trash/files"

function moveToTrash() {
# Test if the command was used with out an argument
    if [ "$1" != "" ]; then
        if [ -d "$TRASHLOC/$1" ]; then
# If target file already exists in trash remove it first
            rm -r $TRASHLOC/$1
        fi

        mv -v --target-directory=$TRASHLOC/ -- "$@"
    else
# If no argument (file to delete) was used exit
        echo "No files selected - exiting without change"
    fi
}

function emptyTrash() {
# Lists trash contents
    ls --color=auto "$TRASHLOC/"
    read -p "Are you sure you want to empty the trash (y/n/i)?"
# Answer "y" - empties trash and gives verbose notice
    if [ "$REPLY" == "y" ]; then
        rm -rfv $TRASHLOC/*
# Answer "i" - provides interactive removal of files
    elif [ "$REPLY" == "i" ]; then
        rm -rfvi $TRASHLOC/*
# All other responses exit without a change
    else
        echo "No changes made"
    fi        
}

alias trashcan='ls --color=auto $TRASHLOC/'
alias mtt=moveToTrash
alias mttrash=emptyTrash

Commands:
  trashcan - will list the contents currently in the trashcan
  mtt [filename1 filename2 ... ] - moves files to trashcan
  mttrash - brings up a an option to either:
    "y" = delete all files in trashcan
    "i" = interactively remove individual files
    "n" = exit without change (any response besides "y" or "i" will exit like this.)

If you don't like the commands I use change the command between the word "alias" and the "=".

**If your trash folder is not located at "$HOME/.local/share/Trash/files" then edit the TRASHLOC variable at the top to reflect your setup.

Don't forget to run "source ~/.bashrc" in the terminal or logout/in for changes to be used.

Hope this can help someone. Happy coding!

**edit - Fixed an error that would delete the trash folder if no files to delete were stated after "mtt" command.

Last edited by overlord_manny (2009-11-20 03:37:30)

Offline

Board footer

Powered by FluxBB