Skip to content

Instantly share code, notes, and snippets.

@dvdsgl
Created June 1, 2011 03:54
Show Gist options
  • Save dvdsgl/1001761 to your computer and use it in GitHub Desktop.
Save dvdsgl/1001761 to your computer and use it in GitHub Desktop.
Delete duplicate files in Haskell
#!/usr/bin/runhaskell
import Data.List
import Control.Monad
import System.Process (readProcess)
import System.Environment (getArgs)
import System.Directory (doesFileExist, removeFile)
data HashedFile = HashedFile { path :: String, hash :: String }
deriving Show
instance Eq HashedFile where
HashedFile _ h1 == HashedFile _ h2 = h1 == h2
mkHashedFile file = do
hash <- (last . words) `fmap` readProcess "md5" [file] ""
return $ HashedFile file hash
main = do
hfiles <- getArgs >>= filterM doesFileExist >>= mapM mkHashedFile
let dups = map path . concatMap tail . group $ hfiles
mapM_ putStrLn dups
mapM_ removeFile dups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment