Results 1 to 5 of 5

Thread: Script Required for Manipulating Directory Names

  1. #1
    Join Date
    Oct 2008
    Beans
    116
    Distro
    Xubuntu 12.04 Precise Pangolin

    Question Script Required for Manipulating Directory Names

    I'm looking for a simple bash script that I can execute that will strip the first 5 chars of each folder name in a given folder.

    Any ideas?

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Script Required for Manipulating Directory Names

    something like

    Code:
    find . -type d -exec rename -nv 's/.{5}//' {} \;
    maybe? remove the -n option when you've got the match working right

    You can add a -maxdepth to the find if you don't want it to descend recursively, or just

    Code:
    rename -nv 's/.{5}//' */
    to just rename directories directly below the current dir
    Last edited by steeldriver; September 5th, 2012 at 02:23 PM.

  3. #3
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    rename

    That munges the beginning of the paths!

    Here's a variant:

    Code:
    find /some/path/ -type d -exec rename -nv 's/\/[^\/]{5}([^\/]*)$/\/$1/' {} \;
    Edit: thanks steeldriver, it's now corrected.
    Last edited by Lars Noodén; September 5th, 2012 at 03:22 PM. Reason: catching the tail end of the directory name explicitly

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Script Required for Manipulating Directory Names

    oops you're right - thanks for the correction Lars!

    EDIT: I think the regex might need something to explicitly catch the trailing part though?

    Code:
    's/\/[^\/]{5}([^\/]*)$/\/$1/'
    Last edited by steeldriver; September 5th, 2012 at 03:15 PM.

  5. #5
    Join Date
    Oct 2008
    Beans
    116
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Script Required for Manipulating Directory Names

    Thanks, I will try this later

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
  •