Find the answer to your Linux question:
Results 1 to 5 of 5
Hi All, I’m trying to copy a large number of files from a Linux server to a Windows file share. Unfortunately, all of the files and folders I have to ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8

    Question [SOLVED] Change file names to remove invalid character.

    Hi All,

    I’m trying to copy a large number of files from a Linux server to a Windows file share. Unfortunately, all of the files and folders I have to copy have 10 numbers followed by 2 colons "::" in the name (example: 1234567890::WordDoc.doc) which of course is invalid in windows naming conventions. So now I'm trying to come up with a way to change the file and folder names on the fly to replace the colons with a dash "-" or space " ". I'm even willing delete the frist 12 characters in necessary.

    I have tried cp, mv, tr, and several -bash scripts but nothing seems to help.

    I'm really desperate at this point and would appreciate any help.

    Thanks

    Ray Laureyns
    Last edited by laureynsr; 04-01-2009 at 08:35 PM. Reason: More details

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    I was just helping someone with this a few weeks ago

    You can use a for loop to rename each file in Linux:

    Code:
    for filename in `ls` ; do mv $filename `echo $filename | sed s/:://` ; done
    Make sure you have backups before trying this though incase it isn't what you want

    You can see the full explanation in this post:http://www.linuxforums.org/forum/sus...files-two.html
    Linux User #453176

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8
    Hi Kieren,

    Thanks for the tip, but I might be missing something. I copied the folder to another location to test and created a tried the command as stated:
    for filename in 'ls' ; do mv $filename 'echo $filename | sed s/:://' ; done

    I get "mv: cannot stat `ls': No such file or directory" I am running this from the path where the files and folders are.

    I know there is a way of doing this even if I have to parse file and foler names so everything to the left of the "::" is deleted.

    Thanks

    Ray Laureyns

  4. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You have wrapped ls in single quotes (') when it should be an apostrophe (`)

    apostrophe can be found (on my keyboard anyway) next to the number 1 key. The same is needed around "echo $filename | sed s/:://"

    Here is what you have used:

    Code:
    for filename in 'ls' ; do mv $filename 'echo $filename | sed s/:://' ; done
    And here is what it should be:

    Code:
    for filename in `ls` ; do mv $filename `echo $filename | sed s/:://` ; done
    Linux User #453176

  5. #5
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8
    Kieren,

    You are a LIFE SAVER! (My mistake on the single quote vs apostrophe). Worked on 90% of the files and folders accept the ones with spaces in the names or brackets "()". Those (about 85 files) I'm renaming by hand encapsulating in double quotes.

    Thank you VERY MUCH for your help!!

    Ray

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...