1
for i in L*;
do
cd $i
find . -type f -name 'botrytis_cinerea_T12345.*' -exec rename 's/botrytis_cinerea_T12345/"$i"/g' {} \;
done

I get the error

find: missing argument to `-exec' rename: not enough arguments

Usage:  rename [options] expression replacement file...

Options:  -v, --verbose    explain what is being done  -s, --symlink   act on symlink target

 -h, --help     display this help and exit  -V, --version  output version information and exit

For more details see rename(1).
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Fuyou Fu
  • 13
  • 2
  • You're using the "other" `rename`. Possible duplicate of [Why is the rename utility on Debian/Ubuntu different than the one on other distributions, like CentOS?](https://unix.stackexchange.com/questions/275254/why-is-the-rename-utility-on-debian-ubuntu-different-than-the-one-on-other-distr) – trent Nov 06 '19 at 16:08
  • Actually [Rename All Files with a Certain Name](https://unix.stackexchange.com/questions/24761/rename-all-files-with-a-certain-name) is a nearer dupe. – trent Nov 06 '19 at 16:23

2 Answers2

1

There are two common rename commands that have different syntaxes. You're using a script written for the version that accepts a Perl expression:

rename s/expression/replacement/g file...

But the one you have installed is the one that accepts both a regex and replacement string, as the error message you saw suggests:

rename [options] expression replacement file...

(Count yourself lucky, or smart, that you used \; instead of +.) You can fix this by adjusting your find command:

find . -type f -name 'botrytis_cinerea_T12345.*' -exec rename botrytis_cinerea_T12345 "$i" {} \;

As Kusalananda pointed out in the comments, you're also cding inside the loop, but never going back to the original directory, so each iteration after the first, you repeatedly attempt to cd to a directory that doesn't exist. You might try to fix this by doing another cd after find, but I'd probably try to avoid that by adjusting the find itself:

for i in L*; do
    find "$i" -type f -name 'botrytis_cinerea_T12345.*' -exec rename botrytis_cinerea_T12345 "$i" {} \;
done

See also

trent
  • 433
  • 4
  • 9
  • 1
    Any comment on the `cd` in their code and how the loop is expected to not be able to handle more than one iteration, and what they could do about that? – Kusalananda Nov 06 '19 at 16:47
  • @Kusalananda Good point. I'm paranoid about `cd`ing in scripts so I wrote a solution based on changing `find` instead. – trent Nov 06 '19 at 17:10
0

The single quotes will stop expansion. And {} might need to be escaped Do this instead:

find . -type f -name 'botrytis_cinerea_T12345.*' -exec rename "s/botrytis_cinerea_T12345/$i/g" \{\} \;
Eduardo Trápani
  • 12,032
  • 1
  • 18
  • 35
  • I get the same error. – Fuyou Fu Nov 06 '19 at 16:04
  • Sorry, I updated my answer, you actually need to escape the "{}" – Eduardo Trápani Nov 06 '19 at 16:09
  • Note that their code is stepping down into a directory with `cd`, but that they never change back to the original directory. I would expect the code to eventually generate errors from `cd` too. Also, `{}` does not need to be escaped as it's not special to the shell (unless the user is using `csh` or `tcsh`). – Kusalananda Nov 06 '19 at 16:16