I was wondering is there another way of renaming a file aside from using mv in linux?

For example changing /home/usr/blah.txt to something like -home-usr-blah.txt

  • You mean, you want to encode the path in the filename? – phynfo May 28 '11 at 20:06
  • 2
    You could use cp and rm, but what's the point? – Matt Ball May 28 '11 at 20:07
  • 3
    Your question does not make sense. What has renaming a file got to do with it's path? – Tony May 28 '11 at 20:07
  • 1
    @Tony: erm... Clueless? ln associates another path with an existing inode. unlink dis-associates one of the associated paths from an inode. touch newfile creates a new inode with associated path newfile. inodes are reference counted, i.e.: storage is reclaimed after the last referring path is unlinked. What else does it have to do with? Filesystems might implement this differently, but if they are POSIX compliant, the end result is exactly the same (google POSIX rename) – sehe May 30 '11 at 15:03

This goes to show that you need to think about how you ask the question. Everybody - including me - got your question wrong initially

You could use qmv to bulk rename interactively using an editor. It will allow search/replace and also cyclic renames.


What mv really accomplishes at the driver level is close to this:

ln source target
unlink source

So, you can do that. This, however assumes the optimized scenario where source & target reside in the same filesystem. If not, you will have to use cp instead of ln. Be sure to make that a cp -a

Update in response to (paranoid) comment:

(ln source target || cp -a source target) && unlink source

| improve this answer | |
  • This is a dangerous solution to program into a script though because it could result in data loss in the event that "ln" creates a symbolic link instead of a hard link. It's great that you hint at this, but I do think it would be better to highlight this danger before that excellent two-command example. (Unfortunately, "ln" doesn't appear to have a switch like -s but that ONLY succeeds if a hardlink is successfully created.) – Randolf Richardson May 29 '11 at 21:46
  • 1
    @Randolf Richardson: Huh. Why the FUD? ln will never create a symbolic link unless told to. You might have a broken system or badly devised aliases working against you. Linux evidence: ln dirname dirname2 --> hard link not allowed for directory; ln ~/.bashrc /tmp/ --> ln: creating hard link '/tmp/.bashrc' => '.bashrc': Invalid cross-device link – sehe May 30 '11 at 13:15
  • Okay, thanks for pointing that out. I must've been using a broken system before (there definitely wasn't an intention at FUD -- that's an area that a certain company which-shall-not-be-named has cornered the market on, and I have no interest in competing with them as they are specialists in that area and probably can't be beat at dispersing FUD). – Randolf Richardson May 30 '11 at 16:46

also hardlinks can be used for renaming files

[ 22:16:50 ~/Desktop ] $ echo 'hello' > myfile
[ 22:16:52 ~/Desktop ] $ ln myfile mynewfile
[ 22:17:04 ~/Desktop ] $ rm myfile
remove myfile? y
[ 22:17:11 ~/Desktop ] $ cat mynewfile
[ 22:17:15 ~/Desktop ] $
| improve this answer | |
  • Why would you do that instead of simply mv myfile mynewfile? – Laurent Pireyn May 29 '11 at 12:20
  • When transfering a file between two non-root accounts. The first user is responsible for doing the "ln". The second user is responsible for doing the "rm". – neoneye Nov 27 '11 at 20:29

Well, there is rename

But if you want to rename a full path into a single filename you will need to use other tools like sed or awk.

| improve this answer | |

You can use "rename" feature in various file management tools like Midnight Commander to do this, or you could use the rename() function from a Perl script (or equivilant functions with other programming/scripting languages).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy