LinuxQuestions.org
Register a domain and help support LQ
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 12-09-2011, 07:56 AM   #1
Faye
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Rep: Reputation: 0
Remove a word in a filename


Hi

I am trying to remove a word in a filename for example this

Filename orginal is [bla bla bal ] document 1.zip

Now i want to remove that [bla bla bla] and rename it to document 1.zip.

I found some tips but none do what i like them to do. I tried detox but that program only deleted the brackets.

Also tried this command
Code:
rename -n "s/\[*]/-/g"
but that only delete the last bracket.and made a - (which i also dont want i rather want it gone for good.

Any pointers for me ?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-09-2011, 10:43 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Try
Code:
rename "s/\[.*\] *//" "[bla bla bal ] document 1.zip"
If the filename is stored in a shell variable, you can try a parameter substitution like this:
Code:
mv "$file" "${file/\[*\] }"
Please note that the rename command uses perl-like regular expressions, whereas the parameter substitution in bash uses patterns, hence the difference between the two forms.
 
2 members found this post helpful.
Old 12-09-2011, 11:10 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Have a look at these links:

parameter substitution
string manipulation
globbing
exended globbing
regular expressions introduction
regular expressions tutorial

Colucix's substitution is good, but this may be easier:
Code:
filename="[bla bla bal ] document 1.zip"
mv "$filename" "${filename#*] }"
It simply removes everything from the start of the name up to the first "]" and the space following it.

As for the rename command above, it will generally work, but since "*" is "greedy" in regex, if there ever happen to be two "]" characters in the string it will delete everything up to the second one.

In addition the using "[]" character range expression to match individual characters is considered more reliable than backslash escaping them (if not quite as readable in this specific case ).

So this version is a bit more robust:
Code:
rename 's/[[][^]]+[]] //' "[bla bla bal ] document 1.zip"
 
1 members found this post helpful.
Old 12-09-2011, 12:41 PM   #4
Faye
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Hmmm it doesnt work

I forgot to mentoin it is about multiple files and for some reason the file doesnt start with [ but with \[ . Could this \ give all the trouble ?
 
Old 12-09-2011, 02:17 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Well, to match a literal backslash in a regular expression you have to escape it using a... backslash:
Code:
\\
If you add this to the rename command suggested by David the H. followed by an asterisk, it should match both the files w/ and w/o a leading backslash in their name. Since you mentioned multiple file names, it should be useful at this point to see a list of the real files you want to rename or at least establish an exact criterion to remove all the unwanted parts, e.g.

remove all the parts embedded in square brackets (brackets included) possibly with leading backslashes and trailing blank spaces.
 
Old 12-09-2011, 03:14 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I think colucix has seized upon one of the essential concepts: the distinction between the use of patterns and regular expressions. It is also worth mentioning that assigning the filename to a shell variable provides opportunity for manipulation, and such manipulation may be performed both piece-wise and iteratively. This may lead to code that is easier to develop, easier to understand and easier to modify. In other words, you don't have to make the transformation in one step if it is confusing or overly complex.

--- rod.
 
Old 12-10-2011, 12:00 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Are you absolutely sure the name starts with "\["? If you use tab completion, then the shell will automatically backslash escape all shell-reserved characters in the completed match.

If you run echo *.zip, for example, what output matches do you get?

And see these links for details on how the shell handles whitespace and other reserved characters:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to delete all files with specific word in filename? moviecarpet Programming 13 05-26-2011 09:44 AM
Remove a part from a filename mainstream Linux - Newbie 10 03-29-2011 08:27 PM
[SOLVED] how to release file lock in SAMBA 3.2.5 share (MS Word [filename] is locked for edit. albertwt Linux - Newbie 5 11-04-2010 09:21 AM
[SOLVED] Changing one word in filename to uppercase musonio Linux - General 12 12-18-2009 09:38 AM
rm: cannot remove 'filename.extension' Operation not permitted shinystuffrox Linux - Software 8 03-07-2007 06:09 AM


All times are GMT -5. The time now is 07:59 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration