Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know how renaming files in Linux :

$ mv ~/folder/subfolder/file.txt ~/folder/subfolder/file.sh

     ^------this part-------^    ^------this part-------^

I think it takes too long time to repeat ~/folder/subfolder/file twice.

Is there any fast way ?

share|improve this question
    
Why are you using sudo for moving a file between two directories in your home? –  Bakuriu May 27 at 16:36
    
Good point. Removed :) –  Hamed Kamrava May 27 at 16:49

5 Answers 5

up vote 51 down vote accepted

Try this (works with csh, tcsh, ksh, zsh, bash or fish):

mv ~/folder/subfolder/file.{txt,sh}
share|improve this answer
2  
simple is the best! –  Braiam May 27 at 18:05
1  
awesome use of expansion in shell brilliant! –  harish.venkat May 27 at 18:10
    
It is awesome, but really cryptic. It works but at the first glance I would think it does something different: moves two file somewhere. I don't think this is a great idea, especially since there is a much better solution using rename. –  tohecz May 28 at 21:07
1  
@tohecz: Why is it cryptic? It's a feature of shell and every one can know it by reading shell man page. And I don't think rename is a better solution because rename never ask you to override the existing file or not. –  Gnouc May 29 at 2:32

You can also use rename (part of the util-linux package).

rename .txt .sh ~/folder/subfolder/file.txt

See the rename man page for more details.

share|improve this answer
    
+1 This is the clear way how to do it, much better than relying on shells {a,b} functionality. –  tohecz May 28 at 21:02

All the above are good. This would also work :

$ ( cd ~/folder/subfolder && mv file.txt file.sh )
share|improve this answer
    
No. It doesn't work for me! –  Hamed Kamrava May 27 at 16:35
    
It will work if you remove the sudo part. –  Cthulhu May 27 at 17:03
1  
@Cthulhu Yes your right. But why doesn't work with sudo? –  Hamed Kamrava May 27 at 17:29
    
@HamedKamrava Probably because with sudo it interprets ~ as the root's home directory. –  Cthulhu May 28 at 4:41

No. You need to give the full path to the file in order to rename it. The only alternative is to move into the target folder before running the mv:

cd ~/folder/subfolder/
mv file.txt file.sh

Alternatively, you could write a little function that renames the file in the target directory. For example, add these lines to your shell initialization file (~/bashrc if you are using bash):

lmv(){
    _path=$(dirname -- "$1")
    _target="${_path%/}/$2"
    mv -- "$1" "$_target"
}

Then, open a new terminal or just run source ~/.bashrc to re-read the init file and you can do:

lmv ~/folder/subfolder/file.txt file.sh
share|improve this answer
    
+1 for lmv() function. I didn't know that :) –  Hamed Kamrava May 27 at 16:20
    
@HamedKamrava nobody knew it. I just wrote it, it's not anything standard. –  terdon May 27 at 16:22
2  
I know. I mean I didn't know I can write my own function! –  Hamed Kamrava May 27 at 16:23

Yes. If you use bash, you do sudo pushd ~/folder/subfolder/ && sudo mv ./file.txt ./file.sh && popd.

Which is actually bigger and may fail if you lost access permissions to the original directory when you did the popd.

share|improve this answer
1  
What's the point of pushd and popd here? How is this better than cd ~/folder/subfolder/ && sudo mv file.txt file.sh? –  terdon May 27 at 16:20
1  
The point is, that you are after the command back in the folder you started, not in any subfolder. An alternative to pushd popd is to use cd and go back with cd -. –  jofel May 27 at 16:27
    
The questioner seemed to state an intention not to change the working directory, and popd is better than cd ../../, when available. –  galegosimpatico May 27 at 16:32
4  
Use (cd /path/to/there && mv x y) then (with a subshell) –  Stéphane Chazelas May 27 at 16:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.