Page 5 of 6 FirstFirst ... 3456 LastLast
Results 41 to 50 of 59

Thread: How to search and replace file name characters

  1. #41
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Quote Originally Posted by Vaphell View Post

    tell me, does something like this return the correct result?
    Code:
    $ n='JON ANDERSON -Lord Of The Rings: The Unreleased Yes Songs -[no label 1CD]'
    $ echo "$n" | sed -r 's/[⁄∕:]/-/g'
    JON ANDERSON -Lord Of The Rings- The Unreleased Yes Songs -[no label 1CD]
    if so, you can plug it in in place of ${n//.....}
    Code:
    mv -- "$fd" "$p"/"$(echo "$n" | sed -r 's/[⁄∕:]/-/g' )";
    edit:
    as papibe mentioned and i started suspecting this might be an issue with the colon not being the one we think it is, just like there are these other slashes.
    I'm not sure I have this right but here's what I tried and the result:
    Code:
    /media/newest3TB/0 test$ while read -rd $'\0' fd; do p=${fd%/*}; n=${fd##*/}; echo mv -- "$fd" "$p"/$(echo "$n" | sed -r 's/[//:]/-g' )"; done < <( find "/media/newest3TB/0 test" -depth -iname '*[⁄∕:]*' -print0 )
    >
    I think I've got something wrong.

    You placed a quotation mark here:
    mv -- "$fd" "$p"/"$(echo "$n" | sed -r 's/[⁄∕:]/-/g' )";

    That wasn't in your first example. Also... the space between the g' and the ).. was intentional?

  2. #42
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,364
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to search and replace file name characters

    wth, your bash version seems borked for whatever reason o.O This simply should work, it's a pretty basic bash feature (one of builtin parameter expansion operations)
    http://www.gnu.org/software/bash/man...Expansion.html

    quotes are used to make sure the text inside them stays as a single chunk (unshielded spaces cut strings to pieces by default and command sees greater number of bad parameters

    'levels' are as follows
    Code:
    echo "$n" | sed -r 's/[//:]/-g'  <- we need to capture the output of this and embed it in mv command
    $( echo "$n" | sed -r 's/[//:]/-g' )    <- name can contain spaces, so we need to quote it
    "$( echo "$n" | sed -r 's/[//:]/-g' )"
    put together with the rest
    Code:
    mv ... "fd" "$p"/"$( ... )"  <- two quoted strings glued together with /
    you can also make it a single "..."
    Code:
    mv ... "fd" "$p/$( ... )"    <- single string with / inside


    space in $(_..._) don't matter, i simply find them more readable, it appears i didn't type the other one
    Last edited by Vaphell; March 19th, 2013 at 11:56 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #43
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Here's an update...
    Weirdness.
    I've booted into my Precise install (12.04.2) with bash version 4.2.24(1). I cd'd to the 0 test dir and tried the command, it tells me that the 0 test directory doesn't exist in /media/newest3TB!
    I renamed it 0test, but it is also not recognized.

    On booting it reported an "internal error" - sent the report and proceded.

    Before I ran the command I tried updating via the GUI but it reported it couldn't download the packages and to check the internet connection. No problem accessing web pages <sigh>.
    Then ran update from the CLI and it updated with no problem.

    I haven't tried an upgrade for several years but tried with this install and it tells me there is no new version. I'm guessing since it's the latest LTS version it won't upgrade to a non-LTS version?
    I'm presently downloading and am going to install the Ubuntu-gnome-12.10.1-amd64 (I'm not particularly fond of Unity) version and try again to change those pesky characters.
    Last edited by meanmrmustard; March 20th, 2013 at 11:04 AM.

  4. #44
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: How to search and replace file name characters

    TBH, I think we all got misled into thinking there are some weird Unicode characters in the file names. I feel my share of guilt as I was the first who suggested this. Now, I'm inclined to agree with
    Quote Originally Posted by Vaphell View Post
    i see that you didn't have a problem with slashes at all, i somewhat expected that.
    I guess the NAS is NTFS-formatted and mounted with the option windows_names that prevents creating files with colon and some other characters in file names.

    I further guess the part of the problem now is that the OP somehow manages to put normal slashes in the sed expression:
    Quote Originally Posted by meanmrmustard View Post
    Code:
    sed -r 's/[//:]/-/g'
    No wonder it doesn't work.

    Try only replacing colons instead:
    Code:
    sed 's/:/-/g'
    This should be enough.
    Last edited by schragge; March 20th, 2013 at 11:43 AM.

  5. #45
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Quote Originally Posted by schragge View Post
    I guess the NAS is NTFS-formatted and mounted with the option windows_names that prevents creating files with colon and some other characters in file names.
    .
    Actually the NAS is formatted as ext4.
    Now, I'm inclined to agree with
    Originally Posted by Vaphell
    i see that you didn't have a problem with slashes at all, i somewhat expected that.
    I'm not understanding why it seems there's no problem with the slashes.
    I've had to rename some of the files that caused the transfer to choke and there were no colons in the names, only a slash.
    Changing it to a dash allowed the transfer to continue.
    Last edited by meanmrmustard; March 20th, 2013 at 03:19 PM.

  6. #46
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Sorry, then I must have been wrong and stand corrected. Hmm, how does your NAS get mounted? As a Samba share? Check the options mapchars and iocharset of smbmount.

  7. #47
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,364
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to search and replace file name characters

    I'm not understanding why it seems there's no problem with the slashes.
    your few examples seem to have only : but none of "the other slashes" so it's somewhat understandable to suspect that slashes were not the problem. If you say you have / in names and have to change them manually then the suspicion is false.
    You should give the most broken example possible that covers all corner cases or compile a sample that covers them all.
    Last edited by Vaphell; March 20th, 2013 at 08:38 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #48
    Join Date
    Apr 2012
    Beans
    5,461

    Re: How to search and replace file name characters

    I've been having a bit of a play with this as well

    Rather than having to guess / copy-paste the troublesome UTF-8 characters, I wonder if it might work to replace essentially the whole non-ASCII unicode range? it seems to work in 'sed'

    Code:
    $ > a$'\u2215'funny$'\u2215'file
    $ > another$'\u2215'funny$'\u2215'file
    $
    $ ls
    a∕funny∕file  another∕funny∕file
    $
    $ for file in *; do echo mv "$file" "$(sed s/[^U+0000-U+007f]/-/g <<< "$file")"; done
    mv a∕funny∕file a-funny-file
    mv another∕funny∕file another-funny-file
    $
    I can't make it work in 'rename' though - maybe one of the perl folks on here can figure it out?
    Code:
    $ rename -nv 's/[^U+0000-U+007f]/-/g' *
    a∕funny∕file renamed as ----f-------f---
    another∕funny∕file renamed as ----------f-------f---
    $
    Last edited by steeldriver; March 20th, 2013 at 11:37 PM.

  9. #49
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,364
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to search and replace file name characters

    reportedly ^\x{0000}-\x{007f} should match non-ascii but i think it's too broad and will destroy non-english names/titles.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #50
    Join Date
    Apr 2012
    Beans
    5,461

    Re: How to search and replace file name characters

    ^^^ that's a good point - perhaps adjusting the range like [U+0020-U+024f] (printable ASCII through Latin Extended-B) or [U+0020-U+1fff] (printable ASCII through 'Greek Extended' - stops short of 'General Punctuation')

    Code:
    for file in *; do echo mv "$file" "$(sed s/[^U+0020-U+024f]/-/g <<< "$file")"; done
    Code:
    for file in *; do echo mv "$file" "$(sed s/[^U+0020-U+1fff]/-/g <<< "$file")"; done

Page 5 of 6 FirstFirst ... 3456 LastLast

Bookmarks

Posting Permissions

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