clear multiple directories with rm

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am trying to clear multiple directories stored in an array. Here's a simplified example (I have more directories).



#!/bin/bash 

$IMAGES_DIR="/Users/michael/scripts/imagefiles"
$BACKUP_DIR="/Users/michael/scripts/imagebackups"

...

array=( $IMAGES_DIR $BACKUP_DIR )
for i in $array[@]
do
if [ "$(ls -A $i)" ]; then # check that directory has files in it
rm "$i/"* # remove them
fi
done


I get errors for each directory, e.g.:




rm: /Users/michael/scripts/imagefiles/*: No such file or directory








share|improve this question
























    up vote
    1
    down vote

    favorite












    I am trying to clear multiple directories stored in an array. Here's a simplified example (I have more directories).



    #!/bin/bash 

    $IMAGES_DIR="/Users/michael/scripts/imagefiles"
    $BACKUP_DIR="/Users/michael/scripts/imagebackups"

    ...

    array=( $IMAGES_DIR $BACKUP_DIR )
    for i in $array[@]
    do
    if [ "$(ls -A $i)" ]; then # check that directory has files in it
    rm "$i/"* # remove them
    fi
    done


    I get errors for each directory, e.g.:




    rm: /Users/michael/scripts/imagefiles/*: No such file or directory








    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to clear multiple directories stored in an array. Here's a simplified example (I have more directories).



      #!/bin/bash 

      $IMAGES_DIR="/Users/michael/scripts/imagefiles"
      $BACKUP_DIR="/Users/michael/scripts/imagebackups"

      ...

      array=( $IMAGES_DIR $BACKUP_DIR )
      for i in $array[@]
      do
      if [ "$(ls -A $i)" ]; then # check that directory has files in it
      rm "$i/"* # remove them
      fi
      done


      I get errors for each directory, e.g.:




      rm: /Users/michael/scripts/imagefiles/*: No such file or directory








      share|improve this question












      I am trying to clear multiple directories stored in an array. Here's a simplified example (I have more directories).



      #!/bin/bash 

      $IMAGES_DIR="/Users/michael/scripts/imagefiles"
      $BACKUP_DIR="/Users/michael/scripts/imagebackups"

      ...

      array=( $IMAGES_DIR $BACKUP_DIR )
      for i in $array[@]
      do
      if [ "$(ls -A $i)" ]; then # check that directory has files in it
      rm "$i/"* # remove them
      fi
      done


      I get errors for each directory, e.g.:




      rm: /Users/michael/scripts/imagefiles/*: No such file or directory










      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 24 '17 at 2:07









      Michael Riordan

      385




      385




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          How about accomplishing it all in a single command?



          You can capture the file existence check, globbing and removal with one find call. In the case of GNU's version of find we'd have this:



          for f in "$array[@]"; do
          find "$f" -type f -delete
          done


          If you don't have GNU find use this invocation:



          find "$f" -type f -exec rm -f +


          (If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1 before -type f.)



          But wait, there's more....



          As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find:



           find "$array[@]" -type f -delete


          That's because: 1) find will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find.






          share|improve this answer


















          • 1




            Simpler: find "$array[@]" -type f -delete
            – John1024
            Dec 24 '17 at 3:53






          • 1




            Good one @John1024 ... I shoulda thought of that.
            – B Layer
            Dec 24 '17 at 3:54

















          up vote
          1
          down vote













          Change your code to this:



          #!/bin/bash 

          IMAGES_DIR="/Users/michael/scripts/imagefiles"
          BACKUP_DIR="/Users/michael/scripts/imagebackups"

          array=( $IMAGES_DIR $BACKUP_DIR )
          for i in "$array[@]"
          do
          if [ "$(ls -A "$i")" ]; then
          rm "$i:?"/*
          fi
          done


          Errors:



          1. Placing $ on the left hand side of variable assignments

          2. Not quoting the $i in if [ "$(ls -A $i)" ];then

          3. Use "$var:?" to ensure this, rm "$i/"* never expands to /*





          share|improve this answer






















          • Let me test it, thanks for the HUP!
            – George Udosen
            Dec 24 '17 at 3:03










          • Ok tested on two directories and worked!
            – George Udosen
            Dec 24 '17 at 3:27

















          up vote
          -1
          down vote













          Please find the below awk oneliner to achieve the same , As tested it worked fine




          i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh 



          for example i have assigned variable i = path /root/ you can change as per your requirement



          i="/root/" ===> path






          share|improve this answer




















          • See Why you shouldn't parse the output of ls(1)
            – steeldriver
            Dec 24 '17 at 13:58










          • @steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
            – Praveen Kumar BS
            Dec 25 '17 at 2:53










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412749%2fclear-multiple-directories-with-rm%23new-answer', 'question_page');

          );

          Post as a guest






























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          How about accomplishing it all in a single command?



          You can capture the file existence check, globbing and removal with one find call. In the case of GNU's version of find we'd have this:



          for f in "$array[@]"; do
          find "$f" -type f -delete
          done


          If you don't have GNU find use this invocation:



          find "$f" -type f -exec rm -f +


          (If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1 before -type f.)



          But wait, there's more....



          As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find:



           find "$array[@]" -type f -delete


          That's because: 1) find will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find.






          share|improve this answer


















          • 1




            Simpler: find "$array[@]" -type f -delete
            – John1024
            Dec 24 '17 at 3:53






          • 1




            Good one @John1024 ... I shoulda thought of that.
            – B Layer
            Dec 24 '17 at 3:54














          up vote
          3
          down vote













          How about accomplishing it all in a single command?



          You can capture the file existence check, globbing and removal with one find call. In the case of GNU's version of find we'd have this:



          for f in "$array[@]"; do
          find "$f" -type f -delete
          done


          If you don't have GNU find use this invocation:



          find "$f" -type f -exec rm -f +


          (If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1 before -type f.)



          But wait, there's more....



          As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find:



           find "$array[@]" -type f -delete


          That's because: 1) find will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find.






          share|improve this answer


















          • 1




            Simpler: find "$array[@]" -type f -delete
            – John1024
            Dec 24 '17 at 3:53






          • 1




            Good one @John1024 ... I shoulda thought of that.
            – B Layer
            Dec 24 '17 at 3:54












          up vote
          3
          down vote










          up vote
          3
          down vote









          How about accomplishing it all in a single command?



          You can capture the file existence check, globbing and removal with one find call. In the case of GNU's version of find we'd have this:



          for f in "$array[@]"; do
          find "$f" -type f -delete
          done


          If you don't have GNU find use this invocation:



          find "$f" -type f -exec rm -f +


          (If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1 before -type f.)



          But wait, there's more....



          As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find:



           find "$array[@]" -type f -delete


          That's because: 1) find will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find.






          share|improve this answer














          How about accomplishing it all in a single command?



          You can capture the file existence check, globbing and removal with one find call. In the case of GNU's version of find we'd have this:



          for f in "$array[@]"; do
          find "$f" -type f -delete
          done


          If you don't have GNU find use this invocation:



          find "$f" -type f -exec rm -f +


          (If instead of clearing files from the entire directory hierarchy you only want to clear files that are immediate children then add -maxdepth 1 before -type f.)



          But wait, there's more....



          As John1024 wisely notes you can forgo the loop altogether by passing the array as the first parameter to find:



           find "$array[@]" -type f -delete


          That's because: 1) find will accept multiple directories to be searched and processed in one execution 2) the shell will split the array such that each element (directory) becomes an individual positional parameter to find.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 24 '17 at 4:36

























          answered Dec 24 '17 at 3:46









          B Layer

          3,8991525




          3,8991525







          • 1




            Simpler: find "$array[@]" -type f -delete
            – John1024
            Dec 24 '17 at 3:53






          • 1




            Good one @John1024 ... I shoulda thought of that.
            – B Layer
            Dec 24 '17 at 3:54












          • 1




            Simpler: find "$array[@]" -type f -delete
            – John1024
            Dec 24 '17 at 3:53






          • 1




            Good one @John1024 ... I shoulda thought of that.
            – B Layer
            Dec 24 '17 at 3:54







          1




          1




          Simpler: find "$array[@]" -type f -delete
          – John1024
          Dec 24 '17 at 3:53




          Simpler: find "$array[@]" -type f -delete
          – John1024
          Dec 24 '17 at 3:53




          1




          1




          Good one @John1024 ... I shoulda thought of that.
          – B Layer
          Dec 24 '17 at 3:54




          Good one @John1024 ... I shoulda thought of that.
          – B Layer
          Dec 24 '17 at 3:54












          up vote
          1
          down vote













          Change your code to this:



          #!/bin/bash 

          IMAGES_DIR="/Users/michael/scripts/imagefiles"
          BACKUP_DIR="/Users/michael/scripts/imagebackups"

          array=( $IMAGES_DIR $BACKUP_DIR )
          for i in "$array[@]"
          do
          if [ "$(ls -A "$i")" ]; then
          rm "$i:?"/*
          fi
          done


          Errors:



          1. Placing $ on the left hand side of variable assignments

          2. Not quoting the $i in if [ "$(ls -A $i)" ];then

          3. Use "$var:?" to ensure this, rm "$i/"* never expands to /*





          share|improve this answer






















          • Let me test it, thanks for the HUP!
            – George Udosen
            Dec 24 '17 at 3:03










          • Ok tested on two directories and worked!
            – George Udosen
            Dec 24 '17 at 3:27














          up vote
          1
          down vote













          Change your code to this:



          #!/bin/bash 

          IMAGES_DIR="/Users/michael/scripts/imagefiles"
          BACKUP_DIR="/Users/michael/scripts/imagebackups"

          array=( $IMAGES_DIR $BACKUP_DIR )
          for i in "$array[@]"
          do
          if [ "$(ls -A "$i")" ]; then
          rm "$i:?"/*
          fi
          done


          Errors:



          1. Placing $ on the left hand side of variable assignments

          2. Not quoting the $i in if [ "$(ls -A $i)" ];then

          3. Use "$var:?" to ensure this, rm "$i/"* never expands to /*





          share|improve this answer






















          • Let me test it, thanks for the HUP!
            – George Udosen
            Dec 24 '17 at 3:03










          • Ok tested on two directories and worked!
            – George Udosen
            Dec 24 '17 at 3:27












          up vote
          1
          down vote










          up vote
          1
          down vote









          Change your code to this:



          #!/bin/bash 

          IMAGES_DIR="/Users/michael/scripts/imagefiles"
          BACKUP_DIR="/Users/michael/scripts/imagebackups"

          array=( $IMAGES_DIR $BACKUP_DIR )
          for i in "$array[@]"
          do
          if [ "$(ls -A "$i")" ]; then
          rm "$i:?"/*
          fi
          done


          Errors:



          1. Placing $ on the left hand side of variable assignments

          2. Not quoting the $i in if [ "$(ls -A $i)" ];then

          3. Use "$var:?" to ensure this, rm "$i/"* never expands to /*





          share|improve this answer














          Change your code to this:



          #!/bin/bash 

          IMAGES_DIR="/Users/michael/scripts/imagefiles"
          BACKUP_DIR="/Users/michael/scripts/imagebackups"

          array=( $IMAGES_DIR $BACKUP_DIR )
          for i in "$array[@]"
          do
          if [ "$(ls -A "$i")" ]; then
          rm "$i:?"/*
          fi
          done


          Errors:



          1. Placing $ on the left hand side of variable assignments

          2. Not quoting the $i in if [ "$(ls -A $i)" ];then

          3. Use "$var:?" to ensure this, rm "$i/"* never expands to /*






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 24 '17 at 3:33

























          answered Dec 24 '17 at 2:57









          George Udosen

          1,112318




          1,112318











          • Let me test it, thanks for the HUP!
            – George Udosen
            Dec 24 '17 at 3:03










          • Ok tested on two directories and worked!
            – George Udosen
            Dec 24 '17 at 3:27
















          • Let me test it, thanks for the HUP!
            – George Udosen
            Dec 24 '17 at 3:03










          • Ok tested on two directories and worked!
            – George Udosen
            Dec 24 '17 at 3:27















          Let me test it, thanks for the HUP!
          – George Udosen
          Dec 24 '17 at 3:03




          Let me test it, thanks for the HUP!
          – George Udosen
          Dec 24 '17 at 3:03












          Ok tested on two directories and worked!
          – George Udosen
          Dec 24 '17 at 3:27




          Ok tested on two directories and worked!
          – George Udosen
          Dec 24 '17 at 3:27










          up vote
          -1
          down vote













          Please find the below awk oneliner to achieve the same , As tested it worked fine




          i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh 



          for example i have assigned variable i = path /root/ you can change as per your requirement



          i="/root/" ===> path






          share|improve this answer




















          • See Why you shouldn't parse the output of ls(1)
            – steeldriver
            Dec 24 '17 at 13:58










          • @steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
            – Praveen Kumar BS
            Dec 25 '17 at 2:53














          up vote
          -1
          down vote













          Please find the below awk oneliner to achieve the same , As tested it worked fine




          i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh 



          for example i have assigned variable i = path /root/ you can change as per your requirement



          i="/root/" ===> path






          share|improve this answer




















          • See Why you shouldn't parse the output of ls(1)
            – steeldriver
            Dec 24 '17 at 13:58










          • @steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
            – Praveen Kumar BS
            Dec 25 '17 at 2:53












          up vote
          -1
          down vote










          up vote
          -1
          down vote









          Please find the below awk oneliner to achieve the same , As tested it worked fine




          i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh 



          for example i have assigned variable i = path /root/ you can change as per your requirement



          i="/root/" ===> path






          share|improve this answer












          Please find the below awk oneliner to achieve the same , As tested it worked fine




          i="/root/";ls -ltr /root/| grep "^-rw" | awk -v i="$i" 'print "rm -rvf" " " i$9' | sh 



          for example i have assigned variable i = path /root/ you can change as per your requirement



          i="/root/" ===> path







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 24 '17 at 7:56









          Praveen Kumar BS

          1,010128




          1,010128











          • See Why you shouldn't parse the output of ls(1)
            – steeldriver
            Dec 24 '17 at 13:58










          • @steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
            – Praveen Kumar BS
            Dec 25 '17 at 2:53
















          • See Why you shouldn't parse the output of ls(1)
            – steeldriver
            Dec 24 '17 at 13:58










          • @steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
            – Praveen Kumar BS
            Dec 25 '17 at 2:53















          See Why you shouldn't parse the output of ls(1)
          – steeldriver
          Dec 24 '17 at 13:58




          See Why you shouldn't parse the output of ls(1)
          – steeldriver
          Dec 24 '17 at 13:58












          @steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
          – Praveen Kumar BS
          Dec 25 '17 at 2:53




          @steeldriver As cross verified from above command I am getting the same result let me know if there is any difference in output
          – Praveen Kumar BS
          Dec 25 '17 at 2:53












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412749%2fclear-multiple-directories-with-rm%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay