If you connect a portable HDD or USB memory to a Mac and then connect to Windows or Linux, I notice that a lot of ._ [existing file names] like .DS_Store and metafiles of existing files are created. These files are smaller than the sector size of the file system, so thousands of files can waste a few GB. That was exactly what it was.
On Linux, you can delete it with ``` find. -Name" ._ * "-exec rm -f {} ;` `` Even if I try to search for ._ * with Windows explorer and delete it, it is very difficult to delete because ordinary files and directories that are not ._ * are caught.
So I tried to make a script with python easily for those who are in trouble and myself. In addition, since we do not perform strict file checks, there is a possibility that files that should not be deleted will be deleted, so please use this script at your own risk.
import os
import sys
import shutil
def deldirs(baseDir):
print "Start " + baseDir
ds = [baseDir]
while True:
if len(ds) == 0:
print "Finished!!"
break
# Pop next target dir
d = ds.pop(0)
# Delete file
fs = os.listdir(d)
for fn in fs:
fp = d + "/" +fn
if fn[:2] == "._" or fn == ".DS_Store":
if os.path.isfile(fp):
print "Del File " + fp
os.remove(fp)
#elif os.path.isdir(fp):
# print "Del Dir " + fp
# shutil.rmtree(fp)
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Usage: delmacfile.py TARGET_DIR"
exit(1)
baseDir = sys.argv[1]
if os.path.isdir(baseDir) == False:
print "Please arg1 is a TAGET_DIR"
exit(2)
deldirs(baseDir)
There seems to be a way to do it with Windows commands, but it is for Python lovers.
Recommended Posts