2

I need to rename multiple files, and some tutorials shows that the command rename is the best option instead of just use mv for each file.

But then I have these files:

 ReconhecimentoImagens2021-10-18--2020-2.mp4       ReconhecimentoImagens2021-11-01--2020-2.mp4   ReconhecimentoImagens2021-11-22--2020-2.mp4
 ReconhecimentoImagens2021-10-25--2020-2.mp4       ReconhecimentoImagens2021-11-08--2020-2.mp4   ReconhecimentoImagens2021-11-24--2020-2.mp4
'ReconhecimentoImagens2021-11-01--2020-2(2).mp4'   ReconhecimentoImagens2021-11-17--2020-2.mp4   ReconhecimentoImagens2021-11-29--2020-2.mp4

and just for a test I tried this command:

rename 's/2021/ /' *.mp4

and NOTHING happens, I get code 4 that according to the man page means 'nothing was renamed'.

I searched for multiple tutorials and nobody seems to be doing anything different than that so, what am I doing wrong here?

7
  • There are several versions of the rename command. Could you state yours?
    – FelixJN
    Commented Dec 20, 2021 at 14:39
  • Are you using the perl version of rename (AKA prename, perl-rename, file-rename)? or some other rename (like the one in the util-linux package)? Find out by running rename -V - if it mentions perl or File::Rename at all (e.g. /usr/bin/rename using File::Rename version 1.30, File::Rename::Options version 1.10) then it's the perl rename. If not, then it isn't - and has completely different capabilities and options. If you're using some other rename, I recommend installing and using the perl rename (e.g. on Debian and related distros sudo apt-get install rename).
    – cas
    Commented Dec 20, 2021 at 14:39
  • On other distros, the package might be named prename or file-rename or similar.
    – cas
    Commented Dec 20, 2021 at 14:41
  • its rename util-linux 2.37.2 @cas @FelixJN
    – arksdf
    Commented Dec 20, 2021 at 14:41
  • util-linux's rename is a completely different program with completely different and incompatible options and capabilities. Either read man rename to figure out how to use it (it won't be anything like rename 's/2021/ /' *.mp4) or (highly recommended) install the perl rename utility.
    – cas
    Commented Dec 20, 2021 at 14:43

1 Answer 1

4

There's different versions of rename, and yours isn't the one with PERL regex support.

For most Linux distros and other unixoid systems, there's some package that contains the rename.pl or simply rename command that's built from perl.

In your use case, the "classical" util-linux rename fully suffices:

rename "2021" " "  *.mp4

would do.

Honestly, my shell does well enough that I don't have to remember all these fine things. I learned sed somewhat OK-ish once, I've learned my shell's for syntax once, and that's it:

for filename in *.mp4 ; do
  mv "${filename}" "$(echo "${filename}" | sed 's/regex/replacement/')"
done

is pretty easy to remember for me, at least, and should work in bash and zsh and other bourne-alike shells. Not an extra command I have to learn.

In fact, if you want things to be simple, you can use your own favourite shell's variable expansion to its fullest extent:

#!/usr/bin/env zsh
if [[ "${#}" -lt 2 ]] ; then 
  echo "Usage: ${0} [-r] PATTERN REPLACEMENT [FILE...]"
  echo "Uses normal Zsh globbing for pattern, unless -r is specified:"
  echo "In that case, use the regexp-replace loadable ZSH function"
  exit 1
fi

if [[ "${1}" = "-r" ]]; then
  autoload regexp-replace
  REGEX=1
  shift 1
else
  set -o extendedglob
  REGEX=0
fi

PATTERN="${1}"
REPLACEMENT="${2}"
shift 2

if [[ "${REGEX}" -eq 0 ]]; then
  for filename in ${argv} ; do
    mv "${filename}" "${filename/${1}/${2}}" || echo "Problem renaming ${filename}" >&2
  done
else
  for filename in ${argv} ; do
    original_filename="${filename}
    regexp_replace filename "${PATTERN}" "${REPLACEMENT}"
    mv "${original_filename} "${filename}" || echo "Problem renaming ${original_filename}" >&2
  done
fi
1
  • thanks, that worked. I used sed like this before, but I was looking for something easier to understand in a fast read, but thanks either way, it helped.
    – arksdf
    Commented Dec 20, 2021 at 14:57

You must log in to answer this question.

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