1

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

x54y65.dat

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

3
  • 2
    Ha! Figured it out on my own! Yay me. rename 's//some prefix - /' *.dat Found the help here: link
    – bcsteeve
    Nov 4, 2013 at 1:41
  • You can add your comment as answer. See askubuntu.com/help/self-answer . Nov 4, 2013 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, 2013 at 14:44

3 Answers 3

4

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"
done

to just do one move at time.

2
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 /

1
mv x54y65.dat "some prefix - x54y65.dat"
3
  • 1
    That just renames one file only and doesn't explain anything. The question was about batch renaming.
    – LiveWireBT
    Nov 4, 2013 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, 2013 at 14:46
  • for i in *; do mv $i "some prefix-"$i;done for rename multiple files
    – andr3w
    Nov 4, 2013 at 20:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .