I want to rename all files in a directory from *.ts to *.mkv [duplicate]












2
















This question already has an answer here:




  • Changing extension to multiple files

    12 answers




Rename all the files in .ts files in a directory
My echo command works but not if I try to make it a new variable.



#!/bin/sh
for file in "${1}"/*.ts; do
echo ${file} | sed -e 's|.ts|.mkv|'
new_name=${file} | sed -e 's|.ts|.mkv|'
done









share|improve this question















marked as duplicate by Jeff Schaller, Sparhawk, Isaac, heemayl, Mr Shunz Jan 28 at 9:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Just use rename. If the Perl version, it's just rename 's/.ts$/.mkv' *.

    – Sparhawk
    Jan 28 at 1:38











  • I want to save the renamed file to a variable to pass in another command in the script (input name -> output name). The examples are for renaming them in the directory

    – NasKar
    Jan 28 at 3:22






  • 1





    Please edit your question to include this information.

    – Sparhawk
    Jan 28 at 3:39











  • Are your files named with .ts actually Matroska files? Why are they all misnamed?

    – jamesdlin
    Jan 28 at 5:57


















2
















This question already has an answer here:




  • Changing extension to multiple files

    12 answers




Rename all the files in .ts files in a directory
My echo command works but not if I try to make it a new variable.



#!/bin/sh
for file in "${1}"/*.ts; do
echo ${file} | sed -e 's|.ts|.mkv|'
new_name=${file} | sed -e 's|.ts|.mkv|'
done









share|improve this question















marked as duplicate by Jeff Schaller, Sparhawk, Isaac, heemayl, Mr Shunz Jan 28 at 9:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Just use rename. If the Perl version, it's just rename 's/.ts$/.mkv' *.

    – Sparhawk
    Jan 28 at 1:38











  • I want to save the renamed file to a variable to pass in another command in the script (input name -> output name). The examples are for renaming them in the directory

    – NasKar
    Jan 28 at 3:22






  • 1





    Please edit your question to include this information.

    – Sparhawk
    Jan 28 at 3:39











  • Are your files named with .ts actually Matroska files? Why are they all misnamed?

    – jamesdlin
    Jan 28 at 5:57
















2












2








2









This question already has an answer here:




  • Changing extension to multiple files

    12 answers




Rename all the files in .ts files in a directory
My echo command works but not if I try to make it a new variable.



#!/bin/sh
for file in "${1}"/*.ts; do
echo ${file} | sed -e 's|.ts|.mkv|'
new_name=${file} | sed -e 's|.ts|.mkv|'
done









share|improve this question

















This question already has an answer here:




  • Changing extension to multiple files

    12 answers




Rename all the files in .ts files in a directory
My echo command works but not if I try to make it a new variable.



#!/bin/sh
for file in "${1}"/*.ts; do
echo ${file} | sed -e 's|.ts|.mkv|'
new_name=${file} | sed -e 's|.ts|.mkv|'
done




This question already has an answer here:




  • Changing extension to multiple files

    12 answers








shell-script files rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 28 at 1:32









Jeff Schaller

40.9k1056130




40.9k1056130










asked Jan 28 at 1:24









NasKarNasKar

132




132




marked as duplicate by Jeff Schaller, Sparhawk, Isaac, heemayl, Mr Shunz Jan 28 at 9:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Jeff Schaller, Sparhawk, Isaac, heemayl, Mr Shunz Jan 28 at 9:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    Just use rename. If the Perl version, it's just rename 's/.ts$/.mkv' *.

    – Sparhawk
    Jan 28 at 1:38











  • I want to save the renamed file to a variable to pass in another command in the script (input name -> output name). The examples are for renaming them in the directory

    – NasKar
    Jan 28 at 3:22






  • 1





    Please edit your question to include this information.

    – Sparhawk
    Jan 28 at 3:39











  • Are your files named with .ts actually Matroska files? Why are they all misnamed?

    – jamesdlin
    Jan 28 at 5:57
















  • 2





    Just use rename. If the Perl version, it's just rename 's/.ts$/.mkv' *.

    – Sparhawk
    Jan 28 at 1:38











  • I want to save the renamed file to a variable to pass in another command in the script (input name -> output name). The examples are for renaming them in the directory

    – NasKar
    Jan 28 at 3:22






  • 1





    Please edit your question to include this information.

    – Sparhawk
    Jan 28 at 3:39











  • Are your files named with .ts actually Matroska files? Why are they all misnamed?

    – jamesdlin
    Jan 28 at 5:57










2




2





Just use rename. If the Perl version, it's just rename 's/.ts$/.mkv' *.

– Sparhawk
Jan 28 at 1:38





Just use rename. If the Perl version, it's just rename 's/.ts$/.mkv' *.

– Sparhawk
Jan 28 at 1:38













I want to save the renamed file to a variable to pass in another command in the script (input name -> output name). The examples are for renaming them in the directory

– NasKar
Jan 28 at 3:22





I want to save the renamed file to a variable to pass in another command in the script (input name -> output name). The examples are for renaming them in the directory

– NasKar
Jan 28 at 3:22




1




1





Please edit your question to include this information.

– Sparhawk
Jan 28 at 3:39





Please edit your question to include this information.

– Sparhawk
Jan 28 at 3:39













Are your files named with .ts actually Matroska files? Why are they all misnamed?

– jamesdlin
Jan 28 at 5:57







Are your files named with .ts actually Matroska files? Why are they all misnamed?

– jamesdlin
Jan 28 at 5:57












5 Answers
5






active

oldest

votes


















4














The simplest solution would be to tell you to change the assignment to:



new_name=$( echo ${file} | sed -e 's|.ts|.mkv|' )


But a better solution would be to do:



new_name="${file%.ts}.mkv"





share|improve this answer
























  • the 1st option works the second give file.mkv/*.mkv

    – NasKar
    Jan 29 at 3:16











  • If the loop is applied only to files in "${1}"/*.ts I don't see how a .mkv file gets in the list. @NasKar

    – Isaac
    Jan 29 at 6:28



















2














find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"


Run this in the directory containing the .ts files.






share|improve this answer































    1














    #!/bin/sh
    for file in "${1}"/*.ts; do
    mv "${file}" "$(basename "${file}").mkv"
    done





    share|improve this answer































      0














      rename is what you're looking for :



      rename '.ts' '.mkv' "${1}/*.ts"





      share|improve this answer































        0














        By using $file unquoted in your code, you invite the shell to split its contents into words based on the value of $IFS (space, tab and newline by default). Those words would then undergo filename expansion if they contained any globbing patterns.



        By using echo, you would possibly also expand backslash sequences like n and t depending on what shell options happened to be set (xpg_echo).



        Your first sed command would replace any character followed by ts in the filename with .mkv. This means that the filename bats.ts would be changed to b.mkv.ts.



        The second line in the loop is nonsensical, as it takes the output of an assignment and passes it to sed over a pipe.



        Assigning the output of a command to a variable is done with command substitutions:



        variable=$( some_command )




        #!/bin/sh

        for name in "$1"/*.ts; do
        mv -i "$name" "${name%.ts}.mkv"
        done


        This would loop over all files having a filename suffix of .ts in the directory given by "$1". Each file would be renamed to the same name, but with the .ts filename suffix removed and the .mkv suffix inserted at the end of the filename.



        The variable substitution ${variable%pattern} would remove the shortest suffix in $variable that matches pattern. This would work even if the filename happened to contain embedded newlines.



        By using mv -i we hopefully prevent the function from accidentally overwriting existing files. Another way to do that would be to do something like



        #!/bin/sh

        for name in "$1"/*.ts; do
        newname=${name%.ts}.mkv
        if [ ! -e "$newname" ]; then
        mv "$name" "$newname"
        else
        printf 'Name "%s" is already takenn' "$newname" >&2
        fi
        done


        In addition to this test, you may actually want to check whether $name exists to start with. If the pattern "$1"/.*ts does not match any filenames, then it would by default remain unexpanded. We can catch that with



        #!/bin/sh

        for name in "$1"/*.ts; do
        [ ! -e "$name" ] && break

        newname=${name%.ts}.mkv
        if [ ! -e "$newname" ]; then
        mv "$name" "$newname"
        else
        printf 'Name "%s" is already takenn' "$newname" >&2
        fi
        done


        In the bash shell, you could use shopt -s nullglob to make nonmatching patterns expand to nothing instead. In bash you may also want to use shopt -s dotglob if you want to additionally match hidden filenames.






        share|improve this answer
































          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          The simplest solution would be to tell you to change the assignment to:



          new_name=$( echo ${file} | sed -e 's|.ts|.mkv|' )


          But a better solution would be to do:



          new_name="${file%.ts}.mkv"





          share|improve this answer
























          • the 1st option works the second give file.mkv/*.mkv

            – NasKar
            Jan 29 at 3:16











          • If the loop is applied only to files in "${1}"/*.ts I don't see how a .mkv file gets in the list. @NasKar

            – Isaac
            Jan 29 at 6:28
















          4














          The simplest solution would be to tell you to change the assignment to:



          new_name=$( echo ${file} | sed -e 's|.ts|.mkv|' )


          But a better solution would be to do:



          new_name="${file%.ts}.mkv"





          share|improve this answer
























          • the 1st option works the second give file.mkv/*.mkv

            – NasKar
            Jan 29 at 3:16











          • If the loop is applied only to files in "${1}"/*.ts I don't see how a .mkv file gets in the list. @NasKar

            – Isaac
            Jan 29 at 6:28














          4












          4








          4







          The simplest solution would be to tell you to change the assignment to:



          new_name=$( echo ${file} | sed -e 's|.ts|.mkv|' )


          But a better solution would be to do:



          new_name="${file%.ts}.mkv"





          share|improve this answer













          The simplest solution would be to tell you to change the assignment to:



          new_name=$( echo ${file} | sed -e 's|.ts|.mkv|' )


          But a better solution would be to do:



          new_name="${file%.ts}.mkv"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 28 at 3:12









          IsaacIsaac

          11.8k11752




          11.8k11752













          • the 1st option works the second give file.mkv/*.mkv

            – NasKar
            Jan 29 at 3:16











          • If the loop is applied only to files in "${1}"/*.ts I don't see how a .mkv file gets in the list. @NasKar

            – Isaac
            Jan 29 at 6:28



















          • the 1st option works the second give file.mkv/*.mkv

            – NasKar
            Jan 29 at 3:16











          • If the loop is applied only to files in "${1}"/*.ts I don't see how a .mkv file gets in the list. @NasKar

            – Isaac
            Jan 29 at 6:28

















          the 1st option works the second give file.mkv/*.mkv

          – NasKar
          Jan 29 at 3:16





          the 1st option works the second give file.mkv/*.mkv

          – NasKar
          Jan 29 at 3:16













          If the loop is applied only to files in "${1}"/*.ts I don't see how a .mkv file gets in the list. @NasKar

          – Isaac
          Jan 29 at 6:28





          If the loop is applied only to files in "${1}"/*.ts I don't see how a .mkv file gets in the list. @NasKar

          – Isaac
          Jan 29 at 6:28













          2














          find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"


          Run this in the directory containing the .ts files.






          share|improve this answer




























            2














            find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"


            Run this in the directory containing the .ts files.






            share|improve this answer


























              2












              2








              2







              find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"


              Run this in the directory containing the .ts files.






              share|improve this answer













              find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"


              Run this in the directory containing the .ts files.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 28 at 5:13









              Niko GambtNiko Gambt

              1836




              1836























                  1














                  #!/bin/sh
                  for file in "${1}"/*.ts; do
                  mv "${file}" "$(basename "${file}").mkv"
                  done





                  share|improve this answer




























                    1














                    #!/bin/sh
                    for file in "${1}"/*.ts; do
                    mv "${file}" "$(basename "${file}").mkv"
                    done





                    share|improve this answer


























                      1












                      1








                      1







                      #!/bin/sh
                      for file in "${1}"/*.ts; do
                      mv "${file}" "$(basename "${file}").mkv"
                      done





                      share|improve this answer













                      #!/bin/sh
                      for file in "${1}"/*.ts; do
                      mv "${file}" "$(basename "${file}").mkv"
                      done






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 28 at 3:28









                      nyetnyet

                      13113




                      13113























                          0














                          rename is what you're looking for :



                          rename '.ts' '.mkv' "${1}/*.ts"





                          share|improve this answer




























                            0














                            rename is what you're looking for :



                            rename '.ts' '.mkv' "${1}/*.ts"





                            share|improve this answer


























                              0












                              0








                              0







                              rename is what you're looking for :



                              rename '.ts' '.mkv' "${1}/*.ts"





                              share|improve this answer













                              rename is what you're looking for :



                              rename '.ts' '.mkv' "${1}/*.ts"






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 28 at 8:24









                              breversabreversa

                              1115




                              1115























                                  0














                                  By using $file unquoted in your code, you invite the shell to split its contents into words based on the value of $IFS (space, tab and newline by default). Those words would then undergo filename expansion if they contained any globbing patterns.



                                  By using echo, you would possibly also expand backslash sequences like n and t depending on what shell options happened to be set (xpg_echo).



                                  Your first sed command would replace any character followed by ts in the filename with .mkv. This means that the filename bats.ts would be changed to b.mkv.ts.



                                  The second line in the loop is nonsensical, as it takes the output of an assignment and passes it to sed over a pipe.



                                  Assigning the output of a command to a variable is done with command substitutions:



                                  variable=$( some_command )




                                  #!/bin/sh

                                  for name in "$1"/*.ts; do
                                  mv -i "$name" "${name%.ts}.mkv"
                                  done


                                  This would loop over all files having a filename suffix of .ts in the directory given by "$1". Each file would be renamed to the same name, but with the .ts filename suffix removed and the .mkv suffix inserted at the end of the filename.



                                  The variable substitution ${variable%pattern} would remove the shortest suffix in $variable that matches pattern. This would work even if the filename happened to contain embedded newlines.



                                  By using mv -i we hopefully prevent the function from accidentally overwriting existing files. Another way to do that would be to do something like



                                  #!/bin/sh

                                  for name in "$1"/*.ts; do
                                  newname=${name%.ts}.mkv
                                  if [ ! -e "$newname" ]; then
                                  mv "$name" "$newname"
                                  else
                                  printf 'Name "%s" is already takenn' "$newname" >&2
                                  fi
                                  done


                                  In addition to this test, you may actually want to check whether $name exists to start with. If the pattern "$1"/.*ts does not match any filenames, then it would by default remain unexpanded. We can catch that with



                                  #!/bin/sh

                                  for name in "$1"/*.ts; do
                                  [ ! -e "$name" ] && break

                                  newname=${name%.ts}.mkv
                                  if [ ! -e "$newname" ]; then
                                  mv "$name" "$newname"
                                  else
                                  printf 'Name "%s" is already takenn' "$newname" >&2
                                  fi
                                  done


                                  In the bash shell, you could use shopt -s nullglob to make nonmatching patterns expand to nothing instead. In bash you may also want to use shopt -s dotglob if you want to additionally match hidden filenames.






                                  share|improve this answer






























                                    0














                                    By using $file unquoted in your code, you invite the shell to split its contents into words based on the value of $IFS (space, tab and newline by default). Those words would then undergo filename expansion if they contained any globbing patterns.



                                    By using echo, you would possibly also expand backslash sequences like n and t depending on what shell options happened to be set (xpg_echo).



                                    Your first sed command would replace any character followed by ts in the filename with .mkv. This means that the filename bats.ts would be changed to b.mkv.ts.



                                    The second line in the loop is nonsensical, as it takes the output of an assignment and passes it to sed over a pipe.



                                    Assigning the output of a command to a variable is done with command substitutions:



                                    variable=$( some_command )




                                    #!/bin/sh

                                    for name in "$1"/*.ts; do
                                    mv -i "$name" "${name%.ts}.mkv"
                                    done


                                    This would loop over all files having a filename suffix of .ts in the directory given by "$1". Each file would be renamed to the same name, but with the .ts filename suffix removed and the .mkv suffix inserted at the end of the filename.



                                    The variable substitution ${variable%pattern} would remove the shortest suffix in $variable that matches pattern. This would work even if the filename happened to contain embedded newlines.



                                    By using mv -i we hopefully prevent the function from accidentally overwriting existing files. Another way to do that would be to do something like



                                    #!/bin/sh

                                    for name in "$1"/*.ts; do
                                    newname=${name%.ts}.mkv
                                    if [ ! -e "$newname" ]; then
                                    mv "$name" "$newname"
                                    else
                                    printf 'Name "%s" is already takenn' "$newname" >&2
                                    fi
                                    done


                                    In addition to this test, you may actually want to check whether $name exists to start with. If the pattern "$1"/.*ts does not match any filenames, then it would by default remain unexpanded. We can catch that with



                                    #!/bin/sh

                                    for name in "$1"/*.ts; do
                                    [ ! -e "$name" ] && break

                                    newname=${name%.ts}.mkv
                                    if [ ! -e "$newname" ]; then
                                    mv "$name" "$newname"
                                    else
                                    printf 'Name "%s" is already takenn' "$newname" >&2
                                    fi
                                    done


                                    In the bash shell, you could use shopt -s nullglob to make nonmatching patterns expand to nothing instead. In bash you may also want to use shopt -s dotglob if you want to additionally match hidden filenames.






                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      By using $file unquoted in your code, you invite the shell to split its contents into words based on the value of $IFS (space, tab and newline by default). Those words would then undergo filename expansion if they contained any globbing patterns.



                                      By using echo, you would possibly also expand backslash sequences like n and t depending on what shell options happened to be set (xpg_echo).



                                      Your first sed command would replace any character followed by ts in the filename with .mkv. This means that the filename bats.ts would be changed to b.mkv.ts.



                                      The second line in the loop is nonsensical, as it takes the output of an assignment and passes it to sed over a pipe.



                                      Assigning the output of a command to a variable is done with command substitutions:



                                      variable=$( some_command )




                                      #!/bin/sh

                                      for name in "$1"/*.ts; do
                                      mv -i "$name" "${name%.ts}.mkv"
                                      done


                                      This would loop over all files having a filename suffix of .ts in the directory given by "$1". Each file would be renamed to the same name, but with the .ts filename suffix removed and the .mkv suffix inserted at the end of the filename.



                                      The variable substitution ${variable%pattern} would remove the shortest suffix in $variable that matches pattern. This would work even if the filename happened to contain embedded newlines.



                                      By using mv -i we hopefully prevent the function from accidentally overwriting existing files. Another way to do that would be to do something like



                                      #!/bin/sh

                                      for name in "$1"/*.ts; do
                                      newname=${name%.ts}.mkv
                                      if [ ! -e "$newname" ]; then
                                      mv "$name" "$newname"
                                      else
                                      printf 'Name "%s" is already takenn' "$newname" >&2
                                      fi
                                      done


                                      In addition to this test, you may actually want to check whether $name exists to start with. If the pattern "$1"/.*ts does not match any filenames, then it would by default remain unexpanded. We can catch that with



                                      #!/bin/sh

                                      for name in "$1"/*.ts; do
                                      [ ! -e "$name" ] && break

                                      newname=${name%.ts}.mkv
                                      if [ ! -e "$newname" ]; then
                                      mv "$name" "$newname"
                                      else
                                      printf 'Name "%s" is already takenn' "$newname" >&2
                                      fi
                                      done


                                      In the bash shell, you could use shopt -s nullglob to make nonmatching patterns expand to nothing instead. In bash you may also want to use shopt -s dotglob if you want to additionally match hidden filenames.






                                      share|improve this answer















                                      By using $file unquoted in your code, you invite the shell to split its contents into words based on the value of $IFS (space, tab and newline by default). Those words would then undergo filename expansion if they contained any globbing patterns.



                                      By using echo, you would possibly also expand backslash sequences like n and t depending on what shell options happened to be set (xpg_echo).



                                      Your first sed command would replace any character followed by ts in the filename with .mkv. This means that the filename bats.ts would be changed to b.mkv.ts.



                                      The second line in the loop is nonsensical, as it takes the output of an assignment and passes it to sed over a pipe.



                                      Assigning the output of a command to a variable is done with command substitutions:



                                      variable=$( some_command )




                                      #!/bin/sh

                                      for name in "$1"/*.ts; do
                                      mv -i "$name" "${name%.ts}.mkv"
                                      done


                                      This would loop over all files having a filename suffix of .ts in the directory given by "$1". Each file would be renamed to the same name, but with the .ts filename suffix removed and the .mkv suffix inserted at the end of the filename.



                                      The variable substitution ${variable%pattern} would remove the shortest suffix in $variable that matches pattern. This would work even if the filename happened to contain embedded newlines.



                                      By using mv -i we hopefully prevent the function from accidentally overwriting existing files. Another way to do that would be to do something like



                                      #!/bin/sh

                                      for name in "$1"/*.ts; do
                                      newname=${name%.ts}.mkv
                                      if [ ! -e "$newname" ]; then
                                      mv "$name" "$newname"
                                      else
                                      printf 'Name "%s" is already takenn' "$newname" >&2
                                      fi
                                      done


                                      In addition to this test, you may actually want to check whether $name exists to start with. If the pattern "$1"/.*ts does not match any filenames, then it would by default remain unexpanded. We can catch that with



                                      #!/bin/sh

                                      for name in "$1"/*.ts; do
                                      [ ! -e "$name" ] && break

                                      newname=${name%.ts}.mkv
                                      if [ ! -e "$newname" ]; then
                                      mv "$name" "$newname"
                                      else
                                      printf 'Name "%s" is already takenn' "$newname" >&2
                                      fi
                                      done


                                      In the bash shell, you could use shopt -s nullglob to make nonmatching patterns expand to nothing instead. In bash you may also want to use shopt -s dotglob if you want to additionally match hidden filenames.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jan 28 at 8:54

























                                      answered Jan 28 at 8:31









                                      KusalanandaKusalananda

                                      128k16241398




                                      128k16241398















                                          Popular posts from this blog

                                          How to change which sound is reproduced for terminal bell?

                                          Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents