Advertisement
OldManRiver

Get Emails - BASH

Apr 10th, 2015
334
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/bash
  2. # Script Name: get-all-emails.sh
  3.  
  4.  
  5. # Email Address extraction script to get Thunderbird EMail addresses from email files.
  6.  
  7. # Get current directory
  8. DIR="$( cd -P "$( dirname "$0" )" && pwd )"
  9. curdir="$DIR";
  10. suser="myuser";
  11. regx1='^\(From\|To\|Cc\|CC\Bcc\|BCC\):';
  12. regx2='[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}';
  13. #file1="/home/${suser}/myfiles/all-addresses.txt";
  14. #file2="/home/${suser}/myfiles/all-addresses.old.txt";
  15. file1="/home/path/TBird/Rules/all-addresses.txt";
  16. file2="/home/path/TBird/Rules/all-addresses.old.txt";
  17. file3="/home/path/TBird/Rules/lf-addresses.txt";
  18. file4="/home/path/TBird/Rules/g0-addresses.txt";
  19. file5="/home/path/TBird/Rules/g1-addresses.txt";
  20. #echo "C=>$curdir";
  21.  
  22. # Declare the Default Thunderbird dir
  23. tbdir="/home/${suser}/.thunderbird";
  24. tbfnd=`find /home/${suser}/ -type f -iname ".*" | grep thunderbird`;
  25. tbidx=`expr index "$tbfnd" '.'`;
  26. tbidx=$(($tbidx+12));
  27. # echo "FD=> ${tbfnd} PS=> ${tbidx}";
  28. tbnst=${tbfnd:${tbidx}};
  29. tbsdx=`expr index "$tbnst" '.'`;
  30. tbsdx=$(($tbsdx+7));
  31. # echo "NS=> ${tbnst} SS=> ${tbsdx}";
  32. tbpdr=${tbnst:0:${tbsdx}};
  33. #echo "DR=> ${tbpdr}";
  34. tbfdr="${tbdir}/${tbpdr}";
  35. #echo "FD=> ${tbfdr}";
  36.  
  37. # Declare the "Local Folders" dir
  38. lcmail="${tbfdr}/Mail/Local Folders";
  39. #echo "LF=> ${lcmail}";
  40. cd "${lcmail}";
  41. echo "Processing Local Folders!";
  42.  
  43. # Run the grep statement to get the addresses
  44. grep $regx1 * | grep -o -E $regx2 | sort -f | uniq -i > $file3;
  45. grep $regx1 Archives | grep -o -E $regx2 | sort -f | uniq -i >> $file3;
  46. grep $regx1 Drafts | grep -o -E $regx2 | sort -f | uniq -i >> $file3;
  47. grep $regx1 Sent | grep -o -E $regx2 | sort -f | uniq -i >> $file3;
  48. grep $regx1 Trash | grep -o -E $regx2 | sort -f | uniq -i >> $file3;
  49. grep $regx1 'Unsent Messages' | grep -o -E $regx2 | sort -f | uniq -i >> $file3;
  50.  
  51. # Declare the gmail.com dir for Thunderbird
  52. gm0="${tbfdr}/ImapMail/imap.googlemail.com";
  53. #echo "GM0=> ${gm0}";
  54. cd "${gm0}";
  55. echo "Processing GMail 0!";
  56.  
  57. grep $regx1 * | grep -o -E $regx2 | sort -f | uniq -i > $file4;
  58. grep $regx1 INBOX | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  59. grep $regx1 Junk | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  60. grep $regx1 Sent-1 | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  61. gm0g="${tbfdr}/ImapMail/imap.googlemail.com/[Gmail].sbd";
  62. #echo "GMG=> ${gm0g}";
  63. cd "${gm0g}";
  64. grep $regx1 * | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  65. grep $regx1 '@Corp' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  66. grep $regx1 '@Marketing' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  67. grep $regx1 'All Mail' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  68. grep $regx1 'Blogs' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  69. grep $regx1 'Bots &- Scrubs' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  70. grep $regx1 'Customers' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  71. grep $regx1 'Drafts' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  72. grep $regx1 'Fiduciary &- Legal' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  73. grep $regx1 'Important' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  74. grep $regx1 'Job Search' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  75. grep $regx1 'Ministry' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  76. grep $regx1 'Partner &- Subcontract Sources' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  77. grep $regx1 'Personal' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  78. grep $regx1 'Projects' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  79. grep $regx1 'Sent Mail' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  80. grep $regx1 'SEO &- SMO Sources' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  81. grep $regx1 'Support' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  82. grep $regx1 'Telecom Tech Jobs' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  83. grep $regx1 'Training &- Education' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  84. grep $regx1 'Trash' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  85. grep $regx1 'Work' | grep -o -E $regx2 | sort -f | uniq -i >> $file4;
  86.  
  87. # Declare the IMapMail dir for Thunderbird
  88. gm1="${tbfdr}/ImapMail/imap.googlemail-1.com";
  89. echo "GM1=> ${gm1}";
  90. cd "${gm1}";
  91. echo "Processing GMail 1!";
  92.  
  93. grep $regx1 * | grep -o -E $regx2 | sort -f | uniq -i > $file5;
  94. grep $regx1 INBOX | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  95. gm1g="${tbfdr}/ImapMail/imap.googlemail-1.com/[Gmail].sbd";
  96. echo "GMG=> ${gm1g}";
  97. cd "${gm1g}";
  98. grep $regx1 * | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  99. grep $regx1 '@Marketing' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  100. grep $regx1 'All Mail' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  101. grep $regx1 'C-Level' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  102. grep $regx1 'DaviSoft Recruiting' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  103. grep $regx1 'Drafts' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  104. grep $regx1 'Important' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  105. grep $regx1 'Ministry' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  106. grep $regx1 'Products &- Vendors' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  107. grep $regx1 'Sent Mail' | grep -o -E $regx2 | sort -f | uniq -i >> $file5;
  108.  
  109. # Sort the output for unique addresses
  110. cat $file3 $file4 $file5 > $file2;
  111. sort $file2 | uniq -u > $file1;
  112.  
  113. # Change back to current directory
  114. cd "$curdir";
  115.  
  116. echo "Output in ${file1}";
  117. nano ${file1};
  118.  
  119.  
  120. Extra Code:
  121.  
  122. # Get all pertainent directories
  123. t_filter='(googlemail|gmail|"local folders")';
  124. t_exclude='dropbox'
  125. ray_cnt=0;
  126. declare -a output;
  127. dirs=$(find / -type d | grep -i thunderbird | grep -i "${t_filter}" | grep -iEv ${t_exclude});
  128. for dir in ${dirs}; do
  129.     output[${ray_cnt}]=${dir};
  130.     let ray_cnt=$ray_cnt+1;
  131. done;
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement