How to definitely move a file without overwritting anything Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionMove file to another user's home directory (without sudo)?mv misbehaves in shell scriptMove directory (with contents) to another user's home directory (without sudo)?Recursive move (`mv -rn`, like `cp -rn`), a move that will only move not present filesMerely moving a directory inside another using mvCreate directory name based on other other directoriesHow can I move a file within a directory to the current working directory without renaming?Problem using mvfind and move files without overwriting and with outputMove or copy without overwrite and check success

What would be the ideal power source for a cybernetic eye?

Generate an RGB colour grid

Understanding Ceva's Theorem

Sci-Fi book where patients in a coma ward all live in a subconscious world linked together

What does an IRS interview request entail when called in to verify expenses for a sole proprietor small business?

Compare a given version number in the form major.minor.build.patch and see if one is less than the other

How to find all the available tools in mac terminal?

What exactly is a "Meth" in Altered Carbon?

Storing hydrofluoric acid before the invention of plastics

ListPlot join points by nearest neighbor rather than order

Is it true that "carbohydrates are of no use for the basal metabolic need"?

Why did the Falcon Heavy center core fall off the ASDS OCISLY barge?

Identifying polygons that intersect with another layer using QGIS?

Echoing a tail command produces unexpected output?

Output the ŋarâþ crîþ alphabet song without using (m)any letters

How do I keep my slimes from escaping their pens?

Extract all GPU name, model and GPU ram

How to deal with a team lead who never gives me credit?

Why are Kinder Surprise Eggs illegal in the USA?

Why aren't air breathing engines used as small first stages

The logistics of corpse disposal

Error "illegal generic type for instanceof" when using local classes

How discoverable are IPv6 addresses and AAAA names by potential attackers?

Can a non-EU citizen with residency visa traveling with me come with me through the EU passport line, when entering Schengen area?



How to definitely move a file without overwritting anything



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionMove file to another user's home directory (without sudo)?mv misbehaves in shell scriptMove directory (with contents) to another user's home directory (without sudo)?Recursive move (`mv -rn`, like `cp -rn`), a move that will only move not present filesMerely moving a directory inside another using mvCreate directory name based on other other directoriesHow can I move a file within a directory to the current working directory without renaming?Problem using mvfind and move files without overwriting and with outputMove or copy without overwrite and check success



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















Is there a command or option that will allow me to move files without overwriting anything?



For the sake of argument, let us call the file foo.rar.



Now the -n sort of works, it won't overwrite the file.
But if a file is there it also won't move the file. I want the file moved except for a system error like otu of disk space.



-b sort of works too. The problem with -b is that if there is a backup file, then -b will clobber the backup file with the new backup file.



So now to add a little meat to the question let us look at our example of foo.rar .Let us do a search for foo.rar in /home. We find 10 different files with that name. What I would like to do is execute the following three commands.



mkdir /tmp/foo_files
find /home -iname "foo.rar" -exec wanted_mv_command /tmp/foo_files ;
mv /tmp/foo_files ~/


I want ~/foo_files to contain something like: foo.rar, foo.rar.1, foo.rar.2 ... foo.rar.9 .



I don't care what pattern is used. Instead of foo.rar.# it could for example use foo.#.rar > I jsut want two things. That it indicates what the original name of the file was, and that it shows distinct versions of the file.










share|improve this question




























    2















    Is there a command or option that will allow me to move files without overwriting anything?



    For the sake of argument, let us call the file foo.rar.



    Now the -n sort of works, it won't overwrite the file.
    But if a file is there it also won't move the file. I want the file moved except for a system error like otu of disk space.



    -b sort of works too. The problem with -b is that if there is a backup file, then -b will clobber the backup file with the new backup file.



    So now to add a little meat to the question let us look at our example of foo.rar .Let us do a search for foo.rar in /home. We find 10 different files with that name. What I would like to do is execute the following three commands.



    mkdir /tmp/foo_files
    find /home -iname "foo.rar" -exec wanted_mv_command /tmp/foo_files ;
    mv /tmp/foo_files ~/


    I want ~/foo_files to contain something like: foo.rar, foo.rar.1, foo.rar.2 ... foo.rar.9 .



    I don't care what pattern is used. Instead of foo.rar.# it could for example use foo.#.rar > I jsut want two things. That it indicates what the original name of the file was, and that it shows distinct versions of the file.










    share|improve this question
























      2












      2








      2








      Is there a command or option that will allow me to move files without overwriting anything?



      For the sake of argument, let us call the file foo.rar.



      Now the -n sort of works, it won't overwrite the file.
      But if a file is there it also won't move the file. I want the file moved except for a system error like otu of disk space.



      -b sort of works too. The problem with -b is that if there is a backup file, then -b will clobber the backup file with the new backup file.



      So now to add a little meat to the question let us look at our example of foo.rar .Let us do a search for foo.rar in /home. We find 10 different files with that name. What I would like to do is execute the following three commands.



      mkdir /tmp/foo_files
      find /home -iname "foo.rar" -exec wanted_mv_command /tmp/foo_files ;
      mv /tmp/foo_files ~/


      I want ~/foo_files to contain something like: foo.rar, foo.rar.1, foo.rar.2 ... foo.rar.9 .



      I don't care what pattern is used. Instead of foo.rar.# it could for example use foo.#.rar > I jsut want two things. That it indicates what the original name of the file was, and that it shows distinct versions of the file.










      share|improve this question














      Is there a command or option that will allow me to move files without overwriting anything?



      For the sake of argument, let us call the file foo.rar.



      Now the -n sort of works, it won't overwrite the file.
      But if a file is there it also won't move the file. I want the file moved except for a system error like otu of disk space.



      -b sort of works too. The problem with -b is that if there is a backup file, then -b will clobber the backup file with the new backup file.



      So now to add a little meat to the question let us look at our example of foo.rar .Let us do a search for foo.rar in /home. We find 10 different files with that name. What I would like to do is execute the following three commands.



      mkdir /tmp/foo_files
      find /home -iname "foo.rar" -exec wanted_mv_command /tmp/foo_files ;
      mv /tmp/foo_files ~/


      I want ~/foo_files to contain something like: foo.rar, foo.rar.1, foo.rar.2 ... foo.rar.9 .



      I don't care what pattern is used. Instead of foo.rar.# it could for example use foo.#.rar > I jsut want two things. That it indicates what the original name of the file was, and that it shows distinct versions of the file.







      mv






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 19 '15 at 19:31









      Mouse.The.Lucky.DogMouse.The.Lucky.Dog

      1,01711532




      1,01711532




















          2 Answers
          2






          active

          oldest

          votes


















          5














          Since you're using GNU mv, you can use its --backup option. Just turn on numbered backups.



           mv --backup=numbered file1 file2 your_dir


          You can change de suffix via the -S option.



          Here is an example:



          $ mv --backup=numbered aa/foo bb/foo cc/foo .
          $ ls
          aa bb cc foo foo.~1~ foo.~2~





          share|improve this answer
































            3














            I just made a small script, let's call it mv_safe.sh. Usage: mv_safe SOURCE_FILE TARGET_DIRECTORY



            if test ! -e "$2/$1"
            then
            mv -- "$1" "$2"
            else
            tries=1
            while test -e "$2/$1.$tries"
            do
            tries=$((tries+1))
            done
            mv -T -- "$1" "$2/$1.$tries"
            fi


            Warning: this is not atomic. If you run multiple copies of this script in parallel and they happen to target the same file, they may overwrite each other's files.



            Example : you have feefoo/foo.bar and foo.bar in your /tmp:



            $ ./mv_safe.sh foo.bar feefoo


            So here we want to move "safely" foo.bar to feefoo, where the name "foo.bar" is already used.
            Let's see whaat it gives :



            $ ls feefoo
            foo.bar foo.bar.1


            Does a usual mv if $1's name is not yet used in $2






            share|improve this answer




















            • 3





              Fails terribly if any component of the source or destination contains a space in the filename, such as mv "/tmp/my file.txt" ~ (You can fix that by quoting your variables.)

              – roaima
              Dec 19 '15 at 20:49












            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            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%2f250442%2fhow-to-definitely-move-a-file-without-overwritting-anything%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            Since you're using GNU mv, you can use its --backup option. Just turn on numbered backups.



             mv --backup=numbered file1 file2 your_dir


            You can change de suffix via the -S option.



            Here is an example:



            $ mv --backup=numbered aa/foo bb/foo cc/foo .
            $ ls
            aa bb cc foo foo.~1~ foo.~2~





            share|improve this answer





























              5














              Since you're using GNU mv, you can use its --backup option. Just turn on numbered backups.



               mv --backup=numbered file1 file2 your_dir


              You can change de suffix via the -S option.



              Here is an example:



              $ mv --backup=numbered aa/foo bb/foo cc/foo .
              $ ls
              aa bb cc foo foo.~1~ foo.~2~





              share|improve this answer



























                5












                5








                5







                Since you're using GNU mv, you can use its --backup option. Just turn on numbered backups.



                 mv --backup=numbered file1 file2 your_dir


                You can change de suffix via the -S option.



                Here is an example:



                $ mv --backup=numbered aa/foo bb/foo cc/foo .
                $ ls
                aa bb cc foo foo.~1~ foo.~2~





                share|improve this answer















                Since you're using GNU mv, you can use its --backup option. Just turn on numbered backups.



                 mv --backup=numbered file1 file2 your_dir


                You can change de suffix via the -S option.



                Here is an example:



                $ mv --backup=numbered aa/foo bb/foo cc/foo .
                $ ls
                aa bb cc foo foo.~1~ foo.~2~






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 20 '15 at 1:41









                Gilles

                548k13011131631




                548k13011131631










                answered Dec 19 '15 at 20:54









                KiraKira

                3,242925




                3,242925























                    3














                    I just made a small script, let's call it mv_safe.sh. Usage: mv_safe SOURCE_FILE TARGET_DIRECTORY



                    if test ! -e "$2/$1"
                    then
                    mv -- "$1" "$2"
                    else
                    tries=1
                    while test -e "$2/$1.$tries"
                    do
                    tries=$((tries+1))
                    done
                    mv -T -- "$1" "$2/$1.$tries"
                    fi


                    Warning: this is not atomic. If you run multiple copies of this script in parallel and they happen to target the same file, they may overwrite each other's files.



                    Example : you have feefoo/foo.bar and foo.bar in your /tmp:



                    $ ./mv_safe.sh foo.bar feefoo


                    So here we want to move "safely" foo.bar to feefoo, where the name "foo.bar" is already used.
                    Let's see whaat it gives :



                    $ ls feefoo
                    foo.bar foo.bar.1


                    Does a usual mv if $1's name is not yet used in $2






                    share|improve this answer




















                    • 3





                      Fails terribly if any component of the source or destination contains a space in the filename, such as mv "/tmp/my file.txt" ~ (You can fix that by quoting your variables.)

                      – roaima
                      Dec 19 '15 at 20:49
















                    3














                    I just made a small script, let's call it mv_safe.sh. Usage: mv_safe SOURCE_FILE TARGET_DIRECTORY



                    if test ! -e "$2/$1"
                    then
                    mv -- "$1" "$2"
                    else
                    tries=1
                    while test -e "$2/$1.$tries"
                    do
                    tries=$((tries+1))
                    done
                    mv -T -- "$1" "$2/$1.$tries"
                    fi


                    Warning: this is not atomic. If you run multiple copies of this script in parallel and they happen to target the same file, they may overwrite each other's files.



                    Example : you have feefoo/foo.bar and foo.bar in your /tmp:



                    $ ./mv_safe.sh foo.bar feefoo


                    So here we want to move "safely" foo.bar to feefoo, where the name "foo.bar" is already used.
                    Let's see whaat it gives :



                    $ ls feefoo
                    foo.bar foo.bar.1


                    Does a usual mv if $1's name is not yet used in $2






                    share|improve this answer




















                    • 3





                      Fails terribly if any component of the source or destination contains a space in the filename, such as mv "/tmp/my file.txt" ~ (You can fix that by quoting your variables.)

                      – roaima
                      Dec 19 '15 at 20:49














                    3












                    3








                    3







                    I just made a small script, let's call it mv_safe.sh. Usage: mv_safe SOURCE_FILE TARGET_DIRECTORY



                    if test ! -e "$2/$1"
                    then
                    mv -- "$1" "$2"
                    else
                    tries=1
                    while test -e "$2/$1.$tries"
                    do
                    tries=$((tries+1))
                    done
                    mv -T -- "$1" "$2/$1.$tries"
                    fi


                    Warning: this is not atomic. If you run multiple copies of this script in parallel and they happen to target the same file, they may overwrite each other's files.



                    Example : you have feefoo/foo.bar and foo.bar in your /tmp:



                    $ ./mv_safe.sh foo.bar feefoo


                    So here we want to move "safely" foo.bar to feefoo, where the name "foo.bar" is already used.
                    Let's see whaat it gives :



                    $ ls feefoo
                    foo.bar foo.bar.1


                    Does a usual mv if $1's name is not yet used in $2






                    share|improve this answer















                    I just made a small script, let's call it mv_safe.sh. Usage: mv_safe SOURCE_FILE TARGET_DIRECTORY



                    if test ! -e "$2/$1"
                    then
                    mv -- "$1" "$2"
                    else
                    tries=1
                    while test -e "$2/$1.$tries"
                    do
                    tries=$((tries+1))
                    done
                    mv -T -- "$1" "$2/$1.$tries"
                    fi


                    Warning: this is not atomic. If you run multiple copies of this script in parallel and they happen to target the same file, they may overwrite each other's files.



                    Example : you have feefoo/foo.bar and foo.bar in your /tmp:



                    $ ./mv_safe.sh foo.bar feefoo


                    So here we want to move "safely" foo.bar to feefoo, where the name "foo.bar" is already used.
                    Let's see whaat it gives :



                    $ ls feefoo
                    foo.bar foo.bar.1


                    Does a usual mv if $1's name is not yet used in $2







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 12 hours ago









                    Rui F Ribeiro

                    42.1k1484142




                    42.1k1484142










                    answered Dec 19 '15 at 20:39









                    joH1joH1

                    570620




                    570620







                    • 3





                      Fails terribly if any component of the source or destination contains a space in the filename, such as mv "/tmp/my file.txt" ~ (You can fix that by quoting your variables.)

                      – roaima
                      Dec 19 '15 at 20:49













                    • 3





                      Fails terribly if any component of the source or destination contains a space in the filename, such as mv "/tmp/my file.txt" ~ (You can fix that by quoting your variables.)

                      – roaima
                      Dec 19 '15 at 20:49








                    3




                    3





                    Fails terribly if any component of the source or destination contains a space in the filename, such as mv "/tmp/my file.txt" ~ (You can fix that by quoting your variables.)

                    – roaima
                    Dec 19 '15 at 20:49






                    Fails terribly if any component of the source or destination contains a space in the filename, such as mv "/tmp/my file.txt" ~ (You can fix that by quoting your variables.)

                    – roaima
                    Dec 19 '15 at 20:49


















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f250442%2fhow-to-definitely-move-a-file-without-overwritting-anything%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    -mv

                    Popular posts from this blog

                    Frič See also Navigation menuinternal link

                    Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                    fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox