Linux Recycle Bin Mechanism Implementation Process and Usage Detailed Explanation

  • 2021-08-17 01:54:11
  • OfStack

Foreword:

rm under linux system is irreversible, There is nothing wrong with the command design itself, The problem is that we are usually very confident. I like rm-rf when I execute it. That would be very dangerous, If the command is not executed correctly, Even if the directory is wrong, it will cause a lot of trouble, which will lead to overtime work, waste manpower and material resources, affect the company's business, and even have the danger of dismissal. The recycle bin mechanism under windows is a very good strategy, but linux does not come with it, so we can manually create one and execute it directly:


mkdir -p ~/.Trash
cat >>~/.bashrc<<EOF
#add by caimengzhi at $(date +%F) for Linux trash start
alias rm=trash
alias rl='ls ~/.Trash' 
alias ur=undelfile
undelfile() 
{ 
 mv -i ~/.Trash/\$@ ./ 
} 
trash() 
{ 
 mv \$@ ~/.Trash/ 
}
cleartrash() 
{ 
  read -p "Clear trash?[n]" confirm 
[ \$confirm == 'y' ] || [ \$confirm == 'Y' ] && /usr/bin/rm -rf ~/.Trash/* 
}
#add by caimengzhi at $(date +%F) for Linux trash end
EOF
source ~/.bashrc

Description:

1. ~/. Trash is where the files and folders that are later deleted are moved, which is the recycle bin

2.\ $confirm means to implement validation, that is, $confirm ends up in the file. Among them\ $@ 1 samples

3. The above function, to put it bluntly, is to command the renaming of rm.

Use syntax:

rm (Delete), ur (Undo), rl (List Recycle Bin), cleartrash (Empty Recycle Bin) commands.

# Delete 1 folder and files will be moved to the Recycle Bin.

$rm filedirctory

# Delete 1 file

$rm file.txt

# Undo deletion of file. txt

$ur file.txt

# Undo filedirctory folder

$ur filedirctory

# List Recycle Bin

$rl

# Empty the Recycle Bin

cleartrash

ok, That's the basic content. Essentially, it only replaces the rm command of root user with mv command. If other users are established in the system, it is also necessary to use this user to re-execute the above command. It depends on the situation. If the root authority is lost, the power of rm-rf will not be feared. Of course, we also need to execute the regular rm command. How to do it, so ok:

/usr/bin/rm -rf


Related articles: