Results 1 to 5 of 5

Thread: bash rename command

  1. #1

    Default bash rename command

    Hi,
    I'm having a problem using the bash rename command on multiple directories. It keeps telling me 'not enough arguments'

    My scenario is that I have a directory named Output with about 40 sub directorieseach each containing another 10 or so sub directories that have names in a clock format of **:**:**_Measurements
    Example my directory structure looks like Output/2017_02_16/08:32:29_Measurements

    They were created by R on a Linux box and there is now a requirement for those directories to be moved to a Windows PC which of course will not deal with the : character.
    I therefore need a way to bulk rename all these directories.

    I can do:

    Code:
    find /Output -type d | grep ":"
    which duly gives me a list of all the time stamped directories with the : characters.

    I then try to further pipe that output to rename like

    Code:
    find /Output -type d | grep ":" | rename -v 's/://g'
    in the hope of stripping out the : character, however I just get a rename error informing me not enough arguments

    I have searched on the web for solutions and have found some slightly different variations using rename but all fail with the 'not enough arguments error.
    Eventually I found an article that suggests there are more than one variation of rename! and the syntax I am using is possibly that used for the Ubuntu version.. Could anyone provide advice on how one goes about renaming, or just replacing that pesky : character on multiple directories on SuSe???

    Thanks,

    PS. using SuSe 13.1 should that be relevant

  2. #2

    Default Re: bash rename command

    You're working way to hard. Just go into various directories and run this:

    Code:
    #Run once to get rid of the first colon
    rename ':' '' *:*
    #Run again to get rid of the second
    rename ':' '' *:*
    In other words, find a colon, replace it with nothing, for any files with
    colons in their names.

    If you want to do this hundreds of times, so doing so manually is not fun,
    then a quick loop should handle that when executed within the /Output
    directory:

    Code:
    for onedirectory in *; do
    cd "${onedirectory}";
    #Run once to get rid of the first colon
    rename ':' '' *:*;
    #Run again to get rid of the second
    rename ':' '' *:*;
    cd ..
    done
    Actually, this is cleaner:

    Code:
    #Ignore the errors.
    find . -type d -name '*:*' -exec rename ':' '' '{}' ';'
    find . -type d -name '*:*' -exec rename ':' '' '{}' ';'
    Another option is to set a variable with the original name, create a
    second variable with the new name (use 'sed' to get a version sans colons)
    and then use 'mv' to just do a change of name. For example:

    Code:
    for dir0 in *; do
    for dir1 in $(find "${dir0}" -type d -iname '*:*'); do
    newdir=$(echo "${dir1}" | sed -e 's/://g');
    echo "Renaming ${dir1} to ${newdir}";
    mv "${dir1}" "${newdir}";
    done;
    done;
    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below...

  3. #3

    Default Re: bash rename command

    Actually, this is cleaner:

    Code:
    #Ignore the errors.
    find . -type d -name '*:*' -exec rename ':' '' '{}' ';'
    find . -type d -name '*:*' -exec rename ':' '' '{}' ';'
    As you suggested with 40 odd directories containing 400+ subdirectories, to change them manually going to each one was not going to be much fun.
    The code you supplied above worked perfectly when run from within the top level directory 'Output'. Not sure why it was necessary to run it twice - or why it generated 'No such file or directory' error for each one it processed but the end result is it stripped out all the :'s

    Thank you so much!

    Edit: just figured out why I had to run it twice - first time to get rid of the first colon and second time to get rid of the second one - right?

  4. #4

    Default Re: bash rename command

    On 03/04/2017 05:46 PM, shadychris wrote:
    >
    > Actually, this is cleaner:
    >
    >
    > Code:
    > --------------------
    >
    > #Ignore the errors.
    > find . -type d -name '*:*' -exec rename ':' '' '{}' ';'
    > find . -type d -name '*:*' -exec rename ':' '' '{}' ';'
    >
    > --------------------
    >
    > As you suggested with 40 odd directories containing 400+
    > subdirectories, to change them manually going to each one was not going
    > to be much fun.
    > The code you supplied above worked perfectly when run from within the
    > top level directory 'Output'. Not sure why it was necessary to run it
    > twice - or why it generated 'No such file or directory' error for each
    > one it processed but the end result is it stripped out all the :'s


    The reason twice: apparently 'rename' only replaces one instance of the
    sought string (with the replacement string) at a time, and I could not
    find a way to have it go a global match like I could with a standard regex
    (see the other example with 'sed' which did that successfully).

    Glad to hear it worked; thank-you for posting back your results.

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below...

  5. #5

    Default Re: bash rename command

    Hi,

    I would first try to use the good ol mv inside a loop and with find, since find is recursive by default.
    Something like this might do the trick.


    Code:
    find Output/ -type d -name '*:*' -exec bash -c 'for d do echo mv -v "$d" "${d//:}"; done' {} +
    The echo is there just to show you whats going to happen. If the stdout result is fine with you then you can just remove the echo so the mv can actually do its job. I know I have a late reply but hey...
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

Posting Permissions

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