2016-07-26 47 views
1

我得到了我的文件夹中,我想订购的文件列表:订购文件通过标签

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

'tags = $(echo $ file | grep -Eo“^ [^ - ] *”)'? – Aaron

+0

我认为你应该澄清,如果你的字面意思是排序单词'One','Two','Three'(如果你打算转到100,人们不会免费编码;-) ..或者如果你真正的意思是“主题区标签”,如“Rock”,“90s”,“Asian”。 ?祝你好运。 – shellter

+0

@shellter我的意思是'TagOne','TagTwo','TagThree' ..这些真正的标签显然不是真正的标签。我把它们分成了*快*,*慢*,*声*,*动作*等。 – jeyemgfx

回答

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