LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to traverse directory tree and rename files (https://www.linuxquestions.org/questions/programming-9/bash-script-to-traverse-directory-tree-and-rename-files-490552/)

intramaweb 10-08-2006 07:40 AM

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


ghostdog74 10-08-2006 08:54 AM

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


Tinkster 10-08-2006 12:42 PM

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

frob23 10-08-2006 12:51 PM

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.


All times are GMT -5. The time now is 08:00 PM.