Results 1 to 7 of 7

Thread: complexed rename folders?

  1. #1
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    486
    Distro
    Ubuntu

    complexed rename folders?

    Hello, I have a folder with a 100s of sub-folders so I need to automate this if possible..

    example...
    brad and sarah in france 00:15:56
    brad and sarah at home 00:21:04
    pop and gill in the USA 00:56:09
    pop and gill at home 00:08:12

    I want to end up with the following...

    brad and sarah in france 15
    brad and sarah at home 21
    pop and gill in the USA 56
    pop and gill at home 08

    so basically the first 2 numbers and the :colon should be removed and the :colon and the last 2 numbers should be removed recursively.

    and if possible I would like to keep the first 2 numbers if they are other than 00 , eg 01:25:45 but all colons must be removed or replaced with a dash - so if there were a number starting 01 and the middle numbers were 25 I would end up with 01-25

    how can this be done from the command line?

    thanks

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

    Re: complexed rename folders?

    You can do this kind of thing with rename:

    Code:
    rename -n 's/\d\d:(\d\d):\d\d$/$1/' *
    The -n stops it from actually making any changes. Remove it to really rename the files.
    However that only works with one folder at a time and you would likely have to combine it with find to get all the subdirectories.

    Code:
    find . -type f -exec rename -n 's/\d\d:(\d\d):\d\d$/$1/' "{}" \;
    Do make a backup with tar first before renaming, just in case.

  3. #3
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    486
    Distro
    Ubuntu

    Re: complexed rename folders?

    thanks but it didnt appear to do anything at all, neither did it give any error message, I did remove the -n

    I cd in to the folder with the sub folders first then -
    Code:
     find . -type f -exec rename 's/\d\d:(\d\d):\d\d$/$1/' "{}" \;

    Quote Originally Posted by Lars Noodén View Post
    You can do this kind of thing with rename:

    Code:
    rename -n 's/\d\d:(\d\d):\d\d$/$1/' *
    The -n stops it from actually making any changes. Remove it to really rename the files.
    However that only works with one folder at a time and you would likely have to combine it with find to get all the subdirectories.

    Code:
    find . -type f -exec rename -n 's/\d\d:(\d\d):\d\d$/$1/' "{}" \;
    Do make a backup with tar first before renaming, just in case.

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

    Re: complexed rename folders?

    Try it with the -n until you get the output you want. rename is a perl script so these are perl regular expressions (known outside of perl as PCRE). So anything you can write as a perl regular expression will work.

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

    Re: complexed rename folders?

    "if possible I would like to keep the first 2 numbers if they are other than 00 , eg 01:25:45 but all colons must be removed or replaced with a dash - so if there were a number starting 01 and the middle numbers were 25 I would end up with 01-25"

    I didn't see that the first time around. That can be done by adding a second search and replace:

    Code:
    rename -n 's/00:(\d\d):\d\d$/$1/; s/(\d\d):(\d\d):\d\d$/$1-$2/' ./*

  6. #6
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    486
    Distro
    Ubuntu

    Re: complexed rename folders?

    thanks but I must be missing something very basic here, non of these commands do anything at all?

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

    Re: complexed rename folders?

    I've made four files using the sample names you gave. Here's what I get with ls and rename:

    Code:
    $ ls -1
    brad and sarah at home 01:21:04
    brad and sarah in france 00:15:56
    pop and gill at home 00:08:12
    pop and gill in the USA 00:56:09
    
    $ rename -n 's/00:(\d\d):\d\d$/$1/; s/(\d\d):(\d\d):\d\d$/$1-$2/' ./*
    ./brad and sarah at home 01:21:04 renamed as ./brad and sarah at home 01-21
    ./brad and sarah in france 00:15:56 renamed as ./brad and sarah in france 15
    ./pop and gill at home 00:08:12 renamed as ./pop and gill at home 08
    ./pop and gill in the USA 00:56:09 renamed as ./pop and gill in the USA 56
    I'm not sure why your output is different. rename is a regular part of Ubuntu.

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
  •