I'd take a little different approach, in that I'd want to test that the filename ends in .mp3
and the length of the t
variable is 12 characters long before processing. This should keel it from acting on files that do not conform to the pattern and avoid throwing errors, even though the errors in this case should be non-fatal. I just don't like unnecessary junk outputted.
This is an example of it in script form:
#!/bin/bash
for f in *.mp3; do
t="$(awk '/_.*/ { match($0, /_.*/); print substr($0, RSTART + 1, RLENGTH - 7); }'<<<"$f")"
if [[ ${#t} -eq 12 ]]; then
touch -t "$t" "$f"
fi
done
This is an example of the script as a one-liner, albeit a long one.
for f in *.mp3; do t="$(awk '/_.*/ { match($0, /_.*/); print substr($0, RSTART + 1, RLENGTH - 7); }'<<<"$f")"; if [[ ${#t} -eq 12 ]]; then touch -t "$t" "$f"; fi; done
Which can be shortened to:
for f in *.mp3; do t="$(awk '/_.*/ { match($0, /_.*/); print substr($0, RSTART + 1, RLENGTH - 7); }'<<<"$f")"; [[ ${#t} -eq 12 ]] && touch -t "$t" "$f"; done
If you really want to shorten it further:
for f in *; do t="$(awk '/_.*/ { match($0, /_.*/); print substr($0, RSTART + 1, RLENGTH - 7); }'<<<"$f")"; touch -t "$t" "$f" 2>/dev/null; done
The above form of the compound command does no error checking and redirects the error output of the touch
command so it doesn't clutter the screen. This form is fine in this use case as a malformed touch commands will be non-fatal.
aaaa@XXXXXX(XXXXXXX)
, other then@
,(
and)
area...
andX...
alpha characters only or alphanumeric characters? Additionally, I saw your now deleted question you wrongfully posted as a answer to another question and are you wanting to do the same thing, that is,touch
the files to change the date-time stamp? (Just want to be sure that's what you're wanting.)