Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What should be paid attention to when using rm command under Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what you need to pay attention to when using rm commands under Linux. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

The rm command under Linux means to delete a file, while Linux does not have a recycle bin mechanism, so once the file is deleted, it will be deleted completely from the disk. While many people like to add the-f option, which means mandatory, what is more violent is to use the rm-rf command when deleting a folder, so that the folder itself, as well as the children and grandchildren under the folder, are over.

And in case you execute the rm-rf / * command, the effect is undoubtedly the same as the following:

Because there is no recycle bin mechanism, many people have suffered this loss, and so have the good wishes. The code that has been hard written for several months and a rm command have all come to naught, making their hands tremble every time they execute the rm command.

Since there is no recycle bin in Linux, can we implement one ourselves? The answer is, of course, yes. Next, we will make a recycle bin ourselves and give ourselves a chance to regret.

Since the culprit is the rm command, let's modify it. Redefining commands under Linux uses the alias command and modifies the .bashrc file, that is:

Vim / .bashrc

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

Mkdir-p ~ / .trashalias rm=trashalias r=trashalias 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 creating a hidden .trash folder under the home directory as the Recycle Bin. Then, we redefine the rm command. When we execute rm or r, we execute the trash function. In the trash function, you only do one thing:

Mv-I $@ ~ / .trash/

Is to move all the files after rm to the .trash directory (that is, the simulation is thrown into the Recycle Bin). The-I option indicates whether to overwrite if the .trash directory has a file with the same name.

We define rl as ls ~ /. Trash/, that is, we can view the files in the .trash directory through rl, that is, the files that have been "deleted".

If you want to restore the file, you can execute ur, and ur will execute the undelfile function. In undelfile, the files after ur are moved back to the original directory from the .trash directory so that the files can be deleted and restored.

After a while, there are too many files in the Recycle Bin, so we can use the cleartrash command to empty the Recycle Bin. It will execute a function of the same name and call the / bin/rm command to empty the .trash directory.

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

Source / .bashrc

Let's have a practical experience:

Delete a file

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

View Recycle Bin Fil

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 file3alvin@alvin-pc:~/test$ rl file1 file2 file3alvin@alvin-pc:~/test$ cleartrash clear sure? [n] y alvin@alvin-pc:~/test$ rl alvin@alvin-pc:~/test$ above are all the contents of this article entitled "what to pay attention to when using rm commands under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report