I want to rename all files in a directory from *.ts to *.mkv [duplicate]
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
shell-script files rename
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.
add a comment |
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
shell-script files rename
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 userename
. If the Perl version, it's justrename '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
add a comment |
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
shell-script files rename
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
shell-script files rename
edited Jan 28 at 1:32
Jeff Schaller
40.9k1056131
40.9k1056131
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 userename
. If the Perl version, it's justrename '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
add a comment |
2
Just userename
. If the Perl version, it's justrename '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
add a comment |
5 Answers
5
active
oldest
votes
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"
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
add a comment |
find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"
Run this in the directory containing the .ts
files.
add a comment |
#!/bin/sh
for file in "${1}"/*.ts; do
mv "${file}" "$(basename "${file}").mkv"
done
add a comment |
rename
is what you're looking for :
rename '.ts' '.mkv' "${1}/*.ts"
add a comment |
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.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"
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
add a comment |
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"
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
add a comment |
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"
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"
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
add a comment |
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
add a comment |
find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"
Run this in the directory containing the .ts
files.
add a comment |
find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"
Run this in the directory containing the .ts
files.
add a comment |
find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"
Run this in the directory containing the .ts
files.
find . -maxdepth 1 -type f -name '*.ts' -exec rename .ts .mkv {} "+"
Run this in the directory containing the .ts
files.
answered Jan 28 at 5:13
Niko GambtNiko Gambt
1836
1836
add a comment |
add a comment |
#!/bin/sh
for file in "${1}"/*.ts; do
mv "${file}" "$(basename "${file}").mkv"
done
add a comment |
#!/bin/sh
for file in "${1}"/*.ts; do
mv "${file}" "$(basename "${file}").mkv"
done
add a comment |
#!/bin/sh
for file in "${1}"/*.ts; do
mv "${file}" "$(basename "${file}").mkv"
done
#!/bin/sh
for file in "${1}"/*.ts; do
mv "${file}" "$(basename "${file}").mkv"
done
answered Jan 28 at 3:28
nyetnyet
13113
13113
add a comment |
add a comment |
rename
is what you're looking for :
rename '.ts' '.mkv' "${1}/*.ts"
add a comment |
rename
is what you're looking for :
rename '.ts' '.mkv' "${1}/*.ts"
add a comment |
rename
is what you're looking for :
rename '.ts' '.mkv' "${1}/*.ts"
rename
is what you're looking for :
rename '.ts' '.mkv' "${1}/*.ts"
answered Jan 28 at 8:24
breversabreversa
1115
1115
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 28 at 8:54
answered Jan 28 at 8:31
KusalanandaKusalananda
128k16241399
128k16241399
add a comment |
add a comment |
2
Just use
rename
. If the Perl version, it's justrename '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