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.