Linux mv file with long name

filesfilesystemslinuxmv

In Linux I sometimes rename files like this:

mv dir1/dir2/dir3/file.txt  dir1/dir2/dir3/file.txt.old

Note that I want to just rename the file, not move it to another directory.

Is there a command that would allow me to do a shorthand version of that command? I am thinking something like:

mv dir1/dir2/dir3/file.txt  file.txt.old

or maybe even something like (to just append to the name):

mv dir1/dir2/dir3/file.txt  {}.old

My goal is not to have to specify the complete path again.

I know those "examples" I wrote don't work, but it is just an idea of what I want to accomplish.

I don't want to have to cd in to the directory.

Best Answer

for a single file try

mv dir1/dir2/dir3/file.{txt,txt.old}

where the X{a,b} construct expand to Xa Xb, you can have a preview using

echo dir1/dir2/dir3/file.{txt,txt.old}

to see if it fit your need.

note:

  1. that for multiple files

    mv dir1/dir2/dir3/file{1,2,3}.{txt,txt.old}
    

is unlikely to expand to what you want. (this will expand to a mixed of file.txt file1.txt.old file2.txt ...)

  1. {txt,txt.old} can be shorterned to {,.old} as per comment

  2. if directory name are unambigous, wildcard can be used.

    mv *1/*2/d*/*.{,old}
    

for multiple file use rename

rename -n s/txt/old.txt/ dir1/dir2/dir3/file*.txt 

drop -n to have effective rename.