Linux-on the Character interface, avoid accidental file deletion and establish a resource recovery mechanism

Source: Internet
Author: User
On the Linux-character interface, avoid accidental file deletion and establish a resource recovery mechanism-Linux general technology-Linux programming and kernel information. For details, see the following. Files are deleted in reverse mode to avoid deleting files and rescue deleted files in ext2 File System

Avoid accidental deletion of files and create a resource recycle bucket mechanism

Today, almost all systems [graphic interfaces] have built-in Resource Recovery mechanisms to reduce the regret of accidental file deletion.

However, in characters, the current versions are related to the establishment of relevant mechanisms, so the rm command is used to delete important files, most of which are difficult to restore. Therefore, we will introduce the centralized method and establish a mechanism similar to the resource recycle bin.

Delete an archive using the answer

The simplest way to avoid accidental file deletion is to confirm the deletion before deleting the file. If the-I parameter is added to the rm command, before deleting the file, the rm command will ask if we are sure:

[Root @ localhost ~] # Rm-I yum.gz
Rm: Delete the general file 'um.gz '? Y

Therefore, we use the alias command to create a command alias named rm as follows:

[Root @ localhost ~] # Alias rm = 'rm-I'

[The preceding commands are applicable to the Bourne Shell families such as zsh and bash. If your shell is tcsh or csh, the command is alias rm? I '. The instructions and settings in the subsequent content of this chapter are also different under tcsh or csh]

When you use rm to delete an object, the-I parameter is automatically added and you are sure:

[Root @ localhost ~] # Touch iso1
[Root @ localhost ~] # Touch iso2
[Root @ localhost ~] # Rm iso *
Rm: The 'iso 'directory cannot be deleted. It is a directory.
Rm: Do you want to delete the generally empty file 'iso1 '? Y
Rm: Do you want to delete a general empty file 'iso2 '? Y

Because the alias defined under the command is temporary, you can add the preceding alias command to the end of the. bashrc file in the home directory to automatically set this alias upon each login. If you want all users on the system to use this alias, you can add it to the end of the profile file in the/etc/directory.

[Many releases default to include this alias in the. bashrc file of the root account, so as to avoid having the highest right to root accidentally delete important system files]

Establish a resource recovery mechanism-replace rm with mv

The permanent solution is to create a resource recycle bin. The files to be deleted are first moved to the recycle bin. after a certain period of time, the deleted files can be avoided.

Once you replace the rm command with mv, you can create a simple resource recycle bin. First, replace the original rm command with a command draft. In the/usr/local/bin directory, use the file editor to create a file named rm. junk's new file:

#! /Bin/sh

# Set the Directory Name of the resource recycle bin

[Root @ localhost ~] # Vi/usr/local/bin/rm. junk
#! /Bin/sh

# Set the Directory Name of the resource recycle bin

TRASH_DIR = ". trash"
[-D $ TRASH_DIR] | mkdir-p ~ /$ TRASH_DIR
Mv $ *~ /$ TRASH_DIR/

After completion, run the following command to add the execution permission to the command draft:

[Root @ localhost ~] # Chmod + x/usr/local/bin/rm. junk
[Root @ localhost ~] # Alias rm = '/usr/local/bin/rm. junk'

[The setting is temporary. Therefore, you need to add the alias command to the end of the. bashrc file in the home directory, so that you can set this alias each time you log on. If you want all users of the system to use this alias, you can add it to the end of the profile file under the/etc/directory]

When you enter rm to delete the file, call rm. juk command draft. this command will create a command named. trash directory, and then move all deleted files to this directory.

[If you want to delete the file, enter/bin/rm to directly call the rm execution file in the/bin directory]

[Root @ localhost ~] # Rm grub *
[Root @ localhost ~] # Cd ~ /. Trash ./
[Root @ localhost ~] # Cd ~ /. Trash/
[Root @ localhost. trash] # ls
Grub-0.95 grub-1.96 grub-1.96.tar.gz grub1.iso grub_for_dos-0.4.1
[Root @ localhost. trash] #

[We use rm. juk command draft, let the mv command replace the original rm command, because the rm command and rm command parameters are different, so common parameters cannot be used, for example, if the mv command does not have the-r parameter, execute rm? R errors]

However, all deleted files are moved to the recycle bin, so these files are still stored in the system. To prevent them from occupying valuable resources, You can execute crontab? E command, add the following content:

0 2 ***/bin/rm? Rf 'Find ~ /. Trash/-atime + 1'

[This means to search for files not accessed for more than one day in the. trash directory of the price Directory]

At every day, the System Searches the resource recycle bin under the account for the information that has not been accessed for more than one day, and then deletes it automatically. If you want to help all users in the system automatically delete files that have been stored in the resource recycle bin for more than one day, you can use the root account to execute crontab? E command, add:

0 2 ***/bin/rm? Rf 'Find/home/*/. trash/-atime + 1'
Related Article

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.