Script to change the timestamp of files according to filename
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
I'm in a macOS environment.
I'd like to change the time stamp on multiple files that has names that starts with this format:
XXXXX_DD_MM_YYYY_HH-MM-SS_
This is an example of a filename:
CALLU_16-10-2018_10-58-26_p123456789012.mp3
Creation and modification date should be change to 16/10/2018 at 10:58:26.
I had the following script for filenames of the form Call@1234567890(1234567890)_20160624205913
. It extracts date and time from the end of the file name, as a parameter for touch -t STAMP
:
for f in *; do
t="$(awk '/_.*/ match($0, /_.*/)
print substr($0, RSTART + 1, RLENGTH - 7)
'<<<"$f" )"
touch -t "$t" "$f" 2>/dev/null
done
scripting osx timestamps
New contributor
add a comment |
up vote
-1
down vote
favorite
I'm in a macOS environment.
I'd like to change the time stamp on multiple files that has names that starts with this format:
XXXXX_DD_MM_YYYY_HH-MM-SS_
This is an example of a filename:
CALLU_16-10-2018_10-58-26_p123456789012.mp3
Creation and modification date should be change to 16/10/2018 at 10:58:26.
I had the following script for filenames of the form Call@1234567890(1234567890)_20160624205913
. It extracts date and time from the end of the file name, as a parameter for touch -t STAMP
:
for f in *; do
t="$(awk '/_.*/ match($0, /_.*/)
print substr($0, RSTART + 1, RLENGTH - 7)
'<<<"$f" )"
touch -t "$t" "$f" 2>/dev/null
done
scripting osx timestamps
New contributor
I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
– Giancarlo Passaglia
yesterday
find . -iname 'XXXX*' | xargs touch
– user1133275
yesterday
Try removing the2>/dev/null
to see a useful error message, possibly including the output of theawk
step.
– JigglyNaga
11 hours ago
this should work, provided that theXXXXX
doesn't contain-
or_
:xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3
. I don't have any MacOS to test on, though.
– mosvy
11 hours ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm in a macOS environment.
I'd like to change the time stamp on multiple files that has names that starts with this format:
XXXXX_DD_MM_YYYY_HH-MM-SS_
This is an example of a filename:
CALLU_16-10-2018_10-58-26_p123456789012.mp3
Creation and modification date should be change to 16/10/2018 at 10:58:26.
I had the following script for filenames of the form Call@1234567890(1234567890)_20160624205913
. It extracts date and time from the end of the file name, as a parameter for touch -t STAMP
:
for f in *; do
t="$(awk '/_.*/ match($0, /_.*/)
print substr($0, RSTART + 1, RLENGTH - 7)
'<<<"$f" )"
touch -t "$t" "$f" 2>/dev/null
done
scripting osx timestamps
New contributor
I'm in a macOS environment.
I'd like to change the time stamp on multiple files that has names that starts with this format:
XXXXX_DD_MM_YYYY_HH-MM-SS_
This is an example of a filename:
CALLU_16-10-2018_10-58-26_p123456789012.mp3
Creation and modification date should be change to 16/10/2018 at 10:58:26.
I had the following script for filenames of the form Call@1234567890(1234567890)_20160624205913
. It extracts date and time from the end of the file name, as a parameter for touch -t STAMP
:
for f in *; do
t="$(awk '/_.*/ match($0, /_.*/)
print substr($0, RSTART + 1, RLENGTH - 7)
'<<<"$f" )"
touch -t "$t" "$f" 2>/dev/null
done
scripting osx timestamps
scripting osx timestamps
New contributor
New contributor
edited 11 hours ago
JigglyNaga
3,424728
3,424728
New contributor
asked yesterday
Giancarlo Passaglia
11
11
New contributor
New contributor
I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
– Giancarlo Passaglia
yesterday
find . -iname 'XXXX*' | xargs touch
– user1133275
yesterday
Try removing the2>/dev/null
to see a useful error message, possibly including the output of theawk
step.
– JigglyNaga
11 hours ago
this should work, provided that theXXXXX
doesn't contain-
or_
:xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3
. I don't have any MacOS to test on, though.
– mosvy
11 hours ago
add a comment |
I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
– Giancarlo Passaglia
yesterday
find . -iname 'XXXX*' | xargs touch
– user1133275
yesterday
Try removing the2>/dev/null
to see a useful error message, possibly including the output of theawk
step.
– JigglyNaga
11 hours ago
this should work, provided that theXXXXX
doesn't contain-
or_
:xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3
. I don't have any MacOS to test on, though.
– mosvy
11 hours ago
I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
– Giancarlo Passaglia
yesterday
I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
– Giancarlo Passaglia
yesterday
find . -iname 'XXXX*' | xargs touch
– user1133275
yesterday
find . -iname 'XXXX*' | xargs touch
– user1133275
yesterday
Try removing the
2>/dev/null
to see a useful error message, possibly including the output of the awk
step.– JigglyNaga
11 hours ago
Try removing the
2>/dev/null
to see a useful error message, possibly including the output of the awk
step.– JigglyNaga
11 hours ago
this should work, provided that the
XXXXX
doesn't contain -
or _
: xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3
. I don't have any MacOS to test on, though.– mosvy
11 hours ago
this should work, provided that the
XXXXX
doesn't contain -
or _
: xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3
. I don't have any MacOS to test on, though.– mosvy
11 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
In your filename, the date is in little-endian format, so your awk
script will need to swap the components, as well as removing all the separators:
awk -F '[-_]' 'print $4$3$2$5$6"."$7'
(If your input filenames contain additional -
characters before the date, this will need rewriting.)
Test this with your example file:
$ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
201810161058.26
So your updated script could look something like:
for f in *.mp3; do
t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
touch -t "$t" "$f"
done
Your old awk
script was searching for the first _
in the filename, and then using everything from the next character (RSTART+1
) to 6 characters before the end (RLENGTH-7
: +1-7 = -6).
On your input filename, that would produce 20160624
. touch
expects the timestamp to be in the form:
[[CC]YY]MMDDhhmm[.SS]
...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch
reporting an error because of the 2>/dev/null
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
In your filename, the date is in little-endian format, so your awk
script will need to swap the components, as well as removing all the separators:
awk -F '[-_]' 'print $4$3$2$5$6"."$7'
(If your input filenames contain additional -
characters before the date, this will need rewriting.)
Test this with your example file:
$ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
201810161058.26
So your updated script could look something like:
for f in *.mp3; do
t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
touch -t "$t" "$f"
done
Your old awk
script was searching for the first _
in the filename, and then using everything from the next character (RSTART+1
) to 6 characters before the end (RLENGTH-7
: +1-7 = -6).
On your input filename, that would produce 20160624
. touch
expects the timestamp to be in the form:
[[CC]YY]MMDDhhmm[.SS]
...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch
reporting an error because of the 2>/dev/null
.
add a comment |
up vote
0
down vote
In your filename, the date is in little-endian format, so your awk
script will need to swap the components, as well as removing all the separators:
awk -F '[-_]' 'print $4$3$2$5$6"."$7'
(If your input filenames contain additional -
characters before the date, this will need rewriting.)
Test this with your example file:
$ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
201810161058.26
So your updated script could look something like:
for f in *.mp3; do
t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
touch -t "$t" "$f"
done
Your old awk
script was searching for the first _
in the filename, and then using everything from the next character (RSTART+1
) to 6 characters before the end (RLENGTH-7
: +1-7 = -6).
On your input filename, that would produce 20160624
. touch
expects the timestamp to be in the form:
[[CC]YY]MMDDhhmm[.SS]
...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch
reporting an error because of the 2>/dev/null
.
add a comment |
up vote
0
down vote
up vote
0
down vote
In your filename, the date is in little-endian format, so your awk
script will need to swap the components, as well as removing all the separators:
awk -F '[-_]' 'print $4$3$2$5$6"."$7'
(If your input filenames contain additional -
characters before the date, this will need rewriting.)
Test this with your example file:
$ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
201810161058.26
So your updated script could look something like:
for f in *.mp3; do
t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
touch -t "$t" "$f"
done
Your old awk
script was searching for the first _
in the filename, and then using everything from the next character (RSTART+1
) to 6 characters before the end (RLENGTH-7
: +1-7 = -6).
On your input filename, that would produce 20160624
. touch
expects the timestamp to be in the form:
[[CC]YY]MMDDhhmm[.SS]
...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch
reporting an error because of the 2>/dev/null
.
In your filename, the date is in little-endian format, so your awk
script will need to swap the components, as well as removing all the separators:
awk -F '[-_]' 'print $4$3$2$5$6"."$7'
(If your input filenames contain additional -
characters before the date, this will need rewriting.)
Test this with your example file:
$ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
201810161058.26
So your updated script could look something like:
for f in *.mp3; do
t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
touch -t "$t" "$f"
done
Your old awk
script was searching for the first _
in the filename, and then using everything from the next character (RSTART+1
) to 6 characters before the end (RLENGTH-7
: +1-7 = -6).
On your input filename, that would produce 20160624
. touch
expects the timestamp to be in the form:
[[CC]YY]MMDDhhmm[.SS]
...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch
reporting an error because of the 2>/dev/null
.
answered 8 hours ago
JigglyNaga
3,424728
3,424728
add a comment |
add a comment |
Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.
Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.
Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.
Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481538%2fscript-to-change-the-timestamp-of-files-according-to-filename%23new-answer', 'question_page');
);
Post as a guest
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
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
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
I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
– Giancarlo Passaglia
yesterday
find . -iname 'XXXX*' | xargs touch
– user1133275
yesterday
Try removing the
2>/dev/null
to see a useful error message, possibly including the output of theawk
step.– JigglyNaga
11 hours ago
this should work, provided that the
XXXXX
doesn't contain-
or_
:xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3
. I don't have any MacOS to test on, though.– mosvy
11 hours ago