Change extension of all file names containing specific string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












When I get a file whose name contains the string "x264", I wish to change its extension to ".mp4" (for reasons too detailed to explain here). Examples:



Seinfeld_S02E05_x264.mkv -> Seinfeld_S02E05_x264.mp4



Seinfeld_S02E05_x264.avi -> Seinfeld_S02E05_x264.mp4



What script can I use to automatically make such a change? My computer is a NAS4FREE server running FreeBSD 11.1



UPDATE (after Jeff's response): I forgot to mention that I want this to work in the designated directory and all its sub-directories as well. Perhaps a command rather than a script?







share|improve this question





















  • Do you know? with this way you are losing your file Seinfeld_S02E05_x264.mkv? In your given sample you are rename two different files name to single name and first one will be deleted completely
    – Î±Ò“sнιη
    Apr 19 at 14:34











  • I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion.
    – Jim
    Apr 20 at 2:03














up vote
0
down vote

favorite












When I get a file whose name contains the string "x264", I wish to change its extension to ".mp4" (for reasons too detailed to explain here). Examples:



Seinfeld_S02E05_x264.mkv -> Seinfeld_S02E05_x264.mp4



Seinfeld_S02E05_x264.avi -> Seinfeld_S02E05_x264.mp4



What script can I use to automatically make such a change? My computer is a NAS4FREE server running FreeBSD 11.1



UPDATE (after Jeff's response): I forgot to mention that I want this to work in the designated directory and all its sub-directories as well. Perhaps a command rather than a script?







share|improve this question





















  • Do you know? with this way you are losing your file Seinfeld_S02E05_x264.mkv? In your given sample you are rename two different files name to single name and first one will be deleted completely
    – Î±Ò“sнιη
    Apr 19 at 14:34











  • I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion.
    – Jim
    Apr 20 at 2:03












up vote
0
down vote

favorite









up vote
0
down vote

favorite











When I get a file whose name contains the string "x264", I wish to change its extension to ".mp4" (for reasons too detailed to explain here). Examples:



Seinfeld_S02E05_x264.mkv -> Seinfeld_S02E05_x264.mp4



Seinfeld_S02E05_x264.avi -> Seinfeld_S02E05_x264.mp4



What script can I use to automatically make such a change? My computer is a NAS4FREE server running FreeBSD 11.1



UPDATE (after Jeff's response): I forgot to mention that I want this to work in the designated directory and all its sub-directories as well. Perhaps a command rather than a script?







share|improve this question













When I get a file whose name contains the string "x264", I wish to change its extension to ".mp4" (for reasons too detailed to explain here). Examples:



Seinfeld_S02E05_x264.mkv -> Seinfeld_S02E05_x264.mp4



Seinfeld_S02E05_x264.avi -> Seinfeld_S02E05_x264.mp4



What script can I use to automatically make such a change? My computer is a NAS4FREE server running FreeBSD 11.1



UPDATE (after Jeff's response): I forgot to mention that I want this to work in the designated directory and all its sub-directories as well. Perhaps a command rather than a script?









share|improve this question












share|improve this question




share|improve this question








edited Apr 19 at 12:14









don_crissti

46.3k15123152




46.3k15123152









asked Apr 18 at 10:30









Jim

32




32











  • Do you know? with this way you are losing your file Seinfeld_S02E05_x264.mkv? In your given sample you are rename two different files name to single name and first one will be deleted completely
    – Î±Ò“sнιη
    Apr 19 at 14:34











  • I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion.
    – Jim
    Apr 20 at 2:03
















  • Do you know? with this way you are losing your file Seinfeld_S02E05_x264.mkv? In your given sample you are rename two different files name to single name and first one will be deleted completely
    – Î±Ò“sнιη
    Apr 19 at 14:34











  • I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion.
    – Jim
    Apr 20 at 2:03















Do you know? with this way you are losing your file Seinfeld_S02E05_x264.mkv? In your given sample you are rename two different files name to single name and first one will be deleted completely
– Î±Ò“sнιη
Apr 19 at 14:34





Do you know? with this way you are losing your file Seinfeld_S02E05_x264.mkv? In your given sample you are rename two different files name to single name and first one will be deleted completely
– Î±Ò“sнιη
Apr 19 at 14:34













I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion.
– Jim
Apr 20 at 2:03




I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion.
– Jim
Apr 20 at 2:03










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Using find:



find -type f -name "*x264*" -execdir sh -c 'echo mv -n "$1" "$1%.*.mp4"' _ ;


The -n option will prevent mv to overwrite to existing files as your given samples files' name renaming to single file name and with this case you are are deleting first file.



To get this work on files without having extensions (suffix):



find -type f -name "*x264*" -execdir sh -c 'file="$1#./"; 
echo mv -n "$file" "$file%.*.mp4"' _ ;


remove echo in above to get rid of dry-run and perform rename on files.






share|improve this answer























  • Thanks! This did the trick. I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion
    – Jim
    Apr 20 at 2:04

















up vote
2
down vote













for f in *x264*.*
do
mv -- "$f" "$f%.*".mp4
done


The above runs a shell loop over a "glob" pattern that finds files with "x264" in their filename before some sort of extension (so that there's an extension to rename later); once it has that list of files, it calls mv to do the rename, and uses parameter expansion to strip the extension (period followed by anything *) then manually appends the .mp4 extension.






share|improve this answer





















  • Note that hidden files (files whose name starts with .) are not processed.
    – Stéphane Chazelas
    Apr 18 at 12:45










  • Indeed; seems ok for the assumed scenario. dotglob would toggle this behavior, if needed.
    – Jeff Schaller
    Apr 18 at 12:47










  • This will also do it for directories having x264 and dot in it's name.
    – Bhagyesh Dudhediya
    Apr 19 at 11:10










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438476%2fchange-extension-of-all-file-names-containing-specific-string%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Using find:



find -type f -name "*x264*" -execdir sh -c 'echo mv -n "$1" "$1%.*.mp4"' _ ;


The -n option will prevent mv to overwrite to existing files as your given samples files' name renaming to single file name and with this case you are are deleting first file.



To get this work on files without having extensions (suffix):



find -type f -name "*x264*" -execdir sh -c 'file="$1#./"; 
echo mv -n "$file" "$file%.*.mp4"' _ ;


remove echo in above to get rid of dry-run and perform rename on files.






share|improve this answer























  • Thanks! This did the trick. I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion
    – Jim
    Apr 20 at 2:04














up vote
0
down vote



accepted










Using find:



find -type f -name "*x264*" -execdir sh -c 'echo mv -n "$1" "$1%.*.mp4"' _ ;


The -n option will prevent mv to overwrite to existing files as your given samples files' name renaming to single file name and with this case you are are deleting first file.



To get this work on files without having extensions (suffix):



find -type f -name "*x264*" -execdir sh -c 'file="$1#./"; 
echo mv -n "$file" "$file%.*.mp4"' _ ;


remove echo in above to get rid of dry-run and perform rename on files.






share|improve this answer























  • Thanks! This did the trick. I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion
    – Jim
    Apr 20 at 2:04












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Using find:



find -type f -name "*x264*" -execdir sh -c 'echo mv -n "$1" "$1%.*.mp4"' _ ;


The -n option will prevent mv to overwrite to existing files as your given samples files' name renaming to single file name and with this case you are are deleting first file.



To get this work on files without having extensions (suffix):



find -type f -name "*x264*" -execdir sh -c 'file="$1#./"; 
echo mv -n "$file" "$file%.*.mp4"' _ ;


remove echo in above to get rid of dry-run and perform rename on files.






share|improve this answer















Using find:



find -type f -name "*x264*" -execdir sh -c 'echo mv -n "$1" "$1%.*.mp4"' _ ;


The -n option will prevent mv to overwrite to existing files as your given samples files' name renaming to single file name and with this case you are are deleting first file.



To get this work on files without having extensions (suffix):



find -type f -name "*x264*" -execdir sh -c 'file="$1#./"; 
echo mv -n "$file" "$file%.*.mp4"' _ ;


remove echo in above to get rid of dry-run and perform rename on files.







share|improve this answer















share|improve this answer



share|improve this answer








edited Apr 20 at 10:02


























answered Apr 19 at 14:53









αғsнιη

14.8k82462




14.8k82462











  • Thanks! This did the trick. I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion
    – Jim
    Apr 20 at 2:04
















  • Thanks! This did the trick. I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion
    – Jim
    Apr 20 at 2:04















Thanks! This did the trick. I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion
– Jim
Apr 20 at 2:04




Thanks! This did the trick. I was showing the same filename twice just as an example of how I wanted the extension name changed no matter what the original extension name was. I should have given different filenames - sorry for the confusion
– Jim
Apr 20 at 2:04












up vote
2
down vote













for f in *x264*.*
do
mv -- "$f" "$f%.*".mp4
done


The above runs a shell loop over a "glob" pattern that finds files with "x264" in their filename before some sort of extension (so that there's an extension to rename later); once it has that list of files, it calls mv to do the rename, and uses parameter expansion to strip the extension (period followed by anything *) then manually appends the .mp4 extension.






share|improve this answer





















  • Note that hidden files (files whose name starts with .) are not processed.
    – Stéphane Chazelas
    Apr 18 at 12:45










  • Indeed; seems ok for the assumed scenario. dotglob would toggle this behavior, if needed.
    – Jeff Schaller
    Apr 18 at 12:47










  • This will also do it for directories having x264 and dot in it's name.
    – Bhagyesh Dudhediya
    Apr 19 at 11:10














up vote
2
down vote













for f in *x264*.*
do
mv -- "$f" "$f%.*".mp4
done


The above runs a shell loop over a "glob" pattern that finds files with "x264" in their filename before some sort of extension (so that there's an extension to rename later); once it has that list of files, it calls mv to do the rename, and uses parameter expansion to strip the extension (period followed by anything *) then manually appends the .mp4 extension.






share|improve this answer





















  • Note that hidden files (files whose name starts with .) are not processed.
    – Stéphane Chazelas
    Apr 18 at 12:45










  • Indeed; seems ok for the assumed scenario. dotglob would toggle this behavior, if needed.
    – Jeff Schaller
    Apr 18 at 12:47










  • This will also do it for directories having x264 and dot in it's name.
    – Bhagyesh Dudhediya
    Apr 19 at 11:10












up vote
2
down vote










up vote
2
down vote









for f in *x264*.*
do
mv -- "$f" "$f%.*".mp4
done


The above runs a shell loop over a "glob" pattern that finds files with "x264" in their filename before some sort of extension (so that there's an extension to rename later); once it has that list of files, it calls mv to do the rename, and uses parameter expansion to strip the extension (period followed by anything *) then manually appends the .mp4 extension.






share|improve this answer













for f in *x264*.*
do
mv -- "$f" "$f%.*".mp4
done


The above runs a shell loop over a "glob" pattern that finds files with "x264" in their filename before some sort of extension (so that there's an extension to rename later); once it has that list of files, it calls mv to do the rename, and uses parameter expansion to strip the extension (period followed by anything *) then manually appends the .mp4 extension.







share|improve this answer













share|improve this answer



share|improve this answer











answered Apr 18 at 11:03









Jeff Schaller

31.1k846105




31.1k846105











  • Note that hidden files (files whose name starts with .) are not processed.
    – Stéphane Chazelas
    Apr 18 at 12:45










  • Indeed; seems ok for the assumed scenario. dotglob would toggle this behavior, if needed.
    – Jeff Schaller
    Apr 18 at 12:47










  • This will also do it for directories having x264 and dot in it's name.
    – Bhagyesh Dudhediya
    Apr 19 at 11:10
















  • Note that hidden files (files whose name starts with .) are not processed.
    – Stéphane Chazelas
    Apr 18 at 12:45










  • Indeed; seems ok for the assumed scenario. dotglob would toggle this behavior, if needed.
    – Jeff Schaller
    Apr 18 at 12:47










  • This will also do it for directories having x264 and dot in it's name.
    – Bhagyesh Dudhediya
    Apr 19 at 11:10















Note that hidden files (files whose name starts with .) are not processed.
– Stéphane Chazelas
Apr 18 at 12:45




Note that hidden files (files whose name starts with .) are not processed.
– Stéphane Chazelas
Apr 18 at 12:45












Indeed; seems ok for the assumed scenario. dotglob would toggle this behavior, if needed.
– Jeff Schaller
Apr 18 at 12:47




Indeed; seems ok for the assumed scenario. dotglob would toggle this behavior, if needed.
– Jeff Schaller
Apr 18 at 12:47












This will also do it for directories having x264 and dot in it's name.
– Bhagyesh Dudhediya
Apr 19 at 11:10




This will also do it for directories having x264 and dot in it's name.
– Bhagyesh Dudhediya
Apr 19 at 11:10












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438476%2fchange-extension-of-all-file-names-containing-specific-string%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?