Securely Delete and Restore Files in Linux

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 ". 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! "The Shell script of the fi done recovery file 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 saves the two files and uses the chmod command to add executable permissions to them, copy the file 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.