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.

Aaron
  • 6,764
Dobz
  • 251

4 Answers4

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.

Oli
  • 299,380
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.

terdon
  • 104,119
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.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
1

You just use mv:

mv oldname newname

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

jobin
  • 28,567