2

Looking to quickly arrange a whole load (talking about multiple thousands) of files into subfolders, and the files come in sets of three (left audio, right audio, video) separate files. Not experienced in terminal really but messed around in Automator before.

Example:

00015_A01.mxf
00015_A02.mxf
00015.mxf
00016_A01.mxf
00016_A02.mxf
00016.mxf

I want to automate it so that Finder looks for files with matching first characters, and puts them in a sub folder (preferably with the name of that file but I’ve managed a workaround for that.)

So the result would be:

[00015] (folder)
00015_A01.mxf
00015_A02.mxf
00015.mxf

[00016] (folder)
00016_A01.mxf
00016_A02.mxf
00016.mxf

Any ideas? I checked around on here but the answers were a bit over my head...

2

The following script will take care to construct new directories as needed and will only move new files as they appear within a given directory, into their respective subdirectories, ie. 0015, 0016, etc.

$ cat mover.sh
#!/bin/bash

find . -type f -name "*.mxf" -depth 1 -exec sh -c '
    file="${1/\.\//}"
    echo "$file"
    dir=${file/[_.]*mxf/}
    [ -d "$dir" ] || mkdir -p $dir
    mv $file $dir/.
' find-sh {} \;

With the above in a file called mover.sh and this file is made executable via the command chmod +x mover.sh we can then run it like this, ./mover.sh.

Example

Here we have the following directory where some subdirectories already exist and contain their respective files.

$ tree .
.
├── 0015.mxf
├── 0015_A01.mxf
├── 0015_A02.mxf
├── 0016.mxf
├── 0016_A01.mxf
├── 0016_A02.mxf
├── 0017
│   ├── 0017.mxf
│   ├── 0017_A01.mxf
│   └── 0017_A02.mxf
├── 0018
│   ├── 0018.mxf
│   ├── 0018_A01.mxf
│   └── 0018_A02.mxf
└── mover.sh

Now we'd cd ~/somedir where the *.mxf files needing to get organized are located and run the ./mover.sh script.

$ ./mover.sh
0015_A02.mxf
0015_A01.mxf
0016_A01.mxf
0016_A02.mxf
0015.mxf
0016.mxf

So our script has identified the above files and confirmed that their subdirectory exists and our script will move (mv) these *.mxf to their respective subdirectories. Here we can see the state of the directory where the *.mxf files resides after running the script:

$ tree
.
├── 0015
│   ├── 0015.mxf
│   ├── 0015_A01.mxf
│   └── 0015_A02.mxf
├── 0016
│   ├── 0016.mxf
│   ├── 0016_A01.mxf
│   └── 0016_A02.mxf
├── 0017
│   ├── 0017.mxf
│   ├── 0017_A01.mxf
│   └── 0017_A02.mxf
├── 0018
│   ├── 0018.mxf
│   ├── 0018_A01.mxf
│   └── 0018_A02.mxf
└── mover.sh

4 directories, 13 files

How does it work

The script makes use of a UNIX tool called find to produce a list of files that match the pattern *.mxf. This list is then iterated through and a series of commands are run on each filename found:

  • file="${1/\.\//}" - takes each filename and removes a ./ that is prefixed to each name by find
  • echo "$file" - display the results of this command to the screen
  • dir=${file/[_.]*mxf/} - remove any trailing characters that match the patterns _*.mxf or *.mxf
  • [ -d "$dir" ] || mkdir -p $dir - check if the directory the files goes into exists, if it doesn't make it
  • mv $file $dir/. - move this file into the corresponding directory

Running this periodically

One idea here would be to setup a cronjob that runs periodically on macOS to run this script. You can read more about how to accomplish this here in this tutorial titled: Schedule jobs with crontab on Mac OS X.

The basic steps here are as follows:

  • Create a crontab entry

    $ env EDITOR=nano crontab -e
    
  • Add a entry specifying what script to run and when (every 5 mins)

    0/5 * * * *  cd ~/my/mxf/folder && ./mover.sh
    
  • Confirm your changes

    $ crontab -l
    
  • NOTE: The tree utility does not exist in a default install of macOS! – user3439894 Apr 13 at 13:31
  • Awesome. I see you are -exec an immediate command. What is "find-sh" about? – historystamp Apr 13 at 19:43
  • 1
    Thank you so much for such a detailed answer! Blown away but the rapid and in depth help in this community. You all have saved me multiple days of work with your answers. Thanks again! – BenH Apr 14 at 1:24
  • @user3439894 - good point, tree is available via brew install tree if you have brew available. – slm Apr 14 at 1:26
  • @historystamp - you can read a bit about the methods around find on the U&L site - unix.stackexchange.com/questions/93324/…. Basically the find-sh is a noop place holder to the $0 argument, since I'm using $@ inside, so we use find-sh as a nonce to make things easier to read. – slm Apr 14 at 1:32
2
for i in {10..99}
do
    mkdir 000$i
    mv 000$i* 000$i
done

for i in {100..999}
do
    mkdir 00$i
    mv 00$i* 00$i
done

for i in {1000..9999}
do
    mkdir 0$i
    mv 0$i* 0$i
done

This bash script should work for 10-9999 directories.

  • That does not take much understanding - short & sweet... – Solar Mike Apr 12 at 4:28
  • Or go the full way by running mkdir 000{10..99} first, followed by a loop to move the files :-) – nohillside Apr 12 at 7:00
  • Thank you for your answer! So how would I set this to run on a batch of files in the finder? Do I navigate to the containing folder in finder then use this command? Is there a way to use this as an action in Automator? Thank you again. – BenH Apr 12 at 8:44
  • you need to use the command line via the Terminal app. its located in the Applications/Utilities folder. Double click on the terminal app. Go to the proper folder, and just run the script. edit a file named yourscript.sh, type in the script, and run it. To run it type chmod +x yourscript.sh. Then just type ./yourscript.sh at the prompt. – jmh Apr 12 at 15:03
  • i've editted my answer to help... – jmh Apr 12 at 15:12
0

As of Mac OS 10.14.4 I have not been able to find such function but you can hold command (windows key if using a windows keyboard) then click with two fingers on trackpad or left click and say "New Folder with Selection".

  • Thanks for your input, I’ve been using this method for now, but going through thousands of files three at a time is taking a very long time... appreciate the reply though! – BenH Apr 12 at 9:35

You must log in to answer this question.

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