Announcement

Collapse
No announcement yet.

bash script for renaming files

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    bash script for renaming files

    Hello I got lots of media files and would like to be able to easily rename them. I use midnight commander but it takes too long to specify current directory as it defaults to moving the file while renaming.
    So I decided to try to create a bash script that could do this. I am hoping to achieve this
    filename1.avi

    renamescript filename1.avi newfilename1 (i would get first filename by doing alt+enter in midnight commander so script would have to support like / spacing etc.

    I would like it to preserve the file extension without having to retype it.

    I have been trying but am very new to bash scripting so I am too embarassed to put what I have here. i know i could just do mv filename1.avi filename2.avi but i am just wanting to make this quicker and prevent errors.

    Thank you for any input on this hope it's in the right section.

    #2
    Re: bash script for renaming files

    Maybe these help:

    > Topic: Moving/Renaming Music Files
    > Topic: How Do You Rename Multiple Files Using Console?
    > Topic: Rename Multiple Files


    Afterthought

    I'm not sure how the midnight commander works but with the Konqueror/Dolphin/Krusader you can use Krename.

    man krename
    DESCRIPTION
    KRename is a batch file renamer which can rename a list of files based
    on a set of expressions.
    Before you edit, BACKUP !

    Why there are dead links ?
    1. Thread: Please explain how to access old kubuntu forum posts
    2. Thread: Lost Information

    Comment


      #3
      Re: bash script for renaming files

      Thank you for the links. I read those and have googled this but can't seem to find exactly what I am looking for.

      Want a bash script for learning and I enjoy the konsole. I am hoping to just as said have a script where I could type like
      myrenamescript filename1.avi newfilename1
      result would be newfilename1.avi

      right now mv filename1.avi newfilename1.avi works of course but hoping to shorten it and I will improve on it myself as needed I enjoy learning bash scripting. I am hoping to be able to put spaces in there without any \ or anything too.

      Thanks this is of course not a big problem but I appreciate any help.

      Comment


        #4
        Re: bash script for renaming files

        Maybe this example helps:


        Making text file RE

        RE:
        Code:
        #!/bin/sh
        
        extension="${1##*.}" 
        mv "$1" "$2.$extension"

        Right clicking RE (Dolphin/Konqueror) > Properties > Permissions > is executable

        Dropping it in to the /usr/bin/

        Check:
        Code:
        $ ls -l /usr/bin/RE
        -rwxr-xr-x 1 root root 58 2008-06-22 22:51 /usr/bin/RE

        Test

        Before:
        Code:
        $ ls -l "/media/sda6/tmp_rename/Something with spaces.jpg"
        -rw-r--r-- 1 rog rog 74668 2008-06-12 22:44 /media/sda6/tmp_rename/Something with spaces.jpg
        Using RE:
        Code:
        $ RE "Something with spaces.jpg" "Something else with spaces"
        After:
        Code:
        $ ls -l "/media/sda6/tmp_rename/Something else with spaces.jpg"
        -rw-r--r-- 1 rog rog 74668 2008-06-12 22:44 /media/sda6/tmp_rename/Something else with spaces.jpg

        Links
        > Howto read command line arguments in a bash script
        > Bash script help - rename script
        Before you edit, BACKUP !

        Why there are dead links ?
        1. Thread: Please explain how to access old kubuntu forum posts
        2. Thread: Lost Information

        Comment


          #5
          Re: bash script for renaming files

          Great that little script got me going thank you so much. I modified it a little to my liking this works great. I know there is much more efficient ways of doing it but it works! Now in midnight commander I can just highlight the file I want to rename like this
          (alt+enter puts the filename with special characters to make it one string)
          scriptname (alt+enter) this is my new file

          No quotes or nothing thanks maybe someone wants to improve on this or use it feel free.

          #!/bin/sh
          extension="${1##*.}"
          if [ -n "$8" ]
          then mv "$1" "$2 $3 $4 $5 $6 $7 $8.$extension"
          exit 1
          else echo "success"
          fi
          if [ -n "$7" ]
          then mv "$1" "$2 $3 $4 $5 $6 $7.$extension"
          exit 1
          else echo "success"
          fi
          if [ -n "$6" ]
          then mv "$1" "$2 $3 $4 $5 $6.$extension"
          exit 1
          else echo "success"
          fi
          if [ -n "$5" ]
          then mv "$1" "$2 $3 $4 $5.$extension"
          exit 1
          else echo "success"
          fi
          if [ -n "$4" ]
          then mv "$1" "$2 $3 $4.$extension"
          exit 1
          else echo "success"
          fi
          if [ -n "$3" ]
          then mv "$1" "$2 $3.$extension"
          exit 1
          else echo "success"
          fi
          if [ -n "$2" ]
          then mv "$1" "$2.$extension"
          exit 1
          else echo "success"
          fi






          Comment

          Working...
          X