short cut date as file name

hey guys,

i am looking for a short cut, that renames a file by its creation date. I was thinking about copiing the date from the file information and put it into the files name.

I have no idea how to do that. is there anybody with some expertise?

thank you!!

Posted on Jan 10, 2023 11:47 PM

Reply
4 replies

Jan 17, 2023 09:09 AM in response to Vico63

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


Jan 11, 2023 10:40 AM in response to Vico63

Here's a zsh example script that creates a couple of temporary files in the current directory (might want to start in an empty directory when testing this) and then loops through all files with the specified type and renames those files to the same filename, adding a ~sortable date format acquired by stat'ing the file, reusing the original file type, and appending the string renamed onto the file type. Double quotes are liberally used throughout to avoid issues with embedded spaces in filenames. The shell script invocation, and resulting filenames are then shown. You will need to chmod o+x the script once (not shown) to allow the script to run, as per usual zsh.


$ 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"}
$./tmp.sh
$ ls
tmp.sh				tmp1 20230111132154.tmp renamed	tmp2 20230111132154.tmp renamed
$

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

short cut date as file name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.