1

i have about hundreds of movie files stored each in a folder. the scheme i named each folder is like this:

ABCD

for movies with titles starting with A,B,C or D for example. In this folder are those movie folders like this

Blade Runner 2049 (2017) 0526 20180223Eph

0526 is a specific number for this file/folder.

I'd like to rename all folders to this scheme:

(0526) Blade Runner 2049 (2017) 20180223Eph

but i don't know how to do this on my mac?! please note, that some folders don't have the suffix like 20180223Eph.

It should be done for all movies in one turn - renaming all by using finder would be a little to long...

Thanks in advance,

mnasdaq

sorting of my movies

  • Is the "specific number" always 4 digits? – user3439894 Feb 28 '18 at 16:19
  • yes - from 0001 to - potentially 9999. – m nasdaq Feb 28 '18 at 16:21
1

First off, I have to say that the example bash code used in this answer, shown further below, is by no means robust, however it does get the job done!

With the following as a representative sample of the hierarchical structure of directories:

.
├── ABCD
├── EFGH
├── IJKL
├── MNOP
│   ├── A Most Violent Year (2014) 1180 20150807Lov
│   ├── A Most Wanted Man (2014) 1146
│   ├── A Nightmare on Elm Street (2010) 0432
│   ├── A Perfect Getaway (2009) 0433
│   └── ¡NO! (2012) 0939
├── QRST
├── UVWX
└── YZ

Then after running the example bash code saved as a bash script, and run from within the directory containing the hierarchical structure show above, the results for the directories will be as shown below, and being as the stated desired output based on the representative sample of the hierarchical structure of directories shown above:

.
├── ABCD
├── EFGH
├── IJKL
├── MNOP
│   ├── (0432) A Nightmare on Elm Street (2010)
│   ├── (0433) A Perfect Geatway (2009)
│   ├── (0939) ¡NO! (2012)
│   ├── (1146) A Most Wanted Man (2014)
│   └── (1180) A Most Violent Year (2014) 20150807Lov
├── QRST
├── UVWX
└── YZ

To use the example bash code, In Terminal cd to the directory containing the ABCD, EFGH, etc. directories and run the following commands:

touch rendir; nano rendir

In nano copy and paste the example bash code, show below, into the rendir file and then press Control+X and then Y and then Enter.

Next:

chmod u+x rendir

Now that it's executable, run it by typing:

./rendir

Then press Enter to run it.

Example bash code:

#!/bin/bash

find -E . -type d -regex '.* [0-9]{4}$' > /tmp/foo

while read -r l; do
    p="$(dirname "$l")"
    d="$(basename "$l")"
    n="$(awk '{print "("$NF")"}'<<<"$d")"
    f="$(awk '{$NF=""; sub(/ $/, "", $0); print $0}'<<<"$d")"
    echo mv "$l" "$p/$n $f"
done</tmp/foo

find -E . -type d -regex '.* [0-9]{4} .*$' > /tmp/foo

while read -r l; do
    p="$(dirname "$l")"
    d="$(basename "$l")"
    n="$(awk '{print "("$(NF - 1)")"}'<<<"$d")"
    f="$(awk '{$(NF - 1)=""; print $0}'<<<"$d" | awk 'sub(/  /," ")')"
    echo mv "$l" "$p/$n $f"
done</tmp/foo

rm /tmp/foo

Note that as coded, it will output the mv commands that would be executed if echo wasn't there. After testing and examining the output to see that the mv commands are properly formed, you can the edit rendir (nano rendir) and remove the two occurrences of echo, saving the file, again using (Control+X and then Y and then Enter) and executing it again.

At this point you should have the desired output as shown in the second tree hierarchical structure of directories shown above. This example bash code does not account for any deviation from what was expressed in the OP and comments to the OP.


NOTE: You should never run code without first being properly backed up and or testing the code on a sample copy of the originals.


If I knew awk better I'd imagine that an awk program could be written to manipulate the input string to the desired output string in a single calling of awk, but I don't know awk well enough to achieve that.

Note: The example bash code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .