954,074 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Rename

Is it just me, or is copying then removing the old file a hassle? Why doesn't Linux have a built in rename command for single files / directories?

linux
Posting Shark
933 posts since Aug 2006
Reputation Points: 118
Solved Threads: 30
 

Hey there,

You can use "mv" for files and directories.

Perhaps I'm misunderstanding.

Hope that helps :)

, Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

I'm sorry. Looking at your profile, I think you're just kidding ;) You got me :)

,Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

yeah mv

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,820
Solved Threads: 599
 

but that moves and renames whilst removing the old file !? :)

majestic0110
Nearly a Posting Virtuoso
1,328 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
 

switches can change that

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,820
Solved Threads: 599
 

I'm pretty mv sure it's cp then rm:)

, Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

No, thats actually exactly wrong.

The mv command does not make a new file. Period. The datablocks holding the information remain unchanged. All that happens is that the entry in the directory file that holds the inode information is copied to the new directory, and then the inode information is removed from the old directory.

To demonstrate this; create a file, and change ownership to someone in your group, but first chmod the file to 600. Chmod the directory the file is located in to 770, and make sure that it is owned by your group and/or you.

Create a new directory (770), with the same ownership. Now, technically, you will not have write access to the file (600 owned by Joe). But, since you have permissions to change the inode information contained in both directory listings, you will be able to run an mv command on the file. You will not, however be able to read from that file to perform the cp command. You can mv, but not cp. You have no access to the stored data, just to the inodes pointing to the data. Therefore, you change the location from which those inodes can be accessed, but do not modify the original file. This is a true mv.

omrsafetyo
Junior Poster in Training
58 posts since Apr 2008
Reputation Points: 13
Solved Threads: 9
 

Well, not exactly ;)

I threw that one out there without thinking, I'll admit and you are correct in the circumstance that both files are on the same partition.

If you mv /bob/dir1 to /bob/dir2 - assuming /bob is one partition/filesystem containing dir1 and dir2 - then the pointer to inode explanation is correct.

However, if you mv a file from /bob/dir/1 to /joe/dir/1 - assuming /bob is one partition/filesystem and /joe is another partition/filesystem - Unix (at least, Solaris) actually does do a cp and then an rm. The same test you propose will not allow you to mv or cp the file you don't have permissions to access in that situation.

Hopefully, this post is finally resolved. If I'm not mistaken it all started out as a joke ;)

Take care,

Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

touche!

I had not thought about that. In that case, you would be correct.

omrsafetyo
Junior Poster in Training
58 posts since Apr 2008
Reputation Points: 13
Solved Threads: 9
 

Hey,

Sunny side up: We were both correct :) Nothing wrong with healthy debate and you made an excellent point!

, Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: