moving a (file | directory) while avoiding filename collisions

Clash 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?
bash shell-script mv
add a comment |Â
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?
bash shell-script mv
add a comment |Â
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?
bash shell-script mv
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
bash shell-script mv
edited Aug 21 at 3:11
Rui F Ribeiro
36.7k1271116
36.7k1271116
asked Jul 28 '14 at 19:13
taserian
1134
1134
add a comment |Â
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Jul 28 '14 at 21:06
answered Jul 28 '14 at 20:59
slmâ¦
238k65493664
238k65493664
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Jul 28 '14 at 19:34
unxnut
3,4102918
3,4102918
add a comment |Â
add a comment |Â
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
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
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
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
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