6

I am using Linux Slackware 14.0, bash 4.2. Suppose, I have 2 files in the directory:

bash-4.2$ ls
1.txt  2.txt

I want to rename them to .svg. This answer is accepted and upvoted, so I think, it must work. He used this command:

find . -name "*.andnav" -exec rename -v 's/\.andnav$/\.tile/i' {} \;

So, I wrote the analogous command, but it printed an error:

bash-4.2$ find . -name "*.txt" -exec rename -v 's/\.txt$/\.svg/i' {} \;
rename: not enough arguments

When I want to just print the files, it works:

bash-4.2$ find . -name "*.txt" -print
./1.txt
./2.txt

I know, that I can rename with a simpler approach:

bash-4.2$ rename .txt .svg ./*.txt
bash-4.2$ ls
1.svg  2.svg

But why doesn't the command from that answer work for me?

  • 1
    There are two completely different rename implementation on Linux: the one from perl (which that answer assumes) and the one from util-linux (which you have). – Stéphane Chazelas Jun 7 '13 at 20:36
  • @StephaneChazelas How to convert that answer to use my implementation? – user4035 Jun 7 '13 at 20:40
  • 4
    find . -name '*.txt' -exec rename .txt .svg {} + – Stéphane Chazelas Jun 7 '13 at 20:41
  • @StephaneChazelas Worked, thx. Where can I read the docs, explaining the syntax you used: '{} +'? – user4035 Jun 7 '13 at 20:48
  • info --index-search=-exec find – Stéphane Chazelas Jun 7 '13 at 20:50
8

There are two commands called rename on Linux distributions. There is one in the basic Linux utilities, which is used like this:

rename SUBSTRING REPLACEMENT FILE...

This command replaces the first occurrence of SUBSTRING by REPLACEMENT in each of the specified files. It only supports plain strings, not wildcards or regexps. On Debian and derivatives (Ubuntu, Mint, etc.), this command is called rename.ul. On other Linux distributions, it's called rename.

There is also a perl script called rename which is used like this:

rename 'PERL CODE' FILE...

A common form of Perl code is rename 's/REGEXP/REPLACEMENT/' FILE..., which replaces the first occurrence of REGEXP by REPLACEMENT in each file name (s/REGEXP/REPLACEMENT/g replaces all occurrences).

The author of the question you saw indicated that he's running Ubuntu, hence he accepted this answer using the rename command found on Ubuntu.

The analog for the standard rename command is

find . -name "*.txt" -exec rename .txt .svg {} \;

With bash ≥4, you can use the ** wildcard pattern after setting the globstar option to match files in subdirectories without using find.

shopt -s globstar
rename .txt .svg **/*.txt

Beware that this rename command acts on the first string, so if you have a file called hello.txt.world.txt, it'll be renamed to hello.svg.world.txt.

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.