LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2006, 07:40 AM   #1
intramaweb
LQ Newbie
 
Registered: May 2006
Location: Coventry, UK
Distribution: CentOS 5
Posts: 15

Rep: Reputation: 0
Bash script to traverse directory tree and rename files


I'm decommissioning a Mac OS X server which hosts a stack of files and directories with file names that contain illegal chacters. I need to sort the file names out before I can move the files.

I'm after a bash script that will traverse all the subdirectories within a given directory, and for each directory and file contained within, remove any space characters at the beginning or end of the directory/file name and replace any illegal characters with an alternative character, say an underscore or hyphen?

I think the code below is the beginning of what I'm after but...
- it doesn't deal with files names that have leading/trailing spaces
- it doesn't handle directories
- it only works on the files in one folder
- it only matches one illegal character ("/" in this eg.) (I'm not familiar with useage of sed to know if it can be used to match & replace multiple alternative chars)
- don't know if trying to match "/" will work because won't "ls /" list the root directory? Can I use "\/"?

Code:
#!/bin/bash

illegal=/
replace=_

for i in $( ls *$illegal* );
do
src=$i
tgt=$(echo $i | sed -e "s/$illegal/$replace/")
mv $src $tgt
done
 
Old 10-08-2006, 08:54 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Python alternative(only files):
Code:
import os
illegal = list('''!"#$%&\'()*+,/:;<=>?@[\\]^_`{|}~ ''')
repl = "_"
for root,dir,files in os.walk(os.getcwd()):
        for fi in files:
                for ill in illegal:
                        if ill in fi:     
                                print "found ",  os.path.join(root,fi)                        
                                newname = fi.replace(ill,repl)
                                os.rename( os.path.join(root,fi) , os.path.join(root,newname) )
Output:
Code:
linux:# /test/python test.py
found  /test/test .dat
found  /test/test'.dat

Last edited by ghostdog74; 10-08-2006 at 09:02 AM.
 
Old 10-08-2006, 12:42 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by intramaweb
I'm decommissioning a Mac OS X server which hosts a stack of files and directories with file names that contain illegal chacters. I need to sort the file names out before I can move the files.

I'm after a bash script that will traverse all the subdirectories within a given directory, and for each directory and file contained within, remove any space characters at the beginning or end of the directory/file name and replace any illegal characters with an alternative character, say an underscore or hyphen?

I think the code below is the beginning of what I'm after but...
- it doesn't deal with files names that have leading/trailing spaces
- it doesn't handle directories
- it only works on the files in one folder
- it only matches one illegal character ("/" in this eg.) (I'm not familiar with useage of sed to know if it can be used to match & replace multiple alternative chars)
- don't know if trying to match "/" will work because won't "ls /" list the root directory? Can I use "\/"?

Code:
#!/bin/bash

illegal=/
replace=_

for i in $( ls *$illegal* );
do
src=$i
tgt=$(echo $i | sed -e "s/$illegal/$replace/")
mv $src $tgt
done
What I'd like to know is how you manage to create a file with
a / in the name. I reckon you'll have SERIOUS problems getting
rid of that, because for anything command-line based it's going
to look like a partial path ... and of course, it won't be there.


Cheers,
Tink
 
Old 10-08-2006, 12:51 PM   #4
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
It is my experience that files that look like "foo/bar" from the graphical portion of OS X appear as "foo:bar" in the shell. I've never tried really hard to break this... but I would assume that it works the same way everywhere.
 
  


Reply

Tags
bash, characters, directory, file, illegal, name, rename, script


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
bash script? how to rename files in random way? lefty.crupps Linux - Software 16 11-03-2015 02:07 PM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Bash script to access all files in a directory shinni Programming 5 04-24-2009 03:46 PM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
Script tar+gzip traverse a directory gn00kie Programming 7 06-16-2006 01:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:00 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