Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik5/97705b52f9d4c072f3e9 to your computer and use it in GitHub Desktop.
Save ik5/97705b52f9d4c072f3e9 to your computer and use it in GitHub Desktop.
moving maildir from disorgenized method to an orginized method.
#!/usr/bin/env ruby
require 'fileutils'
SOURCE = '/tmp/mbox/'
DESTINATION = '/tmp/maildir/domain.com/'
def is_maildir(path)
dirs = Dir[File.join(path, '*')]
dirs.select {|name| File.directory?(File.join(path, name)) &&
!(name == '.' or name == '..') }
dirs.include?(File.join(path,'new')) &&
dirs.include?(File.join(path,'Trash')) &&
dirs.include?(File.join(path,'tmp'))
end
def lookup(path)
valid = []
Dir.foreach(path) do |name|
next if name == '.' or name == '..'
fpath = File.join(path,name)
#print "Going over #{fpath} ... "
next unless File.directory?(fpath)
if is_maildir(fpath)
valid.push(fpath)
#puts 'yes'
else
#puts 'no'
valid = valid + lookup(fpath)
end
end
return valid
end
FileUtils::mkdir_p(DESTINATION)
list = lookup(SOURCE)
list.each do |dir|
print "Copying #{dir} to #{DESTINATION} ... "
begin
FileUtils::cp_r(dir, DESTINATION)
rescue => e
puts "Error: #{e.message}"
else
puts 'done'
end
end
@ynonp
Copy link

ynonp commented Aug 9, 2014

הי,
קצת ארוך... לא יותר פשוט:

find . -type d -exec bash -c "[[ -d {}/new && -d {}/Trash && -d {}/tmp ]] && echo {}" ;

@ik5
Copy link
Author

ik5 commented Aug 11, 2014

Indeed a much better solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment