Shell scripts for safely deleting and restoring files in CentOS

Source: Internet
Author: User
Currently, most of the Linux file systems are in Ext3 format. Once a file is deleted, it may not be restored, even if it can be recovered. Therefore, executing the rm command becomes abnormal. Therefore, we can write two Shell scripts to safely delete and restore files. The principle is very simple. create a hidden folder & ldquo;. temp & rdquo; in the current user's home directory, which is equivalent to the recycle bin in Windows. When

Currently, most of the Linux file systems are in Ext3 format. Once a file is deleted, it may not be restored, even if it can be recovered. Therefore, executing the rm command becomes abnormal. Therefore, we can write two Shell scripts to safely delete and restore files.

The principle is very simple. create a hidden folder ". temp" under the current user's home directory, which is equivalent to the recycle bin in Windows. To safely delete a file, cut the file to be deleted to this directory. If you want to recover, cut the files in the ". temp" directory to the original location.

The Shell script for deleting files is named erase. the code is as follows:

#! /bin/bash  RecycleBin=~/.temp   (($#==0)) && { echo "No paraments!";exit 1; }  if [ ! -d $RecycleBin ]; then      mkdir $RecycleBin  fi  for i in $*  do      if test -e $i          then              cd $(dirname $i)              mv -f $(basename $i) $RecycleBin/$(find $(pwd) -maxdepth 1 -name $(basename $i) | tr "/" "=")              cd -      else         echo "$i:No such file or directory!"      fi  done  

The Shell script for file recovery is named unerase. the code is as follows:

#! /bin/bash  cd ~/.temp  list=$(for i in $*; do ls |grep "<$i>"; done)  (($#==0)) && { list=$(ls|grep ""); }  for j in $list  do      file=$(echo $j | tr "=" "/")      mv $j ${file%/*}/${file##*/}  done  

Save the two files, run the chmod command to add executable permissions to them, and copy them to the "/usr/bin" directory, then we can use the erase and unerase commands just like using the rm command. Demonstrate how to delete the test file, restore the test file, and /. Temp "directory file changes.

To safely delete certain files, use the eares command to keep up with the files to be deleted. this command supports both relative paths and absolute paths. To safely restore some files, use the unerase command to keep up with the file name to be restored. if the file name is not followed, the default restore will be "~ /. Temp "directory.

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.