按文件名

时间:2016-07-26 15:07:13

标签: linux bash file directory-structure

我的文件夹中有一个我想订购的文件列表:

Rock, World - SongTitle - Interpret.mp3
Rock, Acoustic, SingerSongwriter - SongTitle2 - Interpret.mp3
Rock, Acoustic - SongTitle3.mp3
SingerSongwriter, World - SongTitle4.mp3

结果应如下所示:

storage/
- Rock, World - SongTitle - Interpret.mp3
- Rock, Acoustic, SingerSongwriter - SongTitle2 - Interpret.mp3
- Rock, Acoustic - SongTitle3.mp3
- SingerSongwriter, World - SongTitle4.mp3
tags/
- Rock/
  - LINK TO: storage/Rock, World - SongTitle - Interpret.mp3
  - LINK TO: storage/Rock, Acoustic, SingerSongwriter - SongTitle2 - Interpret.mp3
  - LINK TO: storage/Rock, Acoustic - SongTitle3.mp3
- World/
  - LINK TO: storage/Rock, World - SongTitle - Interpret.mp3
  - LINK TO: storage/SingerSongwriter, World - SongTitle4.mp3
- Acoustic/
  - LINK TO: storage/Rock, Acoustic, SingerSongwriter - SongTitle2 - Interpret.mp3
  - LINK TO: storage/Rock, Acoustic - SongTitle3.mp3
- SingerSongwriter/
  - LINK TO: storage/SingerSongwriter, World - SongTitle4.mp3
  - LINK TO: storage/SingerSongwriter, World - SongTitle4.mp3

我概述了应该为我处理的脚本:

#!/bin/bash

mkdir -p tags;
mkdir -p storage;

for file in *; do
        #Grab Tags from the file name
        tags=$(echo $file | sed 's/ - /\n/g'); # Doesn't work as it should

        #                                                               #
        #  This is just blind, can't say if it works, but should(TM)..  #
        #                                                               #

        #Move file to storage folder
        mv $file storage/$file;

        #Foreach Tag:
        while read -r tag; do
                #Create tag folder if it doesn't exist yet.
                mkdir -p tags/$tag;
                #Create Symbolic Link
                ln -s storage/$file tags/$tag/$file;
        done <<< $tags;
done

问题:我如何调整脚本以使其有效?我在bash脚本中有点恩惠所以请不要怪我..

1 个答案:

答案 0 :(得分:1)

在解决之后,这是我对该主题的回答。

#!/bin/bash

mkdir -p tags;
mkdir -p storage;

for file in *; do

        #Grab Tags from the file name
        tags=$(echo $file | grep -Eo "^[^-]*" | sed -e 's/, /\n/g');

        #                                                               #
        #  This is just blind, can't say if it works, but should(TM)..  #
        #                                                               #

        #Move file to storage folder
        mv "$file" "storage/$file";

        #Foreach Tag:
        while read -r tag; do
                #Create tag folder if it doesn't exist yet.
                mkdir -p tags/$tag;
                #Create Symbolic Link
                ln -s "storage/$file" "tags/$tag/$file";
        done <<< "$tags";
done