I have a whole bunch of files that are like so:


and I want to prefix them with a specific phrase so that they look like so:

some prefix - x54y65.dat

Its that space after the hyphen that's getting me.

I tried:

 mv * some\ prefix\ -\ *

but that didn't work. How do I do it? Thanks

  • 2
    Ha! Figured it out on my own! Yay me. rename 's//some prefix - /' *.dat Found the help here: link – bcsteeve Nov 4 '13 at 1:41
  • You can add your comment as answer. See askubuntu.com/help/self-answer . – Radu Rădeanu Nov 4 '13 at 9:06
  • actually, I tried that... but I didn't have the reputation to do so. I see now that I can try again as its been > 8 hours. – bcsteeve Nov 4 '13 at 14:44

Although the answer from the original author is correct, I think it is better to explain why the mv command listed in the question does not work.

The problem is a basic misunderstanding on the way in which a Unix shell works --- which is quite different form DOS command line. The special characters (*, ?, etc.) which select multiple files (they are called 'glob') works at the level of the shell, not of the commands. So the first command is expanded by the shell to

mv x54y56.dat x57y58.dat x59y60.dat "some prefix - *"

I am supposing that in the directory you have just this three files for sake of simplicity, and that you have no file which matches the name of "some prefix - " pattern --- so that the shell will probably let it as is. (I am using the quotes to embed spaces, I find it more easily readable than backlashes, although the fine details are in the manual for your shell).

Now, mv a b c d will try to move the files a, b, c to the directory d, and failing --- because d does not exist.

The rename solution, if you have the rename command, is the Right One. As the OP found,

rename 's//some prefix - /' *.dat

works ok. In the old good days, you where supposed to do

for i in *.dat; do
  mv "$i" "some prefix - $i"

to just do one move at time.

rename 's//some prefix - /' *.dat

This uses regex-type syntax to rename all files with the specified extension (.dat) in one line. It handles spaces easily, as it takes any characters encapsulated by the /

mv x54y65.dat "some prefix - x54y65.dat"
  • 1
    That just renames one file only and doesn't explain anything. The question was about batch renaming. – LiveWireBT Nov 4 '13 at 10:02
  • 1
    Even still, I appreciate the clue about the quotes. I'm so new, I didn't even know that. When I Googled about spaces in Linux, I found that you should put a \ before the space. I never saw anything about encapsulating. However, that's standard from DOS days so I should have tried it (duh). The answer I provided myself in my comments works better, however, as it does all files in one go. – bcsteeve Nov 4 '13 at 14:46
  • for i in *; do mv $i "some prefix-"$i;done for rename multiple files – andr3w Nov 4 '13 at 20:50

Your Answer

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

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