Advertisement
Guest User

Simple Tagging Script

a guest
Aug 2nd, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.51 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Matches directory and empty file (tag) names to a list of input strings,
  4. # prints bottom directories containing all input
  5. #
  6. # E.g. finding results from January 2013 in a directory structure
  7. # .
  8. # 2012/
  9. # 2012/01/Simulation/result
  10. # 2013/01/
  11. # 2013/01/Simulation/
  12. # 2013/01/Simulation/result
  13. # 2013/01/Preparation/result
  14. # 2013/02/Simulation/result
  15. #
  16. # $ ./tags.sh 2013 01 result
  17. # .2013/01/Simulation/result
  18. # .2013/01/Preparation/result
  19.  
  20. # Check if strings partly contain, or match, each other
  21. function match() {
  22.     if [[ "$1" == *"$2"* ]]; then
  23.         MATCH=$1
  24.     elif [[ "$2" == *"$1"* ]]; then
  25.         MATCH=$2
  26.     else
  27.         return 1
  28.     fi
  29.  
  30.     return 0
  31. }
  32.  
  33. # Check if FILE $2 substring occurs in TAG $1 results
  34. function control_tags() {
  35.     # If final level, print FILE
  36.     if [ $1 -eq $NUMTAGS ]; then
  37.         echo $2;
  38.     # If not, match against files in TAG and call next if found
  39.     else
  40.         for FILE in ${FOUND[$1]}; do
  41.             if match $2 $FILE; then
  42.                 control_tags $(($1+1)) ${MATCH}
  43.             fi
  44.         done
  45.     fi
  46. }
  47.  
  48. if [ $# == 0 ]; then
  49.     echo USAGE: $1 TAG [TAG [...]]
  50.     exit
  51. fi
  52.  
  53. NUMTAGS=$#
  54. FOUND={}
  55. i=0
  56.  
  57. # Find empty files (tags) and directories corresponding to input TAGs
  58. for TAG in $@; do
  59.     DIRS=$(find . -name ${TAG} -type d)
  60.     FILES=$(find . -name ${TAG} -type f -a -empty -printf '%h\n')
  61.  
  62.     FOUND[$i]=${FILES}${DIRS}
  63.     i=$(($i+1))
  64. done
  65.  
  66. # Initiate TAG control from TAG results 0
  67. control_tags 0 .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement