Linux shell command to overwrite entire disk with small files

Source: Internet
Author: User

Suppose we are confronted with a problem like this, the company's key data copy to the test server, after it is deleted directly, still worry that the server vendor can restore it, even through Fdisk reformatting, there is still the risk of recovery, in view of this, we need to write a script, Create a lot of small files (5MB or so), and then continue to copy the file on the disk where the key data resides so that the Linux inode can no longer be restored, in order to achieve this effect, we need to construct the file first, such as:

The code is as follows Copy Code
/> Find. -name "*" > Testfile
/> Ls-l testfile
-rwxr-xr-x. 1 root 5123678 Dec 9 11:46 testfile
/> Cat > test27.sh
#!/bin/sh

#1. Initializes the counter variable, where Max's value is computed based on the amount of disk space and testfile that are currently populated.

The code is as follows Copy Code
Counter=0
max=2000000
Remainder=0

#2. Each iteration counter variable is added one to ensure that different files are generated at a time. Exits when the value is greater than the maximum value.
#3. Counter the counter variable by 1000, so that you can print one output at a time of 1000 files in order to see the progress of the overlay and how long it will take for the output to be completed.
#4. Create a separate directory to hold these overwrite files.
#5. Generates a temporary file and prints a hint if the write fails.

The code is as follows Copy Code
While True
Todo
((counter=counter+1))
If [#counter-ge $max]; Then
Break
Fi
((remainder=counter%1000))
If [$remainder-eq 0]; Then
Echo-e "counter = $countert date =" $ (date)
Fi
Mkdir-p/HOME/TEMP2
Cat < Testfile > "/home/temp/myfiles. $counter"
if [[$]-ne 0]]; Then
echo "Failed to Wrtie file."
Exit 1
Fi
Done
echo "Done"
Ctrl+d
/>./test27.sh
Counter = 1000 Fri Dec 9 17:25:04 CST 2011
Counter = Fri Dec 9 17:25:24 CST 2011
Counter = 3000 Fri Dec 9 17:25:54 CST 2011


... ...
At the same time, you can monitor disk space usage by performing the following command.

The code is as follows Copy Code
/> watch-n 2 ' df-h '
Every 2.0s:df-h Fri Dec 9 17:31:56 2011

FileSystem Size Used Avail use% mounted on
/dev/sda2 3.9G 2.3G 1.4G 63%/
Tmpfs 504M 100K 504M 1%/DEV/SHM
/DEV/SDA1 49M 36M 11M 77%/boot
/dev/sda3 15G 172M 14G 2%/home

      We can also monitor the number of read and write blocks per second of the script process through the Pidstat command during execution.

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.