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
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.
Simple
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
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
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
hello
[ 22:17:15 ~/Desktop ] $
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
.
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).
cp
andrm
, but what's the point?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 pathnewfile
. 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)