1 Reply Latest reply on Feb 11, 2011 1:18 PM by Milton Stamper

    How to delete directories in a folder without deleting certain ones

    Milton Stamper

      This is probably more of a scripting question than a BL question, but here goes:

       

      The /$RSCD_DIR/Transactions folder is a mess. I want to delete everything in there except for the events, log, and locks directories.

       

      The following command finds everything that is NOT those directories:

       

      find . -maxdepth 1 -type d \( ! -iname "events" ! -iname "locks" ! -iname "log" \)

       

      that command returns the following listing:

       

      .

      ./al4j5j67j4h3g2jnb5g6j7l43n3g3

      ./testdirectory

       

      and if I append "-exec rm -r {} \;" to the end of the above command, you would think it would delete those dirs. It does, but it deletes everything because of the "." in the directory listing pulled from the find command.

       

      I know this is probably a simple syntax thing, but I'm banging my head against the wall here.

       

      How do I delete everything EXCEPT the 3 directories listed above?

       

      ---------------------------------------------------------------------------

       

      And once THAT syntax is correct, how do I feed the stupid Windows path (c/Program\ Files/BMC\ Software/BladeLogic/8.0/RSCD/Transactions)  to the NSH script without it complaining?

       

      If I feed the $RSCD_DIR variable to the CLI for testing, I get "cd: too many arguments."

       

      I understand that's from the delimiter indicating a space in the path but if I manually set the $RSCD_DIR variable without the delimiters and do an ls using the $RSCD_DIR variable, I get:

       

      ls: /c/Program: No such file or directory

      ls: Files/BMC: No such file or directory

      ls: Software/BladeLogic/8.0/RSCD: No such file or directory

       

      So, it doesn't understand the spaces.

       

      If I double-delimit the backslash like this: "c/Program\\ Files/BMC\\ Software/BladeLogic/8.0/RSCD/Transactions" I get basically the same thing:

       

      ls: /c/Program\: No such file or directory

      ls: Files/BMC\: No such file or directory

      ls: Software/BladeLogic/8.0/RSCD: No such file or directory

       

      Thanks, Casey

        • 1. How to delete directories in a folder without deleting certain ones
          Milton Stamper

          Figured it out.

           

          find . -maxdepth 1 -type d \( ! -iname "events" ! -iname "locks" ! -iname "log" \) returns:

          .

          ./dirwedontwant

          ./anotherdirwedontwant

           

          adding the "-mindepth 1" parameter to the find statement returns:

           

          ./dirwedontwant

          ./anotherdirwedontwant

           

          There is no "." to content with. At this point, just add the -exec rm -r {} \; to the end of the statement and it is completed:

           

          find . -maxdepth 1 -mindepth 1 -type d \( ! iname "events" ! -iname "locks" ! -iname "log" \) -exec rm -r {} \;

           

          -------------------------------------------------------------------------------------------------------

           

          The other issue was using the RSCD_DIR variable in a script while at the same time contending w/the Windows path that has spaces in it.

           

          The answer was simple - the script goes something like this:

           

          SERVER_LIST=`blcli -v ProfileGoesHere -r RoleGoesHere Server listAllServers`

           

          for serverName in $SERVER_LIST

           

          do

           

          RSCD_DIR=`blcli -v ProfileGoesHere -r RoleGoesHere Server printPropertyValue $serverName RSCD_DIR`

           

               if [[ ${RSCD_DIR} = " " ]]

               then    

                    echo "Transaction directory parameter missing on $serverName"

               else

                    cd //"serverName$RSCD_DIR"/Transactions

                    find . -maxdepth 1 -mindepth 1 -type d \( ! -iname "events" ! -iname "locks" ! -iname "log" \) -exec rm -r {} \;

               fi

          done