Convert year-month-day to weekday in file name
Clash 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...
rename filenames date
add a comment |
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...
rename filenames date
Use this as reference: stackoverflow.com/questions/12787530/…
– Romeo Ninov
Oct 4 '16 at 12:43
add a comment |
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...
rename filenames date
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
rename filenames date
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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%2f314214%2fconvert-year-month-day-to-weekday-in-file-name%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
Use this as reference: stackoverflow.com/questions/12787530/…
– Romeo Ninov
Oct 4 '16 at 12:43