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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
11-11-2004, 09:16 AM
|
#1
|
LQ Newbie
Registered: Sep 2003
Posts: 4
Rep:
|
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.
|
|
|
11-11-2004, 10:06 AM
|
#2
|
Member
Registered: Aug 2004
Location: Europe
Posts: 608
Rep:
|
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.
|
|
|
01-02-2009, 06:07 PM
|
#3
|
LQ Newbie
Registered: Sep 2005
Posts: 1
Rep:
|
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
|
|
|
07-14-2010, 06:55 AM
|
#4
|
LQ Newbie
Registered: Jul 2010
Posts: 2
Rep:
|
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 06:58 AM.
|
|
|
07-14-2010, 08:17 AM
|
#5
|
Moderator
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,971
|
Hi,
Welcome to LQ!
I don't think you should expect a reply. Look at the thread dates. Resurrecting a six year old thread??
|
|
|
07-28-2010, 12:15 AM
|
#6
|
LQ Newbie
Registered: Jul 2010
Posts: 1
Rep:
|
Quote:
Originally Posted by oli7er
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!!!!!!!!!!
|
|
|
09-14-2011, 03:33 PM
|
#7
|
LQ Newbie
Registered: Sep 2011
Posts: 2
Rep:
|
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
|
|
|
09-14-2011, 07:22 PM
|
#8
|
LQ Newbie
Registered: Sep 2011
Location: Nairobi
Distribution: Fedora, Ubuntu
Posts: 11
Rep:
|
Hi.
Maybe this could help
Code:
cp -Rf test1\* test2\ | rm -Rf test1
Cheers
Last edited by colucix; 09-22-2011 at 02:24 AM.
|
|
|
09-29-2011, 03:12 AM
|
#9
|
LQ Newbie
Registered: Sep 2011
Posts: 4
Rep:
|
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.
|
|
|
09-29-2011, 03:54 AM
|
#10
|
Member
Registered: Sep 2011
Posts: 124
Rep:
|
Quote:
Originally Posted by Pansmanse
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.
|
|
|
09-29-2011, 05:21 AM
|
#11
|
LQ Newbie
Registered: Sep 2011
Posts: 4
Rep:
|
Sorry, I actually have forward slashes in my script and they do work. Copied this from the previous entry.
|
|
|
09-29-2011, 05:45 AM
|
#12
|
Member
Registered: Sep 2011
Posts: 124
Rep:
|
Quote:
Originally Posted by Pansmanse
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 05:46 AM.
|
|
|
08-22-2012, 11:51 AM
|
#13
|
LQ Newbie
Registered: Aug 2012
Posts: 1
Rep:
|
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.
|
|
|
08-22-2012, 10:11 PM
|
#14
|
Member
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862
Rep:
|
Quote:
Originally Posted by okomba
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.
|
|
|
10-11-2012, 01:03 AM
|
#15
|
LQ Newbie
Registered: Jun 2012
Posts: 2
Rep:
|
Quote:
Originally Posted by SharpyWarpy
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
|
|
|
All times are GMT -5. The time now is 05:53 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|