Convert year-month-day to weekday in file name

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











up vote
3
down vote

favorite












I need to take the following filename structure and rename to the appropriate day of the week:



GMT20161003-randomtext.mp4


would end up as monday.mp4



I have a lot of these files in various subdirectories so it would be better if it could be run recursively...










share|improve this question























  • Use this as reference: stackoverflow.com/questions/12787530/…
    – Romeo Ninov
    Oct 4 '16 at 12:43














up vote
3
down vote

favorite












I need to take the following filename structure and rename to the appropriate day of the week:



GMT20161003-randomtext.mp4


would end up as monday.mp4



I have a lot of these files in various subdirectories so it would be better if it could be run recursively...










share|improve this question























  • Use this as reference: stackoverflow.com/questions/12787530/…
    – Romeo Ninov
    Oct 4 '16 at 12:43












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I need to take the following filename structure and rename to the appropriate day of the week:



GMT20161003-randomtext.mp4


would end up as monday.mp4



I have a lot of these files in various subdirectories so it would be better if it could be run recursively...










share|improve this question















I need to take the following filename structure and rename to the appropriate day of the week:



GMT20161003-randomtext.mp4


would end up as monday.mp4



I have a lot of these files in various subdirectories so it would be better if it could be run recursively...







rename filenames date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:34









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Oct 4 '16 at 12:41









user1399145

232




232











  • Use this as reference: stackoverflow.com/questions/12787530/…
    – Romeo Ninov
    Oct 4 '16 at 12:43
















  • Use this as reference: stackoverflow.com/questions/12787530/…
    – Romeo Ninov
    Oct 4 '16 at 12:43















Use this as reference: stackoverflow.com/questions/12787530/…
– Romeo Ninov
Oct 4 '16 at 12:43




Use this as reference: stackoverflow.com/questions/12787530/…
– Romeo Ninov
Oct 4 '16 at 12:43










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










With zsh:



zmodload zsh/datetime
autoload zmv
zmv -n '(**/)GMT(<->)*(.mp4)(#qD.)' '$1$(L)$(
strftime %A "$(strftime -r %Y%m%d $2)")$3'


Remove the -n to actually do the renaming.




  • <-> matches any decimal number.

  • That second (...) is captured in $2, (.mp4) in $3 and the directory ((**/), recursive) in $1.


  • (#qD.) is a glob qualifier that only selects regular files (.: not directories, nor symlinks nor fifos/devices...) and also traverse hidden directories (D for dotfile/dotdir).


  • $(L)...: converts the expansion to lower-case.


  • strftime -r %Y%m%d: reverse-strftime (strptime) to convert the date to an epoch time.


  • strftime %A ...: format time for that epoch time with %A being for the full week day. Beware it's locale-dependany. (in a French locale, you'll get the French week day).

On a GNU system, and with the GNU shell (bash), you could do:



find . -name 'GMT*-*.mp4' -type f -exec bash -c '
for file do
base=$file##*/
date=$base#GMT
date=$date%%-*
wday=$(date -d "$date" +%A)
echo mv -i "$file" "$file%/*/$wday,,.mp4"
done' bash +


(remove echo to perform the operation).



$var,, being bash's operator to convert to lower case. date -d being the GNU date way to parse a date (like strftime -r above).



While zmv would check for conflicts before starting renaming any file, this one wouldn't. So we add a -i above to at least give you a chance of avoiding clobbering files. GNU mv has a -v option to tell it to show what it's going to do which may be useful to revert the command later if anything went wrong.






share|improve this answer






















  • Thank you so much. I will check these out and then report back!
    – user1399145
    Oct 4 '16 at 14:31










  • You are my hero today - used the second solution in a .sh script - just tried on one file in one folder and it worked perfectly. Thank you so much again!!
    – user1399145
    Oct 4 '16 at 14:46










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: 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
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f314214%2fconvert-year-month-day-to-weekday-in-file-name%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










With zsh:



zmodload zsh/datetime
autoload zmv
zmv -n '(**/)GMT(<->)*(.mp4)(#qD.)' '$1$(L)$(
strftime %A "$(strftime -r %Y%m%d $2)")$3'


