Results 1 to 8 of 8

Thread: Changing file extension

  1. #1
    Join Date
    Apr 2008
    Location
    ~
    Beans
    307
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Changing file extension

    I am trying to learn all I can about linux commands and to practice, I am creating simple shell scripts to automate some of the things I do a lot in nautilus.

    I have a folder full of flash videos that I would like to give the .flv extension to, and this is the best I could come up with to do this, but it does not work...

    cd ~/Videos
    mv * ~/Videos/*.flv

  2. #2
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Changing file extension

    This will do it:

    Code:
    rename -n 's/.swf$/.flv/' *.swf
    It assumes that the existing extension is .swf. The -n option directs rename to do a dry run and report proposed changes; substitute -v for -n to actually rename the files.

    The following thread gives a lot of great info on the use of the rename utility:

    http://ubuntuforums.org/showthread.php?p=5210531
    Last edited by kaibob; February 5th, 2009 at 02:21 AM.

  3. #3
    Join Date
    Apr 2008
    Location
    ~
    Beans
    307
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Changing file extension

    Could you break that down for me?

    EDIT

    None of the files already have an extension.

    Thank you! that thread looks useful!
    Last edited by pikabuntu; February 5th, 2009 at 02:29 AM.

  4. #4
    Join Date
    Apr 2008
    Location
    ~
    Beans
    307
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Changing file extension

    Ok, this command is supposed to remove a file extension... Is there any way that I can change it to create one?

    rename -n 's/\.tmp$//' *.tmp

  5. #5
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Changing file extension

    This will add the flv extension to all files in the current directory:

    Code:
    rename -n 's/$/.flv/' *
    Rename is actually quite easy. The general format is:

    Code:
    rename 's/old/new/' files
    S means substitute. Old is the string that will be replaced. New is the string that will be substituted. Files are the files that will be renamed.

    The following are refinements:

    Change only at beginning of name: 's/^old/new/'
    Change only at end of name: 's/old$/new/'
    Change all instances of old: 's/old/new/g'

    Have a look at the following page which provides additional detail:

    http://tips.webdesign10.com/how-to-b...n-the-terminal
    Last edited by kaibob; February 5th, 2009 at 03:02 AM.

  6. #6
    Join Date
    Apr 2008
    Location
    ~
    Beans
    307
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Changing file extension

    Quote Originally Posted by kaibob View Post
    This will add the flv extension to all files in the current directory:

    Code:
    rename -n 's/$/.flv/' *
    Thanks sooo much! That's exactly what I needed.

  7. #7
    Join Date
    Apr 2008
    Location
    ~
    Beans
    307
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Changing file extension

    Hold on... How could I use this to rename only the files with no extension and not have it rename directories?

  8. #8
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Changing file extension

    There is probably an easier way to do this but the following works:

    Code:
    find /directory/name -maxdepth 1 -type f -exec rename -n 's/$/.flv/' '{}' ';'
    You have to change /directory/name. You can read about the find command at:

    http://content.hccfl.edu/pollock/Unix/FindCmd.htm
    Last edited by kaibob; February 5th, 2009 at 03:58 AM.

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
  •