If we want to delete files/folders in Linux, we should use the command rm. However, the files/folders deleted in such a way are uncoverable. Therefore, it might be better (or safer) to create a trash bin like that in Windows or MacOS. When you delete a file, you actually move it to the trash bin. Then, you should be able to recover it as long as the trash bin is not cleared.

Solution

Open ~/.bashrc (or ~/.bash_profile for MacOS) and add the following codes in the end of the files:

mkdir -p ~/.Trash
mkdir -p ~/.Trash/.tmp
alias rm=del
del()
{
    dir=$(pwd)
    mv $@ ~/.Trash/.tmp
    cd ~/.Trash/.tmp
    for file in *
    do
        mv $file ../$(date +"%Y%m%d%H%M%S")_$file
    done
    cd $dir
}
cleardel()
{
    read -p "Clear Sure?[Input 'y' or 'Y' to confirm. && Input 'n' to cancel.]" confirm
    [ $confirm == 'y' ] || [ $confirm == 'Y' ] && /bin/rm -rf ~/.Trash/*
}

source ~/.bashrc

Now, when you delete a file by rm, you should actually move it to the ~/.Trash folder.

Explanation

The idea above can be implemented by the code shown in the following (from the reference in Chinese). Append these codes to your ~/.bashrc (Linux) or ~/.bash_profile (MacOS), you can in principle find your deleted files from ~/.trash.

mkdir -p ~/.trash   # create a .trash hidden folder at home directory
alias rm=del        # set `rm` as the alias of function `del`  
del()               # define function `del`
{  
  mv $@ ~/.trash/  # mv files/folders to ~/.trash
}  
# function `cleardel` to clear the ~/.trash
# 'y' or 'Y' to confirm; 'n' to cancel
cleardel()          
{  
    read -p "clear sure?[Input 'y' or 'Y' to confirm. && Input 'n' to cancel.]" confirm   
    [ $confirm == 'y' ] || [ $confirm == 'Y' ]  && /bin/rm -rf ~/.trash/*   
}  

Problems come when two files/folders have the same name. Imagine you move two files with the same name into the same directory. You would expect the older one is overriden by the newer one. If we move folders rather than files, then you even cannot complete the operation!

A modified solution is that we can rename the deleted files/folders by appending a date to the files/folders name. For example, add $(date +"%Y%m%d%H%M%S") to the file name.

# ...
del()               # define function `del`
{  
  mv $@ ~/.trash/$(date +"%Y%m%d%H%M%S")_$@  # mv files/folders to ~/.trash and rename it
}  
# ...

Be careful! Wildcard * (such as rm test*) is unsurported by this function! This can be solved by moving the deleted files/folders to a .tmp folder and moving them iteratively to the .trash folder.

mkdir -p ~/.Trash
mkdir -p ~/.Trash/.tmp
alias rm=del
del()
{
    dir=$(pwd)
    mv $@ ~/.Trash/.tmp
    cd ~/.Trash/.tmp
    for file in *
    do
        mv $file ../$(date +"%Y%m%d%H%M%S")_$file
    done
    cd $dir
}
cleardel()
{
    read -p "Clear Sure?[Input 'y' or 'Y' to confirm. && Input 'n' to cancel.]" confirm
    [ $confirm == 'y' ] || [ $confirm == 'Y' ] && /bin/rm -rf ~/.Trash/*
}