Renaming files by extracting parts of filenames that match with a pattern
Clash Royale CLAN TAG#URR8PPP
I want to know which command I can use to rename files like this.
Let's say, for instance, the old filenames are:
0_predict-1-500.png
0_predict-2-500.png
0_predict-3-500.png
1_predict-1-500.png
1_predict-2-500.png
1_predict-3-500.png
2_predict-1-500.png
...so on...
What I am expecting is to extract them with the format like filename_predict-times-rounds.png
, then I can replace it into another pattern, for example, filename.png-result-times.png
.
So, the aforementioned filenames will be changed to match the pattern:
0.png-result-1.png
0.png-result-2.png
0.png-result-3.png
1.png-result-1.png
1.png-result-2.png
1.png-result-3.png
2.png-result-1.png
...so on...
Does anyone has the Linux command/tool to do thing like this?
I don't wanna write the Python script to complete this.
linux filenames rename
add a comment |
I want to know which command I can use to rename files like this.
Let's say, for instance, the old filenames are:
0_predict-1-500.png
0_predict-2-500.png
0_predict-3-500.png
1_predict-1-500.png
1_predict-2-500.png
1_predict-3-500.png
2_predict-1-500.png
...so on...
What I am expecting is to extract them with the format like filename_predict-times-rounds.png
, then I can replace it into another pattern, for example, filename.png-result-times.png
.
So, the aforementioned filenames will be changed to match the pattern:
0.png-result-1.png
0.png-result-2.png
0.png-result-3.png
1.png-result-1.png
1.png-result-2.png
1.png-result-3.png
2.png-result-1.png
...so on...
Does anyone has the Linux command/tool to do thing like this?
I don't wanna write the Python script to complete this.
linux filenames rename
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:33
add a comment |
I want to know which command I can use to rename files like this.
Let's say, for instance, the old filenames are:
0_predict-1-500.png
0_predict-2-500.png
0_predict-3-500.png
1_predict-1-500.png
1_predict-2-500.png
1_predict-3-500.png
2_predict-1-500.png
...so on...
What I am expecting is to extract them with the format like filename_predict-times-rounds.png
, then I can replace it into another pattern, for example, filename.png-result-times.png
.
So, the aforementioned filenames will be changed to match the pattern:
0.png-result-1.png
0.png-result-2.png
0.png-result-3.png
1.png-result-1.png
1.png-result-2.png
1.png-result-3.png
2.png-result-1.png
...so on...
Does anyone has the Linux command/tool to do thing like this?
I don't wanna write the Python script to complete this.
linux filenames rename
I want to know which command I can use to rename files like this.
Let's say, for instance, the old filenames are:
0_predict-1-500.png
0_predict-2-500.png
0_predict-3-500.png
1_predict-1-500.png
1_predict-2-500.png
1_predict-3-500.png
2_predict-1-500.png
...so on...
What I am expecting is to extract them with the format like filename_predict-times-rounds.png
, then I can replace it into another pattern, for example, filename.png-result-times.png
.
So, the aforementioned filenames will be changed to match the pattern:
0.png-result-1.png
0.png-result-2.png
0.png-result-3.png
1.png-result-1.png
1.png-result-2.png
1.png-result-3.png
2.png-result-1.png
...so on...
Does anyone has the Linux command/tool to do thing like this?
I don't wanna write the Python script to complete this.
linux filenames rename
linux filenames rename
edited Dec 28 '18 at 11:22
Jeff Schaller
39.2k1054125
39.2k1054125
asked Dec 28 '18 at 10:07
WatchananWatchanan
112
112
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:33
add a comment |
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:33
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:33
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:33
add a comment |
5 Answers
5
active
oldest
votes
I have just found out that there is a tool that can easily manipulate filenames in GNU/Linux which is mmv. It is quite much easier than the posted answers.
In my case,
mmv "*_predict-*-*.png" "#1.png-result-#2.png"
It tries to substitute each part with a wildcard pattern, consequently, we can re-use the part with #1
, #2
, and so on.
add a comment |
With the help of rename command you can edit it, I am assuming that all files are in same directory and you want to rename all .png files.:
find -name *.png -exec sh -c 'x=$( rename -v 's/_predict/.png-result/g' $0 ) ; rename 's/-500//g' "$x##*./" ;' ;
Run this command in that directory where files are.
So basically it runs two times rename command first one to remove _predict
and second one to remove -500
.
Here I used x=$( rename -v 's/_predict/.png-result/g' $0 )
because after first change in the file name, I stored this new name in variable x
and then used this value for second modification.
Command rename 's/-500//g' "$x##*./" ;
is used because value of x is like ./0_predict-1-500.png renamed as ./0.png-result-1-500.png
so I just want 0.png-result-1-500.png
and $x##*./
will do that.
add a comment |
Try also
$ for FN in *.png; do IFS="_-." read F P T R X <<< "$FN"; echo mv "$FN" "$F.$X-result-$T.$X"; done
mv 0_predict-1-500.png 0.png-result-1.png
mv 0_predict-2-500.png 0.png-result-2.png
mv 0_predict-3-500.png 0.png-result-3.png
mv 1_predict-1-500.png 1.png-result-1.png
mv 1_predict-2-500.png 1.png-result-2.png
mv 1_predict-3-500.png 1.png-result-3.png
mv 2_predict-1-500.png 2.png-result-1.png
and remove the echo
if happy with the result. We're looping through the target files, read
the file name components using an adapted IFS
variable and a "here string", and then reassemble the components to form the desired final file name for the mv
command.
add a comment |
To add yet another variation, this uses the bash shell's =~
pattern matching operator in the [[
test command to pick out the elements to be rearranged:
for f in ./*_predict-*-*.png
do
[[ $f =~ ^./([[:digit:]]+)_predict-([[:digit:]]+)-([[:digit:]]+).png ]]
echo mv -- "$f" "$BASH_REMATCH[1].png-result-$BASH_REMATCH[2].png"
done
Remove the echo
if the results look correct.
add a comment |
Mention all the files in a file which needs to be renamed
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename |sh
I have mentioned -v for verbose mode
This is how it shows output for renaming
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename|sh
`0_predict-1-500.png' -> `0.png-result-1.png'
`0_predict-2-500.png' -> `0.png-result-2.png'
`0_predict-3-500.png' -> `0.png-result-3.png'
`1_predict-1-500.png' -> `1.png-result-1.png'
`1_predict-2-500.png' -> `1.png-result-2.png'
`1_predict-3-500.png' -> `1.png-result-3.png'
`2_predict-1-500.png' -> `2.png-result-1.png'
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f491273%2frenaming-files-by-extracting-parts-of-filenames-that-match-with-a-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have just found out that there is a tool that can easily manipulate filenames in GNU/Linux which is mmv. It is quite much easier than the posted answers.
In my case,
mmv "*_predict-*-*.png" "#1.png-result-#2.png"
It tries to substitute each part with a wildcard pattern, consequently, we can re-use the part with #1
, #2
, and so on.
add a comment |
I have just found out that there is a tool that can easily manipulate filenames in GNU/Linux which is mmv. It is quite much easier than the posted answers.
In my case,
mmv "*_predict-*-*.png" "#1.png-result-#2.png"
It tries to substitute each part with a wildcard pattern, consequently, we can re-use the part with #1
, #2
, and so on.
add a comment |
I have just found out that there is a tool that can easily manipulate filenames in GNU/Linux which is mmv. It is quite much easier than the posted answers.
In my case,
mmv "*_predict-*-*.png" "#1.png-result-#2.png"
It tries to substitute each part with a wildcard pattern, consequently, we can re-use the part with #1
, #2
, and so on.
I have just found out that there is a tool that can easily manipulate filenames in GNU/Linux which is mmv. It is quite much easier than the posted answers.
In my case,
mmv "*_predict-*-*.png" "#1.png-result-#2.png"
It tries to substitute each part with a wildcard pattern, consequently, we can re-use the part with #1
, #2
, and so on.
edited Dec 28 '18 at 16:02
P_Yadav
1,5213923
1,5213923
answered Dec 28 '18 at 13:59
WatchananWatchanan
112
112
add a comment |
add a comment |
With the help of rename command you can edit it, I am assuming that all files are in same directory and you want to rename all .png files.:
find -name *.png -exec sh -c 'x=$( rename -v 's/_predict/.png-result/g' $0 ) ; rename 's/-500//g' "$x##*./" ;' ;
Run this command in that directory where files are.
So basically it runs two times rename command first one to remove _predict
and second one to remove -500
.
Here I used x=$( rename -v 's/_predict/.png-result/g' $0 )
because after first change in the file name, I stored this new name in variable x
and then used this value for second modification.
Command rename 's/-500//g' "$x##*./" ;
is used because value of x is like ./0_predict-1-500.png renamed as ./0.png-result-1-500.png
so I just want 0.png-result-1-500.png
and $x##*./
will do that.
add a comment |
With the help of rename command you can edit it, I am assuming that all files are in same directory and you want to rename all .png files.:
find -name *.png -exec sh -c 'x=$( rename -v 's/_predict/.png-result/g' $0 ) ; rename 's/-500//g' "$x##*./" ;' ;
Run this command in that directory where files are.
So basically it runs two times rename command first one to remove _predict
and second one to remove -500
.
Here I used x=$( rename -v 's/_predict/.png-result/g' $0 )
because after first change in the file name, I stored this new name in variable x
and then used this value for second modification.
Command rename 's/-500//g' "$x##*./" ;
is used because value of x is like ./0_predict-1-500.png renamed as ./0.png-result-1-500.png
so I just want 0.png-result-1-500.png
and $x##*./
will do that.
add a comment |
With the help of rename command you can edit it, I am assuming that all files are in same directory and you want to rename all .png files.:
find -name *.png -exec sh -c 'x=$( rename -v 's/_predict/.png-result/g' $0 ) ; rename 's/-500//g' "$x##*./" ;' ;
Run this command in that directory where files are.
So basically it runs two times rename command first one to remove _predict
and second one to remove -500
.
Here I used x=$( rename -v 's/_predict/.png-result/g' $0 )
because after first change in the file name, I stored this new name in variable x
and then used this value for second modification.
Command rename 's/-500//g' "$x##*./" ;
is used because value of x is like ./0_predict-1-500.png renamed as ./0.png-result-1-500.png
so I just want 0.png-result-1-500.png
and $x##*./
will do that.
With the help of rename command you can edit it, I am assuming that all files are in same directory and you want to rename all .png files.:
find -name *.png -exec sh -c 'x=$( rename -v 's/_predict/.png-result/g' $0 ) ; rename 's/-500//g' "$x##*./" ;' ;
Run this command in that directory where files are.
So basically it runs two times rename command first one to remove _predict
and second one to remove -500
.
Here I used x=$( rename -v 's/_predict/.png-result/g' $0 )
because after first change in the file name, I stored this new name in variable x
and then used this value for second modification.
Command rename 's/-500//g' "$x##*./" ;
is used because value of x is like ./0_predict-1-500.png renamed as ./0.png-result-1-500.png
so I just want 0.png-result-1-500.png
and $x##*./
will do that.
edited Dec 28 '18 at 12:05
answered Dec 28 '18 at 11:38
P_YadavP_Yadav
1,5213923
1,5213923
add a comment |
add a comment |
Try also
$ for FN in *.png; do IFS="_-." read F P T R X <<< "$FN"; echo mv "$FN" "$F.$X-result-$T.$X"; done
mv 0_predict-1-500.png 0.png-result-1.png
mv 0_predict-2-500.png 0.png-result-2.png
mv 0_predict-3-500.png 0.png-result-3.png
mv 1_predict-1-500.png 1.png-result-1.png
mv 1_predict-2-500.png 1.png-result-2.png
mv 1_predict-3-500.png 1.png-result-3.png
mv 2_predict-1-500.png 2.png-result-1.png
and remove the echo
if happy with the result. We're looping through the target files, read
the file name components using an adapted IFS
variable and a "here string", and then reassemble the components to form the desired final file name for the mv
command.
add a comment |
Try also
$ for FN in *.png; do IFS="_-." read F P T R X <<< "$FN"; echo mv "$FN" "$F.$X-result-$T.$X"; done
mv 0_predict-1-500.png 0.png-result-1.png
mv 0_predict-2-500.png 0.png-result-2.png
mv 0_predict-3-500.png 0.png-result-3.png
mv 1_predict-1-500.png 1.png-result-1.png
mv 1_predict-2-500.png 1.png-result-2.png
mv 1_predict-3-500.png 1.png-result-3.png
mv 2_predict-1-500.png 2.png-result-1.png
and remove the echo
if happy with the result. We're looping through the target files, read
the file name components using an adapted IFS
variable and a "here string", and then reassemble the components to form the desired final file name for the mv
command.
add a comment |
Try also
$ for FN in *.png; do IFS="_-." read F P T R X <<< "$FN"; echo mv "$FN" "$F.$X-result-$T.$X"; done
mv 0_predict-1-500.png 0.png-result-1.png
mv 0_predict-2-500.png 0.png-result-2.png
mv 0_predict-3-500.png 0.png-result-3.png
mv 1_predict-1-500.png 1.png-result-1.png
mv 1_predict-2-500.png 1.png-result-2.png
mv 1_predict-3-500.png 1.png-result-3.png
mv 2_predict-1-500.png 2.png-result-1.png
and remove the echo
if happy with the result. We're looping through the target files, read
the file name components using an adapted IFS
variable and a "here string", and then reassemble the components to form the desired final file name for the mv
command.
Try also
$ for FN in *.png; do IFS="_-." read F P T R X <<< "$FN"; echo mv "$FN" "$F.$X-result-$T.$X"; done
mv 0_predict-1-500.png 0.png-result-1.png
mv 0_predict-2-500.png 0.png-result-2.png
mv 0_predict-3-500.png 0.png-result-3.png
mv 1_predict-1-500.png 1.png-result-1.png
mv 1_predict-2-500.png 1.png-result-2.png
mv 1_predict-3-500.png 1.png-result-3.png
mv 2_predict-1-500.png 2.png-result-1.png
and remove the echo
if happy with the result. We're looping through the target files, read
the file name components using an adapted IFS
variable and a "here string", and then reassemble the components to form the desired final file name for the mv
command.
answered Dec 28 '18 at 13:49
RudiCRudiC
4,2191312
4,2191312
add a comment |
add a comment |
To add yet another variation, this uses the bash shell's =~
pattern matching operator in the [[
test command to pick out the elements to be rearranged:
for f in ./*_predict-*-*.png
do
[[ $f =~ ^./([[:digit:]]+)_predict-([[:digit:]]+)-([[:digit:]]+).png ]]
echo mv -- "$f" "$BASH_REMATCH[1].png-result-$BASH_REMATCH[2].png"
done
Remove the echo
if the results look correct.
add a comment |
To add yet another variation, this uses the bash shell's =~
pattern matching operator in the [[
test command to pick out the elements to be rearranged:
for f in ./*_predict-*-*.png
do
[[ $f =~ ^./([[:digit:]]+)_predict-([[:digit:]]+)-([[:digit:]]+).png ]]
echo mv -- "$f" "$BASH_REMATCH[1].png-result-$BASH_REMATCH[2].png"
done
Remove the echo
if the results look correct.
add a comment |
To add yet another variation, this uses the bash shell's =~
pattern matching operator in the [[
test command to pick out the elements to be rearranged:
for f in ./*_predict-*-*.png
do
[[ $f =~ ^./([[:digit:]]+)_predict-([[:digit:]]+)-([[:digit:]]+).png ]]
echo mv -- "$f" "$BASH_REMATCH[1].png-result-$BASH_REMATCH[2].png"
done
Remove the echo
if the results look correct.
To add yet another variation, this uses the bash shell's =~
pattern matching operator in the [[
test command to pick out the elements to be rearranged:
for f in ./*_predict-*-*.png
do
[[ $f =~ ^./([[:digit:]]+)_predict-([[:digit:]]+)-([[:digit:]]+).png ]]
echo mv -- "$f" "$BASH_REMATCH[1].png-result-$BASH_REMATCH[2].png"
done
Remove the echo
if the results look correct.
answered Dec 28 '18 at 15:42
Jeff SchallerJeff Schaller
39.2k1054125
39.2k1054125
add a comment |
add a comment |
Mention all the files in a file which needs to be renamed
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename |sh
I have mentioned -v for verbose mode
This is how it shows output for renaming
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename|sh
`0_predict-1-500.png' -> `0.png-result-1.png'
`0_predict-2-500.png' -> `0.png-result-2.png'
`0_predict-3-500.png' -> `0.png-result-3.png'
`1_predict-1-500.png' -> `1.png-result-1.png'
`1_predict-2-500.png' -> `1.png-result-2.png'
`1_predict-3-500.png' -> `1.png-result-3.png'
`2_predict-1-500.png' -> `2.png-result-1.png'
add a comment |
Mention all the files in a file which needs to be renamed
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename |sh
I have mentioned -v for verbose mode
This is how it shows output for renaming
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename|sh
`0_predict-1-500.png' -> `0.png-result-1.png'
`0_predict-2-500.png' -> `0.png-result-2.png'
`0_predict-3-500.png' -> `0.png-result-3.png'
`1_predict-1-500.png' -> `1.png-result-1.png'
`1_predict-2-500.png' -> `1.png-result-2.png'
`1_predict-3-500.png' -> `1.png-result-3.png'
`2_predict-1-500.png' -> `2.png-result-1.png'
add a comment |
Mention all the files in a file which needs to be renamed
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename |sh
I have mentioned -v for verbose mode
This is how it shows output for renaming
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename|sh
`0_predict-1-500.png' -> `0.png-result-1.png'
`0_predict-2-500.png' -> `0.png-result-2.png'
`0_predict-3-500.png' -> `0.png-result-3.png'
`1_predict-1-500.png' -> `1.png-result-1.png'
`1_predict-2-500.png' -> `1.png-result-2.png'
`1_predict-3-500.png' -> `1.png-result-3.png'
`2_predict-1-500.png' -> `2.png-result-1.png'
Mention all the files in a file which needs to be renamed
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename |sh
I have mentioned -v for verbose mode
This is how it shows output for renaming
awk 'print "mv -v" " " $1 " " substr($1,1,1)".png-result-"substr($1,11,1)".png"' filename|sh
`0_predict-1-500.png' -> `0.png-result-1.png'
`0_predict-2-500.png' -> `0.png-result-2.png'
`0_predict-3-500.png' -> `0.png-result-3.png'
`1_predict-1-500.png' -> `1.png-result-1.png'
`1_predict-2-500.png' -> `1.png-result-2.png'
`1_predict-3-500.png' -> `1.png-result-3.png'
`2_predict-1-500.png' -> `2.png-result-1.png'
answered Dec 28 '18 at 16:39
Praveen Kumar BSPraveen Kumar BS
1,248138
1,248138
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f491273%2frenaming-files-by-extracting-parts-of-filenames-that-match-with-a-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:33