In linux, the rm command is modified to prevent accidental deletion, and the linuxrm command is deleted by mistake.

Source: Internet
Author: User
Tags custom name

In linux, the rm command is modified to prevent accidental deletion, and the linuxrm command is deleted by mistake.

I believe many of my friends have experienced the accidental deletion of files by using the rm command in linux. At this moment, there are countless alpaca in their hearts. How can this problem be prevented? Of course there are methods. We can write a shell script to change the role of the rm command.

Step 1:

First, create a directory named username under the home Directory (of course, the name can be customized). The specific command is: sudo mkdir username (custom name)

At this time, remember to use the command: sudo chmod 777 username (custom name) to change your directory permissions, because the newly created directory in the home directory has no execution permission.

Step 2:

Go to the username directory and run the following command: cd username. Then, create two directories under the username Directory: trash and tools, which can be customized. Of course, you must change the permissions, the method is the same as above.

Step 3:

Go to the tools directory. The method has been described in step 2. Create a shell file named remove. sh (the name can also be customized, but the suffix must be. sh), the specific command is: touch remove. sh. At this time, the shell file generally has the execution right. If not, use the command: sudo chmod 777 remove. sh to change the permission.

Step 4:

Then write the code in the shell file remove. sh just created:

 

PARA_CNT =$ # TRASH_DIR = "/home/username/trash" // if you are using a custom directory name, change the name of your custom directory to for I in $ *; do STAMP = $ I mv $ I $ TRASH_DIR /. $ STRMPdone

Step 5:

Run the following command: vi ~ /. Open bashrc ~ /. Add this code to the end of the bashrc file: alias rm = "/home/username/tools/remove. sh ", if your directory name is customized, you must change it to your own name here, and then save and exit.

Step 6:

Run the following command: source ~ /. Just restart bashrc.

In this case, the rm command is changed to the mv command. When you use the rm command, you will move the deleted file to the trash directory, if you accidentally delete the file by mistake, you can retrieve it in the trash directory.

If you want to delete these files thoroughly, run the command: sudo rm [file name/directory name] so that these files will not be moved to the trash directory.

If you want to clear the files in the trash directory, you can directly execute the command: sudo rm * after entering the trash directory to clear all the files in the trash directory.

 

If you are really lazy, let's make it clear things in the trash directory automatically and periodically. The specific operations are as follows:

Run the following command: cd/etc/to enter the etc directory. You will see a file named crontab. Run the command: sudo chmod 777 crontab to change the permissions of the crontab file first, otherwise, you cannot modify the code. Next, open the crontab file and run the command "vi crontab" to directly add the code at the end:

0 0 *** rm-rf/home/username/trash/* // if your name is different from mine, change it to your own

 

This statement indicates clearing the trash directory at 00:00 every day.

Repeat Step 6 to complete the process. At this time, the trash directory will be automatically cleared for you every day. Of course, you can also manually clear the directory, as mentioned above.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.