Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove brackets from files name 1

Status
Not open for further replies.

kaisalhassani

Programmer
Sep 2, 2007
8
0
0
GB
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
 
Hi

You wrote "remove the brackets", but in your example you removed the parenthesis and everything between them. This code does the later :
Code:
[b]for[/b] file [b]in[/b] [teal]*(*)*;[/teal] [b]do[/b] echo mv [i][green]"$file"[/green] [green]"${file//[/green][/i][lime]\([/lime][i][green]*[/green][/i][lime]\)[/lime][i][green]}"[/green][/i][teal];[/teal] [b]done[/b]
As it is, this only outputs the commands to rename the files. If satisfied, remove the [tt]echo[/tt] from the code and run it again to actually rename them.

Feherke.
feherke.ga
 
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.
 
Hi

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

This one works in Dash, so should work in any Bourne-compatible shell :
Code:
[b]for[/b] file [b]in[/b] [teal]*(*)*;[/teal] [b]do[/b] echo mv [i][green]"$file"[/green] [green]"`echo "$file" | sed 's/(.*)//'`"[/green][/i][teal];[/teal] [b]done[/b]

But better see if you have [tt]rename[/tt] ( sometimes [tt]rename.pl[/tt] ) 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 [green][i]'s/\(.*\)//'[/i][/green] [teal]*(*)*[/teal]
As it is, this only outputs a list of what would be renamed. If satisfied, remove the [tt]-n[/tt] from the code and run it again to actually rename them.

Feherke.
feherke.ga
 
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
 
Hi

Oops. You are right, Kais. Either that, or fix my glob :
Code:
[b]for[/b] file [b]in[/b] [teal]*[highlight]\[/highlight](*[highlight]\[/highlight])*;[/teal] [b]do[/b] echo mv [i][green]"$file"[/green] [green]"`echo "$file" | sed 's/(.*)//'`"[/green][/i][teal];[/teal] [b]done[/b]


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top