Using Python and Shred to securely
remove directories and files on Linux File System
Platform OS:
Ubuntu 10.10
Programming Language:
Python -
Application:
Shred
Summary:
There was a need to find an automated means to safely clean up on my desktop using a file shredder application on a Linux platform. The application that I’ve chosen for this project was “Shred”. This application is available on most Linux distributions and since this application does not provide a switch to recursively remove all files and the parent directory. I have chose to quickly write a script to securely overwrite and remove the files in a directory location provided on a command line.
Shred Settings:
The shred application is capable of numerous settings, please see the man pages for a complete list of shred settings. On this particular project I have chosen the following settings to erase files on my linux system:
shred -uvfzn7 <filename>
-u, --remove
truncate and remove file after overwriting
-v, --verbose
show progress
-f, --force
change permissions to allow writing if necessary
-z, --zero
add a final overwrite with zeros to hide shredding
-n, --iterations=7
overwrite N times instead of the default (3)
Python Script Usage: This program has been updated to account for spaced an special characters in file names
Step 1.
Save the script to the following file name- ShredFile.py -
Step 2.
Execute the following script from command line using the following command:
Step 3.
Enter a Directory path within your file system
Step 4.
Verify that you entered the correct directory and select Yes or No to re-enter the folder path.
Results:
You can enter another directory or enter Q to quit the application.
Python Script:
'''
Created on Aug 26, 2011
@author:
'''
import os
emptyDirs = []
def removeDirectory(dirEntry):
print ("Shredding files in " + dirEntry[0])
deleteFiles(dirEntry[2],dirEntry[0])
emptyDirs.insert(0, dirEntry[0])
def deleteFiles(dirList, dirPath):
for file in dirList:
try:
print ("Shredding " + (file))
fileshred = dirPath + "/" + file #This is the old name
fileshred = "\"" + fileshred + "\""
os.system("shred -uvfzn7 " + fileshred)
except: os.error
while True:
try:
pathloc = raw_input('Enter a path for the directory to delete: or Q to Quit >>> ')
path = str(pathloc)
if path == "Q" or path == "q":
print("Good Bye")
break
else:
Verify= raw_input('Are you sure you want to delete %s Yes or No >>> ' % path)
if Verify != "Yes":
print("Please Enter Yes or No exactly")
break
except:
break
else:
try:
tree = os.walk(path)
except: os.error
print("unable to locate Directory -->> %s" % path)
for directory in tree:
removeDirectory(directory)
#Remove the empty directories
for dir in emptyDirs:
#dir = "\"" + dir + "\""
#print ("Removing " + dir)
print (dir)
try:
os.rmdir(dir)
except: os.error
emptyDirs = []