Give you a chance to regret

Linux, rmthe command is to delete a file, but Linux is no mechanism for the Recycle Bin, so once you delete a file, then it is completely removed from the disk. Many people like to add -fthe option, on behalf of the mandatory sense, more violent it is time to delete a folder using the rm -rfcommand this way, the folder itself, as well as children and grandchildren folder are the end of it.

And in case if you execute the rm -rf /*command, and it is undoubtedly equivalent to the following effect:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-4kU4oVQy-1599380335220)(https://i.loli.net/2018/12/04/5c06936a2c133.jpg )]

Because there is no recycle bin mechanism, many people have suffered from this loss, and so is the good promise. After months of hard writing code, an rm command is completely wiped out. Every time the rm command is executed, the hands are shaking. ...

Since Linux does not have a recycle bin, can we implement one ourselves? The answer is of course yes. Next, we will make a recycle bin by ourselves and give ourselves a chance to regret.

Since the culprit is the rm command, then we will modify this command. In the Linux command redefine the use of aliasthe command, modify a .bashrcfile, namely:

vim ~/.bashrc

At the end of the file, we add the following lines of code:

mkdir -p ~/.trash
alias rm=trash
alias r=trash
alias rl='ls ~/.trash/'
alias ur=undelfile

undelfile()
{
    mv -i ~/.trash/$@ ./
}

trash()
{
    mv -i $@ ~/.trash/
}

cleartrash()
{
    read -p "clear sure?[n]" confirm
    [ $confirm == 'y' ] || [ $confirm == 'Y' ] && /bin/rm -rf ~/.trash/*
}

Here we are at home to create a directory .trashin a hidden folder, as the Recycle Bin. Then, we redefine the rm command. When we execute rm or r, the trash function will be executed. In the trash function, only one thing is done:

mv -i $@ ~/.trash/

That is to move all the files after rm to the .trash directory (ie, simulate throwing into the recycle bin). -iThe option means that if there is a file with the same name in the .trash directory, it will prompt whether to overwrite it.

We will rlbe defined as ls ~/.trash/, say, we can view the files in the directory by .trash rl, ie "delete" a file.

If you want to restore a file, you can do ur, and will do ur undelfilefunction. In undelfile, the files after ur are moved from the .trash directory back to the original directory to realize file deletion and restoration.

After a period of time, too many files in the Recycle Bin, we can use the cleartrashcommand to empty the Recycle Bin. It performs the function of the same name, calls the /bin/rmcommand .trash empty directory.

After the .bashrc file is modified, we need to make it effective:

source ~/.bashrc

Let's actually experience it:

Delete Files

alvin@alvin-pc:~/test$ touch file1 file2 file3
alvin@alvin-pc:~/test$ ls
file1  file2  file3
alvin@alvin-pc:~/test$ rm file1 file2 
alvin@alvin-pc:~/test$ r file3

View Recycle Bin files

alvin@alvin-pc:~/test$ rl
file1  file2  file3

Restore deleted files

alvin@alvin-pc:~/test$ rl
file1  file2  file3
alvin@alvin-pc:~/test$ ur file1
alvin@alvin-pc:~/test$ ur file2
alvin@alvin-pc:~/test$ ur file3
alvin@alvin-pc:~/test$ ls
file1  file2  file3
alvin@alvin-pc:~/test$ rl
alvin@alvin-pc:~/test$ 

Empty the recycle bin

alvin@alvin-pc:~/test$ rm file1 file2 file3
alvin@alvin-pc:~/test$ rl
file1  file2  file3
alvin@alvin-pc:~/test$ cleartrash
clear sure?[n]y
alvin@alvin-pc:~/test$ rl
alvin@alvin-pc:~/test$

Through the above steps, although we can realize the function of a recycle bin, in actual work, we still have to develop the habit of regular backup, so that even if there is a misoperation, we can also minimize the loss.

2020 Featured Alibaba/Tencent and other first-line companies interview, resume, advanced, e-book public account " Liang Xu Linux " backstage reply " data " free access

After reading it all is true love, just like it before leaving? Your "three consecutive" is Liang Xu's greatest motivation for continuous creation!

  1. Follow the original public account " Liangxu Linux " and get the latest Linux dry goods the first time!
  2. The official account backstage reply [Information] [Interview] [Resume] to obtain information such as interviews, self-improvements, resumes and other selected first-line companies.
  3. Follow my blog: lxlinux.net

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108432977