Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

remove space and parenthesis in file names

717 views
Skip to first unread message

Ivan Rusinovitch

unread,
Mar 27, 2016, 1:12:31 AM3/27/16
to
This should be one of the most common scripts available!

Yet I'm going crazy trying to remove special characters and am
very sad that I can't find any good existing script by googling.

Here's the best I have and it doesn't work (only partially).

Do you have a script that just converts all files in a directory
that have spaces and parentheses in the name (and possibly other
special characters if necessary) to underscores?

Here's what I'm using but it is driving me crazy.



#!/bin/bash
# https://md314159265.wordpress.com/2006/08/01/a-script-to-remove-spaces-from-a-file-name/
# Writen by Mayuresh Phadke (mayuresh at gmail.com)

# To change the names of all files in a directory including directory names
# run the command
#
# find . -depth -exec ~/rename.sh {} \;
#
# This command is pretty useful if you have a collection of songs or pictures transferred
# from your windows machine and you are finding it difficult to handle the
# spaces in the filenames on UNIX
#

# set -x

progname=`basename $0`
# if [ $# != 1 ]
# then
# echo "Usage: $progname \"file name with spaces\""
# echo
# echo "This utility is useful for renaming files with spaces in the filename. Spaces in the filename are replaced with _"
# echo "\"file name with spaces\" will be renamed to \"file_name_with_spaces\""
# echo
# exit 1
# fi

# Replace all spaces with underscores
for i in *\ *
do
old_name=$i
echo "old_name = $old_name"
dir=`dirname "$i"`
echo "dir = $dir"
file=`basename "$i"`
echo "file = $file"
done

new_file=`echo $file|sed "s/ /_/g"`
new_name=$dir"/"$new_file
echo "new_file = $new_file"
if [ "$old_name" != "$new_name" ]
then
mv "$old_name" "$new_name"
fi

# Replace all open parenthesis with underscores
for i in *\(*
do
old_name=$i
echo "old_name = $old_name"
dir=`dirname "$i"`
echo "dir = $dir"
file=`basename "$i"`
echo "file = $file"
done

new_file=`echo $file|sed "s/(/_/g"`
new_name=$dir"/"$new_file
echo "new_file = $new_file"
if [ "$old_name" != "$new_name" ]
then
mv "$old_name" "$new_name"
fi

# Replace all closed parenthesis with underscores
for i in *\)*
do
old_name=$i
echo "old_name = $old_name"
dir=`dirname "$i"`
echo "dir = $dir"
file=`basename "$i"`
echo "file = $file"
done

new_file=`echo $file|sed "s/)/_/g"`
new_name=$dir"/"$new_file
echo "new_file = $new_file"
if [ "$old_name" != "$new_name" ]
then
mv "$old_name" "$new_name"
fi

exit 0

William Unruh

unread,
Mar 27, 2016, 1:22:03 AM3/27/16
to
for i in *
do
j=`echo $i|tr ' ()' '_'`
mv $i $j
done

William Unruh

unread,
Mar 27, 2016, 1:28:57 AM3/27/16
to
Sorry, that should be
mv "$i" "$j"

Otherwise the spaces act as word breaks.

>

Teemu Likonen

unread,
Mar 27, 2016, 3:07:36 AM3/27/16
to
Ivan Rusinovitch [2016-03-27 05:12:28Z] wrote:

> Do you have a script that just converts all files in a directory that
> have spaces and parentheses in the name (and possibly other special
> characters if necessary) to underscores?

Not quite but I have made "translit-mv" script that renames files and
allows only [a-z0-9._-] characters. It's public domain so copy, modify
and share as you will (see the attachment).


Usage: translit-mv [OPTIONS] [--] SOURCE ...

Convert SOURCE filename(s) to safe ASCII versions which contain only
[a-z0-9._-] characters. Also truncate consecutive [._-] characters to
just one.

Options:

-t, --test Test run. Treat SOURCE parameters as plain filename strings
and show how the basename(s) would be converted. This is
a safe option; SOURCE(s) don't need to be existing
files.

-v, --verbose
Explain what is being done.

-h, --help Display this help and exit.

translit-mv
signature.asc

Pedro Valdez

unread,
Mar 27, 2016, 7:08:10 AM3/27/16
to
Does translit get rid of " / " space slash space?
I have files on a PVR with that because it is in a TV station ID.
Cannot read those files in both linux and Windoze.

Chris Ahlstrom

unread,
Mar 27, 2016, 7:26:23 AM3/27/16
to
William Unruh wrote this copyrighted missive and expects royalties:

> On 2016-03-27, Ivan Rusinovitch <ivanruss...@x.invalid> wrote:
>> This should be one of the most common scripts available!
>>
>> Yet I'm going crazy trying to remove special characters and am
>> very sad that I can't find any good existing script by googling.
>
> for i in *
> do
> j=`echo $i|tr ' ()' '_'`
> mv $i $j
> done

There are also a few "rename" scripts out there, at least one of
which let's one rename files using regular expressions.

--
Your reasoning is excellent -- it's only your basic assumptions that are wrong.

Jasen Betts

unread,
Mar 27, 2016, 9:01:26 AM3/27/16
to
On 2016-03-27, Ivan Rusinovitch <ivanruss...@x.invalid> wrote:
> This should be one of the most common scripts available!

> Yet I'm going crazy trying to remove special characters and am
> very sad that I can't find any good existing script by googling.

try file-rename
https://metacpan.org/release/File-Rename
debian package "rename"

eg: file-rename 's/[\(\)_]+/ /g' filenames



--
\_(ツ)_
0 new messages