Modify RM command under Linux to prevent accidental deletion

Source: Internet
Author: User
Tags custom name

Preface: I believe many friends have encountered in the Linux under the RM command mistakenly delete files, the heart of the moment as if there are countless alpaca in the Pentium. So how do you prevent this from happening? Of course there is a way, we can write a shell script, change the role of the RM command.

The first step:

First in the home directory to create a directory called Username name (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 (a custom name) To change your directory permissions, because the directory you just created in the home directory does not have permission to execute.

Step Two:

in the username directory, the command is: CD username , and then create two directories under the username directory for the trash and tools Two names can be customized, of course, you have to change the permissions, the same way as above.

< Span style= "Background-color: #ccffcc" > step three:

< Span style= "Background-color: #ccffcc" >< Span style= "font-size:13px" > into the tools directory, the method is already mentioned in the second step, creating a shell file in it called r emove.sh (the name can also be customized, but the suffix must be. sh), the specific command is: touch remove.sh When the shell file is generally executed, then the command is not used: sudo chmod 777 remove.sh Change permissions.

Fourth Step:

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

Para_cnt=$ #TRASH_DIR="/home/username/trash"    //If you are a custom directory name, it must be changed to your custom directory name  for in do    STAMP=$i    mv $i $TRASH _dir/. $STRMPdone 

Fifth Step:

Then use the command:VI ~/.bashrc Open the ~/.BASHRC file to the last add this code:alias rm= "/home/username/tools/remove.sh", If your directory name is custom, be sure to change it to your own name, and then save the exit.

Sixth step:

With the command: thesource ~/.BASHRC reboot just fine.

This time to change the RM command to the same role as MV, you use the RM command when you will delete the files you deleted to the Trash directory, if you accidentally deleted the file by mistake, you can go to the Trash directory to find back.

of course, if you want to completely delete these files, you can use the command directly: sudo rm [filename/directory name] so that these files will not be moved to the Trash directory.

If you want to clean up the files in the Trash directory simply, after entering the trash directory, execute the command directly:sudo rm *, so that you can empty all the files in the Trash directory.

If you're really lazy, let it automatically periodically empty the contents of the Trash directory, as follows:

First Use the command:cd/etc/ into the ETC directory, you will see there is a file called crontab, first with the command:sudo chmod 777 crontab Change the crontab file permissions first, Otherwise, you can't change the code inside . Then open the crontab file, the command is:VI crontab directly at the end of the add this code:

0 0  * * * *       Rm-rf/home/username/trash *   ///If your name is not the same as mine, then change to your own

The meaning of this sentence is to empty the Trash directory 0:0 every day.

Then repeat the sixth step and you're done. This time will help you automatically empty the Trash directory every day, of course you can also manually empty, the above has been said.

Modify RM command under Linux to prevent accidental deletion

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.