Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

i tried it the following way:

rename 'bla bla .txt' blabla.txt

But this doesn't work, what's the correct syntax for this case?

share|improve this question
add comment

2 Answers

up vote 14 down vote accepted

You shouldn't be using rename to rename just one file, use mv(1) instead.

Further, to escape whitepsace, prefix it with a \:

"Ubuntu One"Ubuntu\ One

Your command would be

mv bla\ bla.txt blabla.txt
  • the backslash tells bash that the next character is somehow special, e.g. \a for 'bell', \ for a literal space and so on

    see man ascii(7) for more

Alternatively, since you are now using the correct command mv, you can quote the filename if there are many spaces:

mv "bla    bla.txt" blabla.txt

(This would be bla\ \ \ \ bla.txt in escaped form)

  • In bash, quoted strings (single or double quotes) are treated as a single argument, whereas unquoted strings will be split into multiple arguments, like this:

    • ["mv", "bla", "bla.txt", "blabla.txt", ]

    and mv expects:

    • ["mv", "source", "destination", ]
  • Note: typing 'mv' and the first few characters of the filename and pressing TAB will give you the escaped version of the file name, making renaming quick and easy.


rename is used to batch-process the renaming of files using regular expression, as demonstrated in the example from its man-page:

rename 's/\.bak$//' *.bak

To strip any occurence of .bak (at the end of the string [$]) from all of the files matching "*.bak".

share|improve this answer
    
thanks, how would look the command if there are many whitespace in the origin filename? is there a simple regex so that there's no need to escape every single whitespace? –  NES Dec 25 '10 at 21:58
    
Updated the answer to address many spaces and single v. double quotes. –  Stefano Palazzo Dec 25 '10 at 22:09
    
There is a difference between single and double quotes, but it's of no concern for this problem. See this article for the exact details of it –  Stefano Palazzo Dec 25 '10 at 22:18
    
thanks a lot for the help –  NES Dec 25 '10 at 22:21
2  
Great answer :) –  karthick87 Dec 26 '10 at 9:17
add comment

rename is specialized command for bulk renaming. Unintuitively, what we normally call "renaming" is actually a "move" to a new name:

mv 'bla bla .txt' blabla.txt
share|improve this answer
    
what is the difference between writing the filename in single quotes and double quotes? –  NES Dec 25 '10 at 22:00
4  
Double quoting allows variables to be expanded and for process substitution; single quoting doesn't. A=bla ; mv "$A $A .txt" blabla.txt will move bla bla .txt, but A=bla ; mv '$A $A .txt' blabla.txt will look for a file actually named "$A $A .txt". Either one will work for your example though. –  frabjous Dec 25 '10 at 22:14
    
thanks for the information –  NES Dec 25 '10 at 22:21
add comment

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.