mac OS – Creating folders based on part of a filename

前端 未结 2 373
北荒
北荒 2021-01-15 00:31

I\'m running macOS and looking for a way to quickly sort thousands of jpg files. I need to create folders based on part of filenames and then move those files into it.

2条回答
  •  失恋的感觉
    2021-01-15 00:52

    I would back up the files first to somewhere safe in case it all goes wrong. Then I would install homebrew and then install rename with:

    brew install rename
    

    Then you can do what you want with this:

    rename --dry-run -p 's|(^[^_]*)|$1/$1|' *.jpg
    

    If that looks correct, remove the --dry-run and run it again.

    Let's look at that command.

    • --dry-run means just say what the command would do without actually doing anything

    • -p means create any intermediate paths (i.e. directories) as necessary

    • 's|...|' I will explain in a moment

    • *.jpg means to run the command on all JPG files.

    The funny bit in single quotes is actually a substitution, in its simplest form it is s|a|b| which means substitute thing a with b. In this particular case, the a is caret (^) which means start of filename and then [^_]* means any number of things that are not underscores. As I have surrounded that with parentheses, I can refer back to it in the b part as $1 since it is the first thing in parentheses in a. The b part means "whatever was before the underscore" followed by a slash and "whatever was before the underscore again".

提交回复
热议问题