Vico63 wrote:
thank you therefore.
Still I dont know what zsh is? Sorry!
Where to paste this? I have never worked directly with anything else than the short cut app.
I only know the prefixed commands there :O
zsh is the default command line shell on recent macOS versions, though folks with older logins will have the bash command line shell as the default. zsh is very capable, either directly for file operations here, or via invoking osascript calls if GUI access is required.
What I posted is a copy of part of a Terminal session showing a cat command showing the script contents, an invocation of the named script, and an ls command to show the results.
Details follow:
Show the script contents:
$ cat ./tmp.sh
#!/bin/zsh
touch tmp1.tmp
touch tmp2.tmp
for fn (*.tmp) {dt="$(stat -t %Y%m%d%H%M%S -f %Sc -- "${fn}")" && mv -- "${fn}" "${fn:r} ${dt}.${fn:e} renamed"}
The first line of the script is a shebang that tells macOS the script is a zsh script, two touch commands to create temporary files for the purposes of demonstration, then a loop through matching files (those with the extension .tmp) renaming them. The for loop shown uses the stat command to fetch the date, and the mv command to rename, and steps through all files matching the target (each pass through has the matching filename named fn).
Enable running the script (this chmod command is needed only once, the first time before invoking the script), and then invoke the script:
$ chmod u+x tmp.sh # needed once
$./tmp.sh
Show the resulting (renamed) files. What what was once named tmp1.tmp and tmp2.tmp became:
$ ls
tmp.sh tmp1 20230111132154.tmp renamed tmp2 20230111132154.tmp renamed
$
The ls command output might look a little confusing as shown (I should have selected one-file-per-line output for this example), as it shows both the script file itself residing in the local (otherwise empty) directory, and the two (renamed) temporary files also fit on the same output line. So the output for ls shows three files.
To use this script, use pico tmp.sh or use BBEdit (free version) and type in or paste in the script contents shown, then view and invoke it as shown in the command example above. pico is a text editor. BBEdit is powerful a GUI text editor with a free version and a full version, and it can create plain-text files needed here. There are other editors here, including vim and emacs, but that’s a little further along. pico and BBEdit will get you going.
Apple docs:
Intro: Intro to shell scripts in Terminal on Mac - Apple Support
Details: Shell Scripting Primer