The first one is the original name and the second one is the new name. So we'll do the filepath slash and then in curly brackets we'll put the two different names. All rename is is moving a file from one place to another with a new name, right. You can use the move command to also rename files. Then we're going to do a simple mv command which is move. Then you do colon t and then file path is colon h. We're also going to get the path to file and the file name itself.You do that by doing file name is $f, that the f from here looping through all of the files that has passed in. It's important not to have spaces around the equals there. But it's going to set that to a variable. It's going to do, it's going to loop, and then it's going do exactly what we just did in Terminal. It's going to loop through everything that's into it. So then you have a for loop that can loop through the arguments which are represented by the dollar sign and the at symbol there. It's very important to change that because otherwise the default is standard in. So in this I'm going to set the shell script to Pass input as arguments. This will probably work in either zsh or bash. I'm going to use zsh, z shell, because that's the default now in Catalina. You can set the shell to what you want by default and maybe bash. Now you end up with an area that you can write the shell script in. You can see here if you search for it and drag it over. I've set it to receive files or folders in the Finder. What I've done in Automator is I've created a new Quick Action Automator document. Now we need to write a shell script that will actually put that as the name of the file. That is what we want to put at the beginning of each file. Now I'm going to do the same command but afterwards I'm going to put this at the end +% capital Y, for year, a dash % lower case m for the month dash % d for the date, lower case there. We can tell the date command to do a little more formatting to get exactly what we want. It's still not quite what we want it to be as a file name. Tuesday, Dec 17, the time and the time zone and the year. So it's going to basically going to take the results of this stat command and then put it through date dash r which will return a nice looking date. Date dash r, then we'll put in parentheses what we were doing before with a dollar sign there. Something I could read so I can use the Date command to format that. It is the number of seconds since January 1, 1970. When I hit Return you can see I get the creation date. So this is the name of one of the files here. stat dash f, for file, and then %B and then the name of the file. Now to get the date created in the Terminal you can use this stat command. Drag the folder there and then hit Return. If you're not in that folder you can just do CD for change directory. So first here I am in Terminal and I'm in the same folder. What I'm going to do is put that in Automator so I can have it as a service and easily apply it. So now I can the created a date for all these files. Then I can see one of the options is to see Date Created. Here are the contents and the first thing is I want to use the date created. Then you can easily apply it to one or many files. Then the easiest was to access it is to put the shell script inside of Automator in a Quick Action. So what if you wanted to rename a file adding the date of the file before the existing file name or if these are photos using the date that the photo was taken stored in the metadata for the file. MacMost is brought to you thanks to a great group of supporters.
#Automator mac move files to new dated folder how to#
Today I want to show you how to write a Shell Script for Automator that will rename files using the date of the file or photo. Check out Add Dates To File Or Photo Names With an Automator Script at YouTube for closed captioning and more options.