10

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?

user4035
  • 1,115

1 Answers1

16

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.