moving a (file | directory) while avoiding filename collisions

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











up vote
2
down vote

favorite












I have a bash script that moves files from a number of different locations to a folder named completed.



I want to avoid overwriting previous files, so in the case when the name of a file (for example, Selection Of Recipes.zip) I want to move is already in completed, add a nonce or other string to the filename to differentiate (Selection of Recipes-???.zip, where ??? is a random string).



Is this possible with just mv, or should I try creating another bash script with arguments that handles that aspect? Does anyone have a bash script that I can pattern my own against?










share|improve this question



























    up vote
    2
    down vote

    favorite












    I have a bash script that moves files from a number of different locations to a folder named completed.



    I want to avoid overwriting previous files, so in the case when the name of a file (for example, Selection Of Recipes.zip) I want to move is already in completed, add a nonce or other string to the filename to differentiate (Selection of Recipes-???.zip, where ??? is a random string).



    Is this possible with just mv, or should I try creating another bash script with arguments that handles that aspect? Does anyone have a bash script that I can pattern my own against?










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a bash script that moves files from a number of different locations to a folder named completed.



      I want to avoid overwriting previous files, so in the case when the name of a file (for example, Selection Of Recipes.zip) I want to move is already in completed, add a nonce or other string to the filename to differentiate (Selection of Recipes-???.zip, where ??? is a random string).



      Is this possible with just mv, or should I try creating another bash script with arguments that handles that aspect? Does anyone have a bash script that I can pattern my own against?










      share|improve this question















      I have a bash script that moves files from a number of different locations to a folder named completed.



      I want to avoid overwriting previous files, so in the case when the name of a file (for example, Selection Of Recipes.zip) I want to move is already in completed, add a nonce or other string to the filename to differentiate (Selection of Recipes-???.zip, where ??? is a random string).



      Is this possible with just mv, or should I try creating another bash script with arguments that handles that aspect? Does anyone have a bash script that I can pattern my own against?







      bash shell-script mv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 21 at 3:11









      Rui F Ribeiro

      36.7k1271116




      36.7k1271116










      asked Jul 28 '14 at 19:13









      taserian

      1134




      1134




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          If you're using GNU mv you have the following option.



          $ mv -b source/* dest/.


          This switch tells mv to push any files that collide in the dest/. directory to a backed up version, typically adding a tilde (~) to the end of the file, prior to moving files into the directory.



          Example



          Say I have the following sample directories with files.



          $ mkdir source dest
          $ touch source/file1..3 dest/file1..5

          $ tree
          .
          ├── dest
          │   ├── file1
          │   ├── file2
          │   ├── file3
          │   ├── file4
          │   └── file5
          └── source
          ├── file1
          ├── file2
          └── file3


          Now when we move files from source to dest:



          $ mv -b source/* dest/.
          $ tree
          .
          ├── dest
          │   ├── file1
          │   ├── file1~
          │   ├── file2
          │   ├── file2~
          │   ├── file3
          │   ├── file3~
          │   ├── file4
          │   └── file5
          └── source

          2 directories, 8 files


          Controlling the extension



          Again with GNU's version of mv you can change the default behavior using the -S <string> switch.



          $ mv -b -S "string" source/* dest/.


          Example



          $ mv -b -S .old source/* dest/.

          $ tree
          .
          ├── dest
          │   ├── file1
          │   ├── file1.old
          │   ├── file2
          │   ├── file2.old
          │   ├── file3
          │   ├── file3.old
          │   ├── file4
          │   └── file5
          └── source

          2 directories, 8 files





          share|improve this answer





























            up vote
            0
            down vote













            Here is the script. If you call it move, you will have to use the command move file destination



            #!/bin/bash

            FILE=$1
            DEST=$2

            if `ls $DEST/$FILE > /dev/null`
            then
            base=`echo $FILE | sed 's/(.*)..*/1/'`
            ext=`echo $FILE | sed 's/.*.(.*)/1'`
            r=$RANDOM
            mv $FILE $DEST/$base-$r.$ext
            else
            mv $FILE $DEST
            fi


            You probably should add some validation (like making sure that the random numbered file does not exist already) and the fact that the user supplies two arguments.






            share|improve this answer




















              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%2f147059%2fmoving-a-file-directory-while-avoiding-filename-collisions%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote



              accepted










              If you're using GNU mv you have the following option.



              $ mv -b source/* dest/.


              This switch tells mv to push any files that collide in the dest/. directory to a backed up version, typically adding a tilde (~) to the end of the file, prior to moving files into the directory.



              Example



              Say I have the following sample directories with files.



              $ mkdir source dest
              $ touch source/file1..3 dest/file1..5

              $ tree
              .
              ├── dest
              │   ├── file1
              │   ├── file2
              │   ├── file3
              │   ├── file4
              │   └── file5
              └── source
              ├── file1
              ├── file2
              └── file3


              Now when we move files from source to dest:



              $ mv -b source/* dest/.
              $ tree
              .
              ├── dest
              │   ├── file1
              │   ├── file1~
              │   ├── file2
              │   ├── file2~
              │   ├── file3
              │   ├── file3~
              │   ├── file4
              │   └── file5
              └── source

              2 directories, 8 files


              Controlling the extension



              Again with GNU's version of mv you can change the default behavior using the -S <string> switch.



              $ mv -b -S "string" source/* dest/.


              Example



              $ mv -b -S .old source/* dest/.

              $ tree
              .
              ├── dest
              │   ├── file1
              │   ├── file1.old
              │   ├── file2
              │   ├── file2.old
              │   ├── file3
              │   ├── file3.old
              │   ├── file4
              │   └── file5
              └── source

              2 directories, 8 files





              share|improve this answer


























                up vote
                5
                down vote



                accepted










                If you're using GNU mv you have the following option.



                $ mv -b source/* dest/.


                This switch tells mv to push any files that collide in the dest/. directory to a backed up version, typically adding a tilde (~) to the end of the file, prior to moving files into the directory.



                Example



                Say I have the following sample directories with files.



                $ mkdir source dest
                $ touch source/file1..3 dest/file1..5

                $ tree
                .
                ├── dest
                │   ├── file1
                │   ├── file2
                │   ├── file3
                │   ├── file4
                │   └── file5
                └── source
                ├── file1
                ├── file2
                └── file3


                Now when we move files from source to dest:



                $ mv -b source/* dest/.
                $ tree
                .
                ├── dest
                │   ├── file1
                │   ├── file1~
                │   ├── file2
                │   ├── file2~
                │   ├── file3
                │   ├── file3~
                │   ├── file4
                │   └── file5
                └── source

                2 directories, 8 files


                Controlling the extension



                Again with GNU's version of mv you can change the default behavior using the -S <string> switch.



                $ mv -b -S "string" source/* dest/.


                Example



                $ mv -b -S .old source/* dest/.

                $ tree
                .
                ├── dest
                │   ├── file1
                │   ├── file1.old
                │   ├── file2
                │   ├── file2.old
                │   ├── file3
                │   ├── file3.old
                │   ├── file4
                │   └── file5
                └── source

                2 directories, 8 files





                share|improve this answer
























                  up vote
                  5
                  down vote



                  accepted







                  up vote
                  5
                  down vote



                  accepted






                  If you're using GNU mv you have the following option.



                  $ mv -b source/* dest/.


                  This switch tells mv to push any files that collide in the dest/. directory to a backed up version, typically adding a tilde (~) to the end of the file, prior to moving files into the directory.



                  Example



                  Say I have the following sample directories with files.



                  $ mkdir source dest
                  $ touch source/file1..3 dest/file1..5

                  $ tree
                  .
                  ├── dest
                  │   ├── file1
                  │   ├── file2
                  │   ├── file3
                  │   ├── file4
                  │   └── file5
                  └── source
                  ├── file1
                  ├── file2
                  └── file3


                  Now when we move files from source to dest:



                  $ mv -b source/* dest/.
                  $ tree
                  .
                  ├── dest
                  │   ├── file1
                  │   ├── file1~
                  │   ├── file2
                  │   ├── file2~
                  │   ├── file3
                  │   ├── file3~
                  │   ├── file4
                  │   └── file5
                  └── source

                  2 directories, 8 files


                  Controlling the extension



                  Again with GNU's version of mv you can change the default behavior using the -S <string> switch.



                  $ mv -b -S "string" source/* dest/.


                  Example



                  $ mv -b -S .old source/* dest/.

                  $ tree
                  .
                  ├── dest
                  │   ├── file1
                  │   ├── file1.old
                  │   ├── file2
                  │   ├── file2.old
                  │   ├── file3
                  │   ├── file3.old
                  │   ├── file4
                  │   └── file5
                  └── source

                  2 directories, 8 files





                  share|improve this answer














                  If you're using GNU mv you have the following option.



                  $ mv -b source/* dest/.


                  This switch tells mv to push any files that collide in the dest/. directory to a backed up version, typically adding a tilde (~) to the end of the file, prior to moving files into the directory.



                  Example



                  Say I have the following sample directories with files.



                  $ mkdir source dest
                  $ touch source/file1..3 dest/file1..5

                  $ tree
                  .
                  ├── dest
                  │   ├── file1
                  │   ├── file2
                  │   ├── file3
                  │   ├── file4
                  │   └── file5
                  └── source
                  ├── file1
                  ├── file2
                  └── file3


                  Now when we move files from source to dest:



                  $ mv -b source/* dest/.
                  $ tree
                  .
                  ├── dest
                  │   ├── file1
                  │   ├── file1~
                  │   ├── file2
                  │   ├── file2~
                  │   ├── file3
                  │   ├── file3~
                  │   ├── file4
                  │   └── file5
                  └── source

                  2 directories, 8 files


                  Controlling the extension



                  Again with GNU's version of mv you can change the default behavior using the -S <string> switch.



                  $ mv -b -S "string" source/* dest/.


                  Example



                  $ mv -b -S .old source/* dest/.

                  $ tree
                  .
                  ├── dest
                  │   ├── file1
                  │   ├── file1.old
                  │   ├── file2
                  │   ├── file2.old
                  │   ├── file3
                  │   ├── file3.old
                  │   ├── file4
                  │   └── file5
                  └── source

                  2 directories, 8 files






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 28 '14 at 21:06

























                  answered Jul 28 '14 at 20:59









                  slm♦

                  238k65493664




                  238k65493664






















                      up vote
                      0
                      down vote













                      Here is the script. If you call it move, you will have to use the command move file destination



                      #!/bin/bash

                      FILE=$1
                      DEST=$2

                      if `ls $DEST/$FILE > /dev/null`
                      then
                      base=`echo $FILE | sed 's/(.*)..*/1/'`
                      ext=`echo $FILE | sed 's/.*.(.*)/1'`
                      r=$RANDOM
                      mv $FILE $DEST/$base-$r.$ext
                      else
                      mv $FILE $DEST
                      fi


                      You probably should add some validation (like making sure that the random numbered file does not exist already) and the fact that the user supplies two arguments.






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        Here is the script. If you call it move, you will have to use the command move file destination



                        #!/bin/bash

                        FILE=$1
                        DEST=$2

                        if `ls $DEST/$FILE > /dev/null`
                        then
                        base=`echo $FILE | sed 's/(.*)..*/1/'`
                        ext=`echo $FILE | sed 's/.*.(.*)/1'`
                        r=$RANDOM
                        mv $FILE $DEST/$base-$r.$ext
                        else
                        mv $FILE $DEST
                        fi


                        You probably should add some validation (like making sure that the random numbered file does not exist already) and the fact that the user supplies two arguments.






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Here is the script. If you call it move, you will have to use the command move file destination



                          #!/bin/bash

                          FILE=$1
                          DEST=$2

                          if `ls $DEST/$FILE > /dev/null`
                          then
                          base=`echo $FILE | sed 's/(.*)..*/1/'`
                          ext=`echo $FILE | sed 's/.*.(.*)/1'`
                          r=$RANDOM
                          mv $FILE $DEST/$base-$r.$ext
                          else
                          mv $FILE $DEST
                          fi


                          You probably should add some validation (like making sure that the random numbered file does not exist already) and the fact that the user supplies two arguments.






                          share|improve this answer












                          Here is the script. If you call it move, you will have to use the command move file destination



                          #!/bin/bash

                          FILE=$1
                          DEST=$2

                          if `ls $DEST/$FILE > /dev/null`
                          then
                          base=`echo $FILE | sed 's/(.*)..*/1/'`
                          ext=`echo $FILE | sed 's/.*.(.*)/1'`
                          r=$RANDOM
                          mv $FILE $DEST/$base-$r.$ext
                          else
                          mv $FILE $DEST
                          fi


                          You probably should add some validation (like making sure that the random numbered file does not exist already) and the fact that the user supplies two arguments.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 28 '14 at 19:34









                          unxnut

                          3,4102918




                          3,4102918



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f147059%2fmoving-a-file-directory-while-avoiding-filename-collisions%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)