2

I have huge number of files in folders whose names are as following:

abc.test.txt
edf.main.txt
num2.tb.doc
file3.map.csv
file4.test.csv

I would like these files to be renamed such a way that the text in between the . is removed and above files are renamed as following :

abc.txt
edf.txt
num2.doc
file3.csv
file4.csv

Is this possible?

I found something similar but doesn't satisfy what I need to.

how can I rename multiple files by removing a character or string?

  • Yes, it is possible. You can do it in a for loop. Have you tried playing with it? – rvs Apr 22 '16 at 17:07
  • 2
    Are there files named e.g. ab.cd.ef.gh (that is, three or more dots) and if so, should those be renamed too and how ? – don_crissti Apr 22 '16 at 18:00
2

In zsh, use zmv. Run autoload -U zmv or put that in your ``/.zshrc`, then

zmv '(*).*(.*)' '$1$2'

or

zmv -w '*.*.*' '$1.$3'

Alternatively, if you have the prename command (Perl script that applies an expression to each file name):

prename 's/\..*\././' *.*.*

With only tools that are available on every POSIX system, use a shell loop, and parameter expansion to extract the parts of the name.

for x in *.*.*; do
  mv -- "$x" "${x%.*.*}.${x##*.}"
done
  • @aippili Please do not post comments to say “thank you”: upvote answers that you find helpful (one more reputation point and you'll be able to do this). If you think that one answer fully solves your problem, please mark it as accepted (this will give you that missing rep point, by the way). For more information, see the help center. – Gilles May 4 '16 at 17:20
1

Using the perl rename utility:

prename 's/\.[^.]*\././' *

prename allows you to rename files using perl expressions.

On some systems, the same program is called rename rather than (or as well as) prename. There are other programs called rename which work very differently - check the rename man page on your system to be sure.

e.g. by default, rename on Debian systems is a symlink to prename (via the /etc/alternatives system). Other distros may differ.

Debian also has a package called rename which contains an enhanced version of prename called file-rename. It's a newer, updated version of the same script. It has a higher priority in alternatives, so installing the package will change the rename symlink to file-rename.

-1

This should take care of it

for file in $(find ./)
do
  newname=$(echo ${file} | sed -e 's/\.\(.*\)\././')
  mv ${file} ${newname}
done
  • 1
    Don’t do for file in $(find …); do …; instead, do find . … -exec ….  Note that $file and $newname should be quoted (in double quotes); putting them in braces doesn’t accomplish anything when used on a free-standing variable, like ${file} in your answer.  Does this sound familiar? – G-Man Apr 22 '16 at 18:43

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.