4

In a folder I have a .run file and am trying to change the extension to .clu. The problem is that I have tried

rename 's/.run$/.clu/' *.run

But this does absolutely nothing to the file.

Note: I'm not trying to convert the file, just to rename the extension.

4
  • 1
    Do you have write permission on the file?
    – Jos
    Jun 27, 2014 at 10:54
  • I do have write permission.
    – Dobz
    Jun 27, 2014 at 12:16
  • Please, show the name of the files you are trying to rename. ls or whatever is useful.
    – Braiam
    Jun 27, 2014 at 13:51
  • for file in *.run ; do mv -f $file `echo $file | sed 's/\(.*\.\)run/\1clu/'` ; done this works.
    – Dobz
    Jun 27, 2014 at 14:37

4 Answers 4

6

I don't know what your problem is. What you've posted works:

$ mkdir test
$ touch test/test{01..10}.run
$ rename 's/\.run$/.clu/' test/*.run -vn
test/test01.run renamed as test/test01.clu
test/test02.run renamed as test/test02.clu
test/test03.run renamed as test/test03.clu
test/test04.run renamed as test/test04.clu
test/test05.run renamed as test/test05.clu
test/test06.run renamed as test/test06.clu
test/test07.run renamed as test/test07.clu
test/test08.run renamed as test/test08.clu
test/test09.run renamed as test/test09.clu
test/test10.run renamed as test/test10.clu

The -vn is just telling us what it would do if run without it.

I'm escaping the dot (otherwise it's a REGEX "anything") but it really makes no difference here. It works as well without it.


Is it possible that you're actually running rename.ul? Check to make sure you're using the Perl rename (which takes the syntax we're using) with dpkg -S $(readlink -f $(which rename))

  • perl: /usr/bin/prename is good.
  • util-linux: /usr/bin/rename.ul is bad. For some reason you're using a very limited version of rename. Something very squiffy has happened.

For the moment, see if prename exists (you could just use that for now) and if not, start asking why Perl isn't installed properly. It would suggest you don't have Ubuntu installed.

Or if you're happy using rename.ul, the following should work:

rename.ul .run .clu *.run

But that might munch a file like test.run.hooray.run into test.clu.hooray.clu which obviously isn't great.

2
  • looking at your code, it could be the fact that after the 's' you have '/\' while I only have '/', this may be my problem. I'll try it out.
    – Dobz
    Jun 27, 2014 at 12:15
  • @RussellHickey As I commented on another, now deleted answer, it makes absolutely no difference here. As you had it, . means "any character", while \. means . - they both work here.
    – Oli
    Jun 27, 2014 at 12:34
2

You don't need to use rename for this. You can do it using basename:

for f in *run; do mv "$f" "$(basename "$f" .run)".clu; done

basename will print a file's name (no path) and also removes an optional suffix. Therefore, "$(basename "$f" .run)".clu is the name of the file $f with the .run extension replaces with clu.

You can also just use bash's own string manipulation features:

for f in *run; do echo mv "$f" "${f%%.run}".clu; done

The construct ${var%%string} removes the string string from the end of the variable $var.

2

My prefered in cases like this is mmv (it is not installed by default in Ubuntu, but you can install it using sudo apt-get install mmv command):

mmv -n '*.run' '#1.clu'

The -n means that it's a test run and will not actually change any files. It will show you a list of files that would be renamed if you removed the -n. In the case above, it will convert all files in the current directory from a file extension of .run to .clu.

mmv -v '*.run' '#1.clu'

The -v is optional, but it's a good idea to include it because it is the only record you will have of changes that were made by the rename command.

See man mmv for more info.

0
1

You just use mv:

mv oldname newname

mv is used to move/ rename files. Refer to the man page of mv for more.

5
  • 1
    This will be used in a build file so the names will not be known.
    – Dobz
    Jun 27, 2014 at 10:51
  • You could do mv *.run *.clu if you are sure that you want to rename all *.run files to *.clu in the directory you are executing this command.
    – jobin
    Jun 27, 2014 at 10:54
  • this will give the output name as '*.clu' I was trying to just change extention.
    – Dobz
    Jun 27, 2014 at 10:57
  • for file in *.run ; do mv $file echo $file | sed 's/\(.*\.\)run/\1clu/' ; done this has seemed to worked
    – Dobz
    Jun 27, 2014 at 10:57
  • 2
    @Jobin mv *.run *.clu will either fail or move all *run files and all but one *clu file into a directory ending with clu if one happens to exist and in the right alphabetical order. In any case, it will most certainly will not simply change the extensions.
    – terdon
    Jun 27, 2014 at 11:30

You must log in to answer this question.

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