LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2004, 08:16 AM   #1
LarsWestergren
LQ Newbie
 
Registered: Sep 2003
Posts: 4

Rep: Reputation: 0
Bash - rename directory to overwrite existing


Hi,

Not a newbie actually, but I have a very newbie-ish question.

I have two existing directories, test1 and test2. I wish to rename test1 to test2 with bash, and overwrite test2. I can't find out how to do it!

mv test1 test2
This moves test1 INTO test2. And so does every other variation I can think of. Googling for "move rename directory bash linux" etc brings up thousands of helpful pages, but all I have read assume that test2 does not exist.

I can do it in two steps of course (rm -Rf test2; mv test1 test2) but I am wondering if it can be done in one step.

Greatful for any help.
 
Old 11-11-2004, 09:06 AM   #2
r0b0
Member
 
Registered: Aug 2004
Location: Europe
Posts: 608

Rep: Reputation: 50
In unix (and linux as well) you generally have small tools that do simple jobs and do them well.
These are rm and mv. There is no "rename to overwrite existing" tool. If you really need it, create a script for it.
 
Old 01-02-2009, 05:07 PM   #3
Breakable
LQ Newbie
 
Registered: Sep 2005
Posts: 1

Rep: Reputation: 0
Was searching the internet myself until figured it out myself.

to move directory contents use command
mv sourceDirectory\* destinationDirectory\

to overwrite add the " -f"

A little late but better than never
 
Old 07-14-2010, 05:55 AM   #4
oli7er
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
Just came across the same issue.

A quick look into the man pages yields
Quote:
man mv
-T, --no-target-directory
treat DEST as a normal file
which is exactly what we need here, i.e., "mv -T test1 test2" should do the job.

@r0b0: your statement is correct, though not terribly helpful. In fact, the little helpers also come with command line switches!

@Breakable: The '-f' (to force) does not seem to change the 'move-into' versus 'overwrite' behaviour. Also note that Unix/Linux use the regular slash rather than backslash, which would escape the "*" in this case.

Last edited by oli7er; 07-14-2010 at 05:58 AM.
 
Old 07-14-2010, 07:17 AM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Hi,

Welcome to LQ!

I don't think you should expect a reply. Look at the thread dates. Resurrecting a six year old thread??
 
Old 07-27-2010, 11:15 PM   #6
jasonh_
LQ Newbie
 
Registered: Jul 2010
Posts: 1

Rep: Reputation: 0
Red face

Quote:
Originally Posted by oli7er View Post
Just came across the same issue.

A quick look into the man pages yields

which is exactly what we need here, i.e., "mv -T test1 test2" should do the job.

@r0b0: your statement is correct, though not terribly helpful. In fact, the little helpers also come with command line switches!

@Breakable: The '-f' (to force) does not seem to change the 'move-into' versus 'overwrite' behaviour. Also note that Unix/Linux use the regular slash rather than backslash, which would escape the "*" in this case.

thanks for providing your solution. i did a quick google search and found this resurrected thread.

@onebuck surprise!!!!!!!!!!
 
Old 09-14-2011, 02:33 PM   #7
Albert25
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
mv behavior depends on whether target exists

This thread may be almost 7 years old now, but there is still no clear answer.

What I found on my system ( $ mv --version : mv (GNU coreutils) 8.5) ) is that
  • if the destination doesn't exist, mv does a rename
  • if the destination exists, the source gets moved into it.

Here is some more testing:
Code:
$ mkdir src
$ mv -v src dest
`src' -> `dest'

  # now that "dest" already exists, the result is different
$ mkdir src
$ mv -v src dest
`src' -> `dest/src'

  # -T doesn't really help. Nor does -f
$ mkdir src2
$ mv -v -T src2 dest
mv: cannot move `src2' to `dest': Directory not empty
$ mv -v -T -f src2 dest
mv: cannot move `src2' to `dest': Directory not empty
What about the "rename" Perl script which comes with Debian?


Code:
$ mkdir src
$ rm -rf dest
$ rename -v 's/src/dest/' src
src renamed as dest

  # that was like mv

  # now that dest exists:

$ rename -v 's/src2/dest/' src2
src2 not renamed: dest already exists

  # BUT with -f (force), it seems to work:
$ rename -vf 's/src2/dest/' src2
src2 renamed as dest
 
Old 09-14-2011, 06:22 PM   #8
okomba
LQ Newbie
 
Registered: Sep 2011
Location: Nairobi
Distribution: Fedora, Ubuntu
Posts: 11

Rep: Reputation: 0
Hi.

Maybe this could help

