INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

remove brackets from files name

remove brackets from files name

(OP)
Hi

I have files where the name contains brackets. I would like to remove the brackets from the names. I am using ksh.

The files before:-
abc1(1).ctl
abc2(1).ctl
abc3(1).ctl
abc4(1).ctl

I would like to change them to:-
abc1.ctl
abc2.ctl
abc3.ctl
abc4.ctl

Thank you

Kais

RE: remove brackets from files name

Hi

You wrote "remove the brackets", but in your example you removed the parenthesis and everything between them. This code does the later :

CODE --> Ksh93

for file in *(*)*; do echo mv "$file" "${file//\(*\)}"; done 
As it is, this only outputs the commands to rename the files. If satisfied, remove the echo from the code and run it again to actually rename them.

Feherke.
feherke.ga

RE: remove brackets from files name

(OP)
Hi

Thank you for that but I had an error see below.

#for file in *(*)*; do echo mv "$file" "${file//\(*\)}"; done
sh: "${file//\(*\)}": The specified substitution is not valid for this command.

RE: remove brackets from files name

Hi

It works for me with Ksh93 and MirBSD Ksh. I assume you have Ksh88, but I not have that version. sad

This one works in Dash, so should work in any Bourne-compatible shell :

CODE --> Dash

for file in *(*)*; do echo mv "$file" "`echo "$file" | sed 's/(.*)//'`"; done 

But better see if you have rename ( sometimes rename.pl ) from the perl package ( the one from util-linux package replaces only fixed strings, but that one you probably not have anyway ) :

CODE

rename -n 's/\(.*\)//' *(*)* 
As it is, this only outputs a list of what would be renamed. If satisfied, remove the -n from the code and run it again to actually rename them.

Feherke.
feherke.ga

RE: remove brackets from files name

(OP)
Hi
Thank you again.
rename command is not available.
I had to amend the above because I have a file without the brackets too.
for file in *(*)*
do
nfile="`echo "$file" | sed 's/(.*)//'`"
if [[ $file != $nfile ]]; then
mv $file $nfile
fi
done

RE: remove brackets from files name

Hi

Oops. You are right, Kais. Either that, or fix my glob :

CODE

for file in *\(*\)*; do echo mv "$file" "`echo "$file" | sed 's/(.*)//'`"; done 

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close