Page 2 of 6 FirstFirst 1234 ... LastLast
Results 11 to 20 of 59

Thread: How to search and replace file name characters

  1. #11
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Quote Originally Posted by schragge View Post
    Yes. The -v in rename -v is a short form of --verbose. This option will print the name of each file being renamed. Similarly, -n is a short form of --no-act. Have a look at the manual page for rename I linked above.
    Ok... It seem to work but it appears there's another problem character. Any name that starts with a "." will also stop the transfer.
    So would the correct syntax for taking care of that be:

    Code:
    -type f -exec rename -vn 's/:|∕|./-/g' '{}' +
    adding "|." in order to also change the "." to a dash?

  2. #12
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Quote Originally Posted by papibe View Post
    Those help, but it would be very useful some actual names, for instance if you could paste the result of a 'ls' command.

    Regards.
    Sorry?
    The first example is the exact name that actually caused the transfer to stop.

    I manually renamed it with the dash and it was no longer a problem.

  3. #13
    Join Date
    Aug 2011
    Location
    52° N 6° E
    Beans
    2,163
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: How to search and replace file name characters

    I think it would require a \. as . itself is a wildcard character.

  4. #14
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    As a pipe separates the two problem characters in schragge's example, I'd thought another pipe would be necessary before a third character.

  5. #15
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Yes the pipe is needed, but the backslash is needed, too, to escape the special meaning of dot in regular expression:
    Code:
    find -type f -exec rename -vn 's/:|∕|\./-/g' '{}' +
    Beware that this will convert all dots to dashes. Are you sure this is what you want? I'd rather
    Code:
    find -type f -exec rename -vn 's/:|∕|^\./-/g' '{}' +
    to only convert the dot if it's the first character of file name.
    Last edited by schragge; March 19th, 2013 at 12:59 AM.

  6. #16
    Join Date
    Aug 2011
    Location
    52° N 6° E
    Beans
    2,163
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: How to search and replace file name characters

    Yes, I meant s/:|∕|\./g

    It's a regular expression, so, although I don't know much about the rename command, I do know that . is a wildcard character that has to be escaped, or it will mach anything. But with the n option it will show what will happen before you actually do the rename.

  7. #17
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Quote Originally Posted by schragge View Post
    Yes the pipe is needed, but the backslash is needed, too, to escape the special meaning of dot in regular expression:
    Code:
    find -type f -exec rename -vn 's/:|∕|\./-/g' '{}' +
    Beware that this will convert all dots to dashes. Are you sure this is what you want? I'd rather
    Code:
    find -type f -exec rename -vn 's/:|∕|^\./-/g' '{}' +
    to only convert the dot if it's the first character of file name.
    Yes it is for files beginning with "."
    Thanks.

  8. #18
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Quote Originally Posted by schragge View Post
    Yes the pipe is needed, but the backslash is needed, too, to escape the special meaning of dot in regular expression:
    Code:
    find -type f -exec rename -vn 's/:|∕|\./-/g' '{}' +
    Beware that this will convert all dots to dashes. Are you sure this is what you want? I'd rather
    Code:
    find -type f -exec rename -vn 's/:|∕|^\./-/g' '{}' +
    to only convert the dot if it's the first character of file name.
    After determining that the command worked with the -n switch, I've now run it without and the output is claiming:

    "Can't rename /<pathtodirectory>/<Band/Artist Name>/<Album Title>: <subtitle>/<specific track.mp3>: No such file or directory"

    I've randomly verified that several of the claimed non-existent directories/files are there and named just as the output shows them.

    This is the actual command I ran:
    Code:
    $ find /media/newest3TB/ -type f -exec rename -v 's/:|∕|^\./-/g' '{}' +
    I run it again this time including the -n switch and it says everything was renamed properly.

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

    Re: How to search and replace file name characters

    wouldn't
    Code:
    s/^[.]|[∕:]/-/g
    be cleaner? square brackets are just for that


    /<pathtodirectory>/<Band/Artist Name>/<Album Title>: <subtitle>/<specific track.mp3>
    don't you need to only rename directories? If the file names are peachy then it's all that's required. Furthermore that might be source of the problem with your current code. It tries to change the whole path of the file, which means moving files to a destination that doesn't exist yet.
    Last edited by Vaphell; March 19th, 2013 at 03:41 AM.
    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. #20
    Join Date
    Jul 2006
    Beans
    Hidden!

    Re: How to search and replace file name characters

    Quote Originally Posted by Vaphell View Post
    wouldn't s/[∕.]/-/g be cleaner? square brackets are just for that
    Frankly I have no idea.
    All of this is somewhat beyond my knowledge.
    I'm trying to rename files to change all instances of these three characters "/", ":" and "." where the filename begins with "." to "-".
    As a test, the code's output looks fine.
    Not sure what happened when the -n switch was removed.

Page 2 of 6 FirstFirst 1234 ... 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
  •