Code:
cp -Rf test1\* test2\  | rm -Rf test1
Cheers

Last edited by colucix; 09-22-2011 at 01:24 AM.
 
Old 09-29-2011, 02:12 AM   #9
Pansmanse
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Rep: Reputation: Disabled
Complete Newbie, but I was playing with this problem and came up with the same solution as okomba. To overwrite the destination directory I found I had to delete it first. So I have a script which looks like this.

Code:
rm -R test2\
cp -R test1\* test2\
rm -R test1
Since we are no longer overwriting anything the -f is not needed, and actually I found -r was adequate.
 
Old 09-29-2011, 02:54 AM   #10
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
Quote:
Originally Posted by Pansmanse View Post
Complete Newbie, but I was playing with this problem and came up with the same solution as okomba. To overwrite the destination directory I found I had to delete it first. So I have a script which looks like this.

Code:
rm -R test2\
cp -R test1\* test2\
rm -R test1
Since we are no longer overwriting anything the -f is not needed, and actually I found -r was adequate.
Your solution is wacky and won't work. You've got backslashes on the end of two lines, which would mean that your code is equivalent to:

rm -R test2cp -R test1\* test2rm -R test1

I'm not really sure what that command would do, but I bet it's not what you want.
 
Old 09-29-2011, 04:21 AM   #11
Pansmanse
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Rep: Reputation: Disabled
Sorry, I actually have forward slashes in my script and they do work. Copied this from the previous entry.
 
Old 09-29-2011, 04:45 AM   #12
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
Quote:
Originally Posted by Pansmanse View Post
Sorry, I actually have forward slashes in my script and they do work. Copied this from the previous entry.
If you have slashes instead of backslashes, that makes a big difference. You should still be careful though. If you make a mistake and call the script with the wrong arguments, you might delete something you didn't mean to.

Imagine what will happen if the result of calling your script is the following command. Just imagine it, don't try it!!

# rm -r / # don't run this command!

That could possibly happen if your script has some slight problems or if you aren't very careful with how you call it.

Last edited by snooly; 09-29-2011 at 04:46 AM.
 
Old 08-22-2012, 10:51 AM   #13
Moorscode
LQ Newbie
 
Registered: Aug 2012
Posts: 1

Rep: Reputation: Disabled
Workaround

I've been wondering on how to do this aswel and the only working solution for my problem is to make a symlink to the directory that want to use the files from.

So instead of move/renaming a directory into another just make the symlink point to the new source.

Starting point:
dir1/file1
dir2/file2

Create the 'final' point where the files are in that are needed:
ln dir2 final -s

Now switch directory content:
ln dir1 final -sTf

This way you never have a missing directory and the switch is instantaneous.
Not sure if this was your goal aswel, but I'm working on a seemless checkout system on a heavily used directory structure.
The only downside is that you can't easily tell (without commands) what directory the 'final' is pointing at..
A solution would be deleting the directory that was previously linked after switching the link.

So make that an 8 year topic, still relevant.
 
Old 08-22-2012, 09:11 PM   #14
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 91
Quote:
Originally Posted by okomba View Post
Hi.

Maybe this could help

Code:
cp -Rf test1\* test2\  | rm -Rf test1
Cheers
You need a forward slash instead a backward one and the pipe isn't required.
Code:
mv -rf test1/* test2/; rm -rf test1
You were one the right track! The semicolon is there to let BASH know the first command sequence is done and a new one begins. Substituting the "cp" command with the "mv" command isn't necessary, just an alternative showing what else is possible.
 
Old 10-11-2012, 12:03 AM   #15
owenmck
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by SharpyWarpy View Post
You need a forward slash instead a backward one and the pipe isn't required.
Code:
mv -rf test1/* test2/; rm -rf test1
You were one the right track! The semicolon is there to let BASH know the first command sequence is done and a new one begins. Substituting the "cp" command with the "mv" command isn't necessary, just an alternative showing what else is possible.
My ubuntu's man page for mv doesn't mention -r (but cp's does) ... perhaps stick with cp, not the mv alternative!

So, after 8 years, perhaps try
Code:
cp -rf test1/* test2/; rm -rf test1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rename all files in a directory merlin23 Linux - Newbie 4 03-24-2005 11:02 AM
bash wont overwrite variables xushi Programming 4 03-02-2005 04:50 PM
will yum add kernel version or overwrite existing? Kropotkin Fedora 3 11-20-2004 05:10 AM
rename all files in my directory BabaKali Linux - Newbie 2 11-10-2004 04:27 PM
overwrite directory copy medamnit Linux - Newbie 1 05-26-2002 12:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration