LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2018, 12:14 PM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 73

Rep: Reputation: 0
Bash - Remove multiple brackets and text inside it from filename


Hi all,

I have regular files renamed by a program which i can't alter the output of to correct my issue.

The output adds brackets "()" around the date it adds which i want to remove.

Now i'm currently using the following to remove them myself but when it comes across a file with 2 sets of brackets in the name it removes all the text between the first and second set of brackets.

Code:
for file in $to_watch/*; do mv "$file" "${file/ (*)/}"; done
so when this runs on a filename with a two sets of brackets it removes the text like the following example.

FROM: filename (2010) something (2).ext
TO: filename.ext

any ideas how i can get it to remove the two sets of brackets and keep the text between the two?
 
Old 03-08-2018, 12:24 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Do you have the ' rename ' command installed in your system?
Code:
rename --version
 
Old 03-08-2018, 02:12 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
heres something
tested
Code:
userx@slackwhere101:~/changename
$ ls
myscrewyfile(1234)hello(2).tx


$ myfile=$(ls .) ; bo=${myfile//(} ; do=${bo//)} ; mv $myfile $do


$ ls
myscrewyfile1234hello2.tx

Last edited by BW-userx; 03-08-2018 at 03:01 PM.
 
Old 03-08-2018, 03:53 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Or you could enable extglob to do pattern list match
Code:
shopt -s extglob
for file in $to_watch/*; do mv "$file" "${file//+(\(|\))}"; done
 
Old 03-15-2018, 04:23 AM   #5
Springs
Member
 
Registered: Apr 2008
Posts: 73

Original Poster
Rep: Reputation: 0
thanks guys, will test when i can. my machines died at the moment so can't fully test.
 
Old 03-15-2018, 05:02 AM   #6
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
Code:
for file in $to_watch/*; do mv "$file" "${file// \(*\)/}"; done
twice the slash in order to do glob
 
Old 03-15-2018, 07:21 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
That will remove text between the parentheses
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script to read text and use it as filename Sturmie Programming 6 10-01-2013 08:53 AM
Quoting brackets inside a bash function sciss Linux - Newbie 11 09-30-2012 08:00 AM
[SOLVED] Bash Q: How to remove brackets from mp3 files? vinodub Linux - Newbie 6 02-07-2012 03:42 PM
[SOLVED] bash: how to replace symbols inside the file with filename deswarf Linux - Newbie 2 01-26-2012 04:55 AM
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration