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

Bash 在循环中合并和移动文件夹,bash,for-loop,file-io,directory,whitespace,Bash,For Loop,File Io,Directory,Whitespace,我有一个很大的音乐文件库,存储为/“艺术家名”/“唱片集名”/“音频文件” 我想重新组织到/“艺人姓名-专辑名称”/“音频文件” 并能够将其恢复到原来的状态。这是您的第一个循环 mkdir "ArtistName-Albumname" cd "ArtistName/Albumname" for filename in *; do mv "$filename" "$ArtistName-Albumname/" ;; done 下面是将文件夹从“艺

我有一个很大的音乐文件库,存储为
/“艺术家名”/“唱片集名”/“音频文件”

我想重新组织到
/“艺人姓名-专辑名称”/“音频文件”


并能够将其恢复到原来的状态。

这是您的第一个循环

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

下面是将文件夹从
“艺术家名称”/“唱片集名称”
合并到
“艺术家名称-唱片集名称”


如果您理解上面的脚本,您可以编写一个相反的脚本

我想这样的事情会让你开始。它什么都不做,只是解析您的结构并确定需要做什么:

#!/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

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

业余爱好者花了一些时间。但这是我最后的解决办法。非常感谢您对@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

}

我试过了
你试过什么?你能发布你的进度吗?谢谢!我可以用一些从未出现过的东西来代替连字符。无论如何,我是唯一一个看到它的人。但执行此操作时出现错误“find:path必须在表达式:2之前”。有什么线索吗?为了调试,在第一行末尾添加“-xv”。还有,你在单词“find”后复制了点吗?点在那里,是的。对不起,我不知道如何在这里维护换行符
code
#/bin/bash-xv查找-深度2-类型d |而IFS=read p do p=${p:2}{Trim./from start album=${p##*/}album是/artist=${p%/*}之后的一切/newloc=“${artist}-${album}”echo将$artist/$album移到${newloc done+find-深度2-类型d+IFS=+read p find:路径必须在表达式之前:2用法:find[-H][L][p][Olevel][d help | tree | search | stat | rates | opt | exec][path…][expression]
code
Mmm。。奇怪的我不知道你的“发现”是怎么回事。你在哪个站台?您使用的是/bin/find,还是路径中还有其他内容?尝试键入“whichfind”。另外,请尝试在脚本中提供/bin/find的完整路径。谢谢!这一个为每个音频文件执行mkdir。最好只对每个相册进行迭代,重命名和移动整个文件夹,忽略其中的内容。
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