0

For example, I have 100 files and their names all have blank space. One of them is "The Monkey King Return (2015).mkv".

How to remove all blank in name and replace with dot character, also remove "(" and ")"?

The result should be "The.Monkey.King.Return.2015.mkv".

how I can do this in batch?

I am actually running shell script in my Synology NAS via ssh, which is a BusyBox Linux distribution with bash and ash installed, without gcc. Already tried for serveral days, cannot figure it out properly.

Tools aviable: mv / xargs / sed / awk / other standard linux cmd.

Also how about recursively do the renaming for sub folders as well?

Edit: just installed apt-get and rename cmd into my synology nas by using Debian Chroot from https://synocommunity.com/, so it is ok now.

To make person who need simple answer, the cmd is:

find . -iname \*\ \*.\*|rename 's/\ /\./g'
1

Most linux distro's include util-linux which gives you the rename command. You can do what you request by running these commands:

rename ' ' _ *.mkv
rename '(20' 20 *.mkv
rename ').' . *.mkv

You can experiment with the options, it's pretty flexible. If you want to do this recursively, combine rename with the find command like this:

find . -type f -name \*.mkv -exec rename ' ' _ {} \;

These are explained in the manual; type man find or man rename to read them.


Beware that some distro's apparently include a different command which accepts perl regular expressions. If yours does too, you'll need a slightly different syntax:

rename 's/\ /./g' *.mkv
rename 's/[\)\(]//g' *.mkv
  • Beware! At least if yours is the same as mine, rename takes a regular expression, and in a regular expression, . matches any single character. – a CVn Jun 7 '16 at 8:53
  • Well, mine doesn't and the man-page confirms it by supplying this example: rename .htm .html *.htm. But I'll append the warning, thanks – Sjon Jun 7 '16 at 8:54
  • I am actually running shell script in my Synology NAS via ssh, which unfortunately does not have 'rename' command, is it possible to do this using mv command? – chenyi1976 Jun 7 '16 at 9:06
  • @chenyi1976 do you have bash installed? List your constraints in your question – Sjon Jun 7 '16 at 14:59
  • Ticket updated, it is a BusyBox Linux distribution with bash and ash, without gcc. – chenyi1976 Jun 7 '16 at 21:16
2

If you don't have the rename command, you can do this with a little shell scripting which busybox's ash also seems to support (this is recursive):

find -type f -name \*.mkv | while read f; do
    n=${f/ /_} # replace spaces
    n=${n/(/}  # remove opening bracket
    n=${n/)/}  # remove closing bracket

    mv -v "$f" "$n"
done

Please note that will break if the folder-names also contain spaces or brackets.

1

I use this one-liner to remove invalid characters in subtitle files:

for f in *.srt; do nf=$(echo "$f" |sed -e 's/[^A-Za-z0-9.-]/./g;s/\.\.\././g;s/\.\././g'); test "$f" != "$nf" && mv "$f" "$nf" && echo "$nf"; done
  1. Only process *.srt files( * could be used in place of *.srt to process every file)
  2. Removes all other characters except for letters A-Za-z, numbers 0-9, periods ".", and dash's "-"
  3. Removes possible double or triple periods
  4. Checks to see if the file name needs changing
  5. If true, it renames the file with the mv command, then outputs the changes it made with the echo command

It works to normalize directory names of movies:

for f in */; do nf=$(echo "$f" |sed -e 's/[^A-Za-z0-9.]/./g' -e 's/\.\.\././g' -e 's/\.\././g' -e 's/\.*$//'); test "$f" != "$nf" && mv "$f" "$nf" && echo "$nf"; done

Same steps as above but I added one more sed command to remove a period at the end of the directory

X-Men Days of Future Past (2014) [1080p]
Modified to:
X-Men.Days.of.Future.Past.2014.1080p

0

I have been looking for a solution to this problem for a while now. I work on old closed systems that can't have new packages installed. I don't have the rename command. Finally I wrote a script that appears to work with all keyboard entered special characters: ~@#$%^&*()-_=+[]{}\|;:",<.>?'

The script will rename every file and directory in the current directory. It will replace all special characters except -_. with the _ character. The outfile= line can be modified to use a different character for replacement if desired. Replace |_| with |.| to use the . character for example.

#!/bin/bash

for file in ./*
do
  infile=`echo "${file:2}"|sed  \
         -e 's|"\"|"\\"|g' \
         -e 's| |\ |g' -e 's|!|\!|g' \
         -e 's|@|\@|g' -e 's|*|\*|g' \
         -e 's|&|\&|g' -e 's|]|\]|g' \
         -e 's|}|\}|g' -e 's|"|\"|g' \
         -e 's|,|\,|g' -e 's|?|\?|g' \
         -e 's|=|\=|g'  `
  outfile=`echo "${file:2}"|sed -e 's|[^A-Za-z0-9._-]|_|g'`
  mv "$infile" ${outfile} &
done

exit

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.