LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-20-2009, 12:56 PM   #1
old-n-crazy
LQ Newbie
 
Registered: Jan 2009
Location: Seattle
Distribution: Ubuntu 9.04-9.10
Posts: 1

Rep: Reputation: 0
Removing characters from multiple filenames


I have a directory with several files which contain random unwanted characters in the filenames like:

027_blahblah.mp3
074blahblah3.txt

I would like to rmove (in bulk) specific characters in the file names so that after running the command the above would contain only characters:

blahblah.mp3
blahblah.txt

I am not scripting proficient but would like to be.
If anyone has recommendations for a good perl or python tutorial it would be much appreciated.
Thanks for any assistance with this!!

Last edited by old-n-crazy; 03-22-2009 at 11:51 AM. Reason: misspelled word in title
 
Old 03-20-2009, 02:42 PM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
First, exactly what characters would you like to remove or change ? I see an _ and numbers, anything else ? In fact it may be more prudent to say what characters to keep instead.

As an example I have a script that fixes names so they all are lowercase and removes spaces and illegal characters (or rather keeps good ones):

Code:
for i in *
do
  mv "$i" $(echo "$i" | awk '{print(tolower($0))}' | sed 's|[^-._a-z0-9]||g')
done
this happens for all files in the directory, but instead of '*' you could put '*.mp3' for all mp3s in the directory. However, I think you might want to customize it.

Last edited by H_TeXMeX_H; 03-20-2009 at 02:45 PM.
 
Old 03-20-2009, 03:13 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Maybe you should post a few actual filenames. Regex expressions can be very exacting, and "for instance" examples you make up of the top of your head may convey the actual pattern to use. Also, are the number of preceding characters the same? Do they all have the `_' character separating the random characters at the beginning from the filename?

1234_filename.ext
Using ${filename#*_} will remove the characters you want that use this pattern.
12-qqfilename.ext
Using ${filename#?????} will remove the characters for this type of pattern by simply removing the first 5 characters.

You also need to be careful if after removing the characters, the filenames are no longer unique.
1234_picture1.jpg
4321_picture1.jpg
 
Old 03-20-2009, 04:45 PM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Then run 'mv -i' to make it interactive or use '--backup' option to prevent clobbering.
 
  


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
Removing special characters from filenames PlymWS Linux - Software 1 08-09-2007 05:11 AM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 01:46 AM
smbmount problem with filenames from multiple codepages Cyber_Paladin Linux - Software 0 09-17-2006 01:27 PM
Bash Renaming (removing a string from filenames) FreeDoughnut Programming 9 07-20-2006 09:54 PM
Changing multiple filenames with a script brecki Linux - Newbie 8 01-30-2004 03:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:30 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
Open Source Consulting | Domain Registration