LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 08-08-2005, 02:19 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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.
 
Old 08-08-2005, 02:31 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: Kennesaw, GA
Distribution: Ubuntu
Posts: 8,502

Rep: Reputation: 110Reputation: 110
Something LIKE this: (if you have enough disk space to copy them)
Code:
#!/bin/bash
mkdir COPY
find . -print0 | xargs -0 -i  'cp {} COPY/`echo {} | sed 's/[\[\]\|\?]/./g'`'
I'm not 100% sure that will work, and it's a kludge, but I think it will.
 
Old 08-19-2005, 10:18 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
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.
 
Old 08-20-2005, 02:56 PM   #4
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 32
Quote:
Originally posted by Matir
Something LIKE this: (if you have enough disk space to copy them)
Code:
#!/bin/bash
mkdir COPY
find . -print0 | xargs -0 -i  'cp {} COPY/`echo {} | sed 's/[\[\]\|\?]/./g'`'
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):
Code:
sed 's/[][|?*\]/./g'
 
Old 08-20-2005, 08:30 PM   #5
Matir
Moderator
 
Registered: Nov 2004
Location: Kennesaw, GA
Distribution: Ubuntu
Posts: 8,502

Rep: Reputation: 110Reputation: 110
oops, thanks for the catch.
 
Old 08-20-2005, 11:10 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: Kennesaw, GA
Distribution: Ubuntu
Posts: 8,502

Rep: Reputation: 110Reputation: 110
Quote:
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
}
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script to remove spaces from multiple filenames jeffreybluml Linux - Newbie 35 08-22-2011 03:32 AM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 01:46 AM
My remove script matthewa Programming 4 07-12-2005 06:24 AM
bash shell script appends '?' to filenames sceptreofjudah Linux - Newbie 3 03-18-2004 08:54 AM
Changing multiple filenames with a script brecki Linux - Newbie 8 01-30-2004 03:10 PM


All times are GMT -5. The time now is 02:36 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration