need help with script to remove all metachars from filenames
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
need help with script to remove all metachars from filenames
I have a bunch of files on a mac that allows metachars in the file names.. I need to move those files to a linux box that doesn't allow metachars in the file names, so I need to change the file names before they get to the Linux machine.
I'm at a bit of a loss on a couple of things - how to search/replace more than one thing at a time (i.e. search for '[' and ']' and '|' and '?' and replace with '.' all in one operation), and how to recurse directories while doing so.
My intial guess would be change the file names on their way into a tar archive that's piped over ssh from the mac to a linux box, that way the files aren't modified on the mac, but I'm not sure how to do that (or if it's even possible). I can change the file names on the mac if necessary, but always prefer to leave files untouched wherever possible.
Language doesn't matter, I had planned on using a shell script with simple tools like sed, but could easily use a perl script or even a C program if necessary.
Well, as it turns out, it's not so trivial. The problem is, when you do any sort of find the directory list gets stored at the start of the find, so if you modify a directory name, you no longer have access to that directory and therfore can't recurse down into it. I ended up writing a perl script to do it. I suppose it could have been done any number of other ways, but they all need to manually recurse, rather than relying on some sort of a glob of files from some given start point.
I'm not 100% sure that will work, and it's a kludge, but I think it will.
It won't. You don't have to escape characters inside [ ]s and if you want to match ] it must the first character after the [. This will do it (I've added * to the list for completeness):
Originally posted by BrianK Well, as it turns out, it's not so trivial. The problem is, when you do any sort of find the directory list gets stored at the start of the find, so if you modify a directory name, you no longer have access to that directory and therfore can't recurse down into it. I ended up writing a perl script to do it. I suppose it could have been done any number of other ways, but they all need to manually recurse, rather than relying on some sort of a glob of files from some given start point.
I think the important part would be to modify the directory name AFTER recursing it. (Post-order recursion). Pseudocode:
Code:
function handledir{
cd $1
for file in *
do [ -d $file ] && handledir $file
update_file $file
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.