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?

Recommended Answers

All 10 Replies

Hey there,

You can use "mv" for files and directories.

Perhaps I'm misunderstanding.

Hope that helps :)

, Mike

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

,Mike

yeah mv

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

switches can change that

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

, Mike

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.

commented: Wow, lots of information. Thanks. +3

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

touche!

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

Hey,

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

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.