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;
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
add a comment |
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
add a comment |
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
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
mv
asked Dec 19 '15 at 19:31
Mouse.The.Lucky.DogMouse.The.Lucky.Dog
1,01711532
1,01711532
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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~
add a comment |
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
3
Fails terribly if any component of the source or destination contains a space in the filename, such asmv "/tmp/my file.txt" ~
(You can fix that by quoting your variables.)
– roaima
Dec 19 '15 at 20:49
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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~
add a comment |
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~
add a comment |
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~
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~
edited Dec 20 '15 at 1:41
Gilles
548k13011131631
548k13011131631
answered Dec 19 '15 at 20:54
KiraKira
3,242925
3,242925
add a comment |
add a comment |
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
3
Fails terribly if any component of the source or destination contains a space in the filename, such asmv "/tmp/my file.txt" ~
(You can fix that by quoting your variables.)
– roaima
Dec 19 '15 at 20:49
add a comment |
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
3
Fails terribly if any component of the source or destination contains a space in the filename, such asmv "/tmp/my file.txt" ~
(You can fix that by quoting your variables.)
– roaima
Dec 19 '15 at 20:49
add a comment |
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
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
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 asmv "/tmp/my file.txt" ~
(You can fix that by quoting your variables.)
– roaima
Dec 19 '15 at 20:49
add a comment |
3
Fails terribly if any component of the source or destination contains a space in the filename, such asmv "/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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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