Remove the -n to actually do the renaming.




  • <-> matches any decimal number.

  • That second (...) is captured in $2, (.mp4) in $3 and the directory ((**/), recursive) in $1.


  • (#qD.) is a glob qualifier that only selects regular files (.: not directories, nor symlinks nor fifos/devices...) and also traverse hidden directories (D for dotfile/dotdir).


  • $(L)...: converts the expansion to lower-case.


  • strftime -r %Y%m%d: reverse-strftime (strptime) to convert the date to an epoch time.


  • strftime %A ...: format time for that epoch time with %A being for the full week day. Beware it's locale-dependany. (in a French locale, you'll get the French week day).

On a GNU system, and with the GNU shell (bash), you could do:



find . -name 'GMT*-*.mp4' -type f -exec bash -c '
for file do
base=$file##*/
date=$base#GMT
date=$date%%-*
wday=$(date -d "$date" +%A)
echo mv -i "$file" "$file%/*/$wday,,.mp4"
done' bash +


(remove echo to perform the operation).



$var,, being bash's operator to convert to lower case. date -d being the GNU date way to parse a date (like strftime -r above).



While zmv would check for conflicts before starting renaming any file, this one wouldn't. So we add a -i above to at least give you a chance of avoiding clobbering files. GNU mv has a -v option to tell it to show what it's going to do which may be useful to revert the command later if anything went wrong.






share|improve this answer






















  • Thank you so much. I will check these out and then report back!
    – user1399145
    Oct 4 '16 at 14:31










  • You are my hero today - used the second solution in a .sh script - just tried on one file in one folder and it worked perfectly. Thank you so much again!!
    – user1399145
    Oct 4 '16 at 14:46














up vote
2
down vote



accepted










With zsh:



zmodload zsh/datetime
autoload zmv
zmv -n '(**/)GMT(<->)*(.mp4)(#qD.)' '$1$(L)$(
strftime %A "$(strftime -r %Y%m%d $2)")$3'


Remove the -n to actually do the renaming.




  • <-> matches any decimal number.

  • That second (...) is captured in $2, (.mp4) in $3 and the directory ((**/), recursive) in $1.


  • (#qD.) is a glob qualifier that only selects regular files (.: not directories, nor symlinks nor fifos/devices...) and also traverse hidden directories (D for dotfile/dotdir).


  • $(L)...: converts the expansion to lower-case.


  • strftime -r %Y%m%d: reverse-strftime (strptime) to convert the date to an epoch time.


  • strftime %A ...: format time for that epoch time with %A being for the full week day. Beware it's locale-dependany. (in a French locale, you'll get the French week day).

On a GNU system, and with the GNU shell (bash), you could do:



find . -name 'GMT*-*.mp4' -type f -exec bash -c '
for file do
base=$file##*/
date=$base#GMT
date=$date%%-*
wday=$(date -d "$date" +%A)
echo mv -i "$file" "$file%/*/$wday,,.mp4"
done' bash +


(remove echo to perform the operation).



$var,, being bash's operator to convert to lower case. date -d being the GNU date way to parse a date (like strftime -r above).



While zmv would check for conflicts before starting renaming any file, this one wouldn't. So we add a -i above to at least give you a chance of avoiding clobbering files. GNU mv has a -v option to tell it to show what it's going to do which may be useful to revert the command later if anything went wrong.






share|improve this answer






















  • Thank you so much. I will check these out and then report back!
    – user1399145
    Oct 4 '16 at 14:31










  • You are my hero today - used the second solution in a .sh script - just tried on one file in one folder and it worked perfectly. Thank you so much again!!
    – user1399145
    Oct 4 '16 at 14:46












up vote
2
down vote



accepted







up vote
2
down vote



accepted






With zsh:



zmodload zsh/datetime
autoload zmv
zmv -n '(**/)GMT(<->)*(.mp4)(#qD.)' '$1$(L)$(
strftime %A "$(strftime -r %Y%m%d $2)")$3'


Remove the -n to actually do the renaming.




  • <-> matches any decimal number.

  • That second (...) is captured in $2, (.mp4) in $3 and the directory ((**/), recursive) in $1.


  • (#qD.) is a glob qualifier that only selects regular files (.: not directories, nor symlinks nor fifos/devices...) and also traverse hidden directories (D for dotfile/dotdir).


  • $(L)...: converts the expansion to lower-case.


  • strftime -r %Y%m%d: reverse-strftime (strptime) to convert the date to an epoch time.


  • strftime %A ...: format time for that epoch time with %A being for the full week day. Beware it's locale-dependany. (in a French locale, you'll get the French week day).

On a GNU system, and with the GNU shell (bash), you could do:



find . -name 'GMT*-*.mp4' -type f -exec bash -c '
for file do
base=$file##*/
date=$base#GMT
date=$date%%-*
wday=$(date -d "$date" +%A)
echo mv -i "$file" "$file%/*/$wday,,.mp4"
done' bash +


(remove echo to perform the operation).



$var,, being bash's operator to convert to lower case. date -d being the GNU date way to parse a date (like strftime -r above).



While zmv would check for conflicts before starting renaming any file, this one wouldn't. So we add a -i above to at least give you a chance of avoiding clobbering files. GNU mv has a -v option to tell it to show what it's going to do which may be useful to revert the command later if anything went wrong.






share|improve this answer














With zsh:



zmodload zsh/datetime
autoload zmv
zmv -n '(**/)GMT(<->)*(.mp4)(#qD.)' '$1$(L)$(
strftime %A "$(strftime -r %Y%m%d $2)")$3'


Remove the -n to actually do the renaming.




  • <-> matches any decimal number.

  • That second (...) is captured in $2, (.mp4) in $3 and the directory ((**/), recursive) in $1.


  • (#qD.) is a glob qualifier that only selects regular files (.: not directories, nor symlinks nor fifos/devices...) and also traverse hidden directories (D for dotfile/dotdir).


  • $(L)...: converts the expansion to lower-case.


  • strftime -r %Y%m%d: reverse-strftime (strptime) to convert the date to an epoch time.


  • strftime %A ...: format time for that epoch time with %A being for the full week day. Beware it's locale-dependany. (in a French locale, you'll get the French week day).

On a GNU system, and with the GNU shell (bash), you could do:



find . -name 'GMT*-*.mp4' -type f -exec bash -c '
for file do
base=$file##*/
date=$base#GMT
date=$date%%-*
wday=$(date -d "$date" +%A)
echo mv -i "$file" "$file%/*/$wday,,.mp4"
done' bash +


(remove echo to perform the operation).



$var,, being bash's operator to convert to lower case. date -d being the GNU date way to parse a date (like strftime -r above).



While zmv would check for conflicts before starting renaming any file, this one wouldn't. So we add a -i above to at least give you a chance of avoiding clobbering files. GNU mv has a -v option to tell it to show what it's going to do which may be useful to revert the command later if anything went wrong.







share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 4 '16 at 14:14

























answered Oct 4 '16 at 13:14









Stéphane Chazelas

294k54555898




294k54555898











  • Thank you so much. I will check these out and then report back!
    – user1399145
    Oct 4 '16 at 14:31










  • You are my hero today - used the second solution in a .sh script - just tried on one file in one folder and it worked perfectly. Thank you so much again!!
    – user1399145
    Oct 4 '16 at 14:46
















  • Thank you so much. I will check these out and then report back!
    – user1399145
    Oct 4 '16 at 14:31










  • You are my hero today - used the second solution in a .sh script - just tried on one file in one folder and it worked perfectly. Thank you so much again!!
    – user1399145
    Oct 4 '16 at 14:46















Thank you so much. I will check these out and then report back!
– user1399145
Oct 4 '16 at 14:31




Thank you so much. I will check these out and then report back!
– user1399145
Oct 4 '16 at 14:31












You are my hero today - used the second solution in a .sh script - just tried on one file in one folder and it worked perfectly. Thank you so much again!!
– user1399145
Oct 4 '16 at 14:46




You are my hero today - used the second solution in a .sh script - just tried on one file in one folder and it worked perfectly. Thank you so much again!!
– user1399145
Oct 4 '16 at 14:46

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f314214%2fconvert-year-month-day-to-weekday-in-file-name%23new-answer', 'question_page');

);

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






Popular posts from this blog

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

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay