合并并在循环中移动文件夹

时间:2014-02-12 09:55:00

标签: bash for-loop file-io directory whitespace

我有一个大型音乐文件库,存储为./"Artist Name"/"Album Name"/"audio files"

我想重新组织到./"Artist name --- Album name"/"audio files"

能够把它恢复原状。

4 个答案:

答案 0 :(得分:0)

这是你的第一个循环

    mkdir "ArtistName-Albumname"
    cd "ArtistName/Albumname"
    for filename in *; do
         mv "$filename" "$ArtistName-Albumname/" ;;
    done

答案 1 :(得分:0)

以下是将文件夹从"Artist Name"/"Album Name"合并到"Artist name - Album name"

的脚本
#! /usr/bin/env bash

cd /PATH

find . -type f |while read -r line
do
   file=${line##*/}
   folder=${line%/*}
   album=${folder##*/}
   folder=${folder%/*}
   artist=${folder##*/}
   newfolder="$artist - $album"
   mkdir -p "$newfolder"
   echo mv "$line" "$newfolder"
done

如果你理解上面的脚本,你可以写一个反向的脚本。

答案 2 :(得分:0)

我认为这样的事情会让你开始。它没有做任何事情,只是解析你的结构并找出需要做的事情:

#!/bin/bash
find . -depth 2 -type d | while IFS= read p
do
   p=${p:2}     # Trim ./ from start
   album=${p##*/}   # album is everything after /
   artist=${p%/*}   # artist is everything before /
   newloc="${artist} - ${album}"
   echo Would move $artist/$album to ${newloc}
done

示例输出:

Would move artist1/album1 to artist1 - album1
Would move artist1/album2 to artist1 - album2
Would move artist1/album3 to artist1 - album3
Would move artist1/album4 to artist1 - album4
Would move artist2/album1 to artist2 - album1
Would move artist2/album2 to artist2 - album2
Would move artist2/album3 to artist2 - album3
Would move artist3/album1 to artist3 - album1
Would move artist3/album2 to artist3 - album2  
Would move artist3/album3 to artist3 - album3
Would move artist4/album1 to artist4 - album1
Would move artist4/album2 to artist4 - album2
Would move artist4/album3 to artist4 - album3
Would move artist4/album4 to artist4 - album4
Would move artist4/album5 to artist4 - album5

反向操作很棘手,因为专辑名称中可能会出现连字符,因此很难将其与下面代码引入的连字符区分开来。

答案 3 :(得分:0)

花了一些时间给业余爱好者。但这是我最后的解决方案。非常感谢您输入@mark @BMW。

function flatten() {
echo flattening...
    ls -ld --format=single-column */* | while IFS= read albumpath
        do
            echo Flattening "$albumpath"
            artist=${albumpath%/*}   # artist is everything before /
            echo Artist: "$artist"
            echo Album: "$albumpath"
            album=${albumpath##*/} # album is everything after  /
            newfolder="$artist --- $album"
            echo Moving "$albumpath" to "$newfolder"
            mv "$albumpath" "$newfolder"
        done
    find . -depth -type d -empty -delete #delete all empty (artist)folders

}

function unflatten() {
ls -ld --format=single-column */ | while IFS= read pathname
do

    echo REVERSING "$pathname" ;    
    artist=${pathname% ---*}   # artist is everything before " ---"
    echo Artist: "$artist"
    if [ ! -d "$artist" ]; 
        then
            echo Creating "$artist" folder 
            mkdir "$artist"
    fi

    album=${pathname##*--- }   # album is everything after "--- "   
    album=${album%/*}   # strip trailing /
    echo Album: "$album"
    echo Moving "$pathname" "$artist"/"$album"
    mv "$pathname" "$artist"/"$album"
done

}