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 01-26-2006, 06:28 AM   #1
emailarron
LQ Newbie
 
Registered: Jan 2006
Posts: 1

Rep: Reputation: 0
Bash - Batch File Rename Help...


Hello - Hoping somone can help me here :

I have a few thousand files in a few hundred sub-directories.

IE
/categories/cars
/categories/houses
/categories/boats
/categories/business

some of the filenames within these directories contain incorrect characters that I want to just delete

/categories/business/someö_filenme.htm

I want to scan thru the entire directory, and batch delete the special character...

Any idea how to do this from command line?
thanks
-Arron
 
Old 01-26-2006, 06:39 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
What characters do you allow? a-z, A-Z, numbers, underscores, spaces(?), etc?
Try something like:
find top_dir -type f > list_of_files
you can then:
-either edit the list_of_files, deleting all files you want to keep
-use "grep" to get the files you don't want to keep (use regular expression)
Something like
grep -e [^a-zA-Z _] list_of_files
should do the trick. [] indicate a set of characters you want to match. Ranges are indicated by "-" (ie a-z
== abcdefghijklmnopqrstuvwxyz), the ^ after the [ tells grep that it should match any lines that have a
character that is NOT in the set (ie an unwanted character).
So, you may want to add additional characters to the set (just add them before the ] but after the ^) and
you will want to check grep's results afterwards.
Finally, you may also need to use grep's -E (extended regular expressions) option. See "man grep" for details
on how to use regular expressions with your version of grep.

Once you have the list of files, the rest is easy (in Bash):
for i in `cat list_of_files`; do
echo "Removing ${i}";
rm ${i};
done;
You may want to try the above loop, commenting the "rm" command first, just to see which files would get deleted. If it mentions only files you want to throw away, run the loop again, this time not commenting the "rm" command.

Hint: "find" will actually scan the directory and all subdirectories, if there are any.
If you don't want to look in the subdirectories, replace the "find" command I gave you with a simple "ls", but you'll need to omit the subdirectories then (for instance with a "if [[ ! -d ${i} ]];" test in the loop).

Last edited by timmeke; 01-26-2006 at 06:41 AM.
 
Old 01-26-2006, 07:25 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
There is also the tool mmv, once you have understood the syntax, its pretty efficient.
The method with find also works and doesn't need this additionnal tool
 
Old 01-26-2006, 08:11 AM   #4
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
sed (streaming editor) will read text (or input in text format from the cli), look for a specific pattern and chenage it to anything elso accoding to your specifications. The basic syntaxis for the command you'd need is:
sed -e 's/<what to look for>/<what to substitute it with/g'
if you combine it with grep as explained in post #2 and pipe the output from grep into sed you should have a good working sollution.
 
Old 01-26-2006, 08:35 AM   #5
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,397
Blog Entries: 1

Rep: Reputation: 64
more options

There are also the tools convmv and detox that could help, specially if this is caused by different char sets beetween a Samba server and a windows client.

regards,
 
  


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
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 02:26 PM
Batch Renaming in bash xushi Programming 6 07-07-2005 04:24 PM
Batch rename question hellblade Linux - Software 4 05-03-2004 04:57 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 07:52 AM
bash scripting/batch file type thing antken Linux - General 2 01-22-2001 08:56 AM


All times are GMT -5. The time now is 09:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration