You are Here:
Linux Lite 5.2 RC1 is now available for download and testing



Custom Command Line Recycle Bin

Author (Read 7467 times)

0 Members and 1 Guest are viewing this topic.

Custom Command Line Recycle Bin
« on: September 30, 2019, 08:47:33 PM »
 

jack action

  • New to Forums
  • *
  • 13
    Posts
  • Country: 00
  • Reputation: 4
  • Linux Lite Member

  • Linux Lite: 5.0 64bit

  • Kernel: 5.x
I've made a script to put a file or folder into a recycle bin instead of a deleting it with rm (which could be problematic if you make an error).

In a file called usr/bin/trash, I've put the following:

Code: [Select]
#!/bin/bash

# move a file or folder to a __trash__ folder (within the same directory)

if [ "$1" = "" ]; then
echo "You need a file or folder name.";
exit 1;
elif [ "$1" = "/" ]; then
echo 'You cannot put "/" in trash.';
exit 1;
fi

trashParentDir="$(dirname $1)";

mkdir -p "$trashParentDir/__trash__";
mv -vft "$trashParentDir/__trash__" "$1";
exit 0;

Then I created 2 aliases:

Code: [Select]
alias rm="trash"
alias empty-trash="sudo find / -depth -type d -name \"__trash__\" -exec rm -r '{}' ';'"

When using rm, you actually put a file or folder into a __trash__ folder within the same directory. (Note: it will overwrite a pre-existing file of the same name previously 'deleted')
In case of a mistake, just move the file out of the __trash__ folder.

When using empty-trash, you delete all __trash__ folders on the computer.  You need root privilege to do so.
You could make a Cron job to empty-trash regularly.
 


Re: Custom Command Line Recycle Bin
« Reply #1 on: September 14, 2020, 09:25:05 AM »
 

Marmaduke

  • New to Forums
  • *
  • 5
    Posts
  • Country: gb
  • Reputation: 1
  • Linux Lite Member

  • Linux Lite: 5.0 64bit

  • CPU: I7 -7700hq

  • MEMORY: 32Gb

  • VIDEO CARD: GTX 1050
I like the look of this.

I'm an user of rm and always have to stop myself from just banging to command out to makesure I don't do anything stupid.
 


Tags:
 


Linux Lite 5.2 RC1 is now available for download and testing