【问题标题】:Merging and moving folders in loop循环合并和移动文件夹
【发布时间】:2014-03-10 13:38:40
【问题描述】:

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

我想重新整理到./"Artist name --- Album name"/"audio files"

并且能够恢复原状。

【问题讨论】:

  • i've tried 那么你尝试了什么?你能发布你的进度吗?

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


【解决方案1】:

这是你的第一个循环

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

【讨论】:

    【解决方案2】:

    我认为这样的事情会让你开始。它什么也不做,只是解析你的结构并计算出需要做什么:

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

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

    【讨论】:

    • 谢谢!我可以使用从未出现过的东西来代替连字符。反正我是唯一一个看到的。但是执行此操作时出现错误“查找:路径必须在表达式之前:2”。有什么线索吗?
    • 为了调试,在第一行末尾添加“-xv”。另外,您是否复制了“find”一词后的点?
    • 点在那里,是的。
    • 对不起,这里不知道如何维护换行符code#!/bin/bash -xv find .深度 2 型 d | while IFS= read p do p=${p:2} # Trim ./ from start album=${p##*/} # 专辑是之后的一切 / 艺术家=${p%/*} # 艺术家是之前的一切/newloc="${artist} - ${album}" echo 将 $artist/$album 移动到 ${newloc} done + find 。 -depth 2 -type d + IFS= + read p find:路径必须在表达式之前:2 用法:find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat| rate|opt|exec] [路径...] [表达式]code
    • 嗯.. 很奇怪。我不知道你的“发现”是怎么回事。你在哪个平台?您是在使用 /bin/find,还是在您的路径中有其他内容?尝试输入“找到”。还可以尝试在脚本中提供 /bin/find 的完整路径。
    【解决方案3】:

    这是将文件夹从 "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
    

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

    【讨论】:

    • 谢谢!这个为每个音频文件执行 mkdir。最好只对每个专辑进行迭代,重命名和移动整个文件夹,忽略其中的内容。
    【解决方案4】:

    花了一些时间作为一个业余爱好者。但这是我的最终解决方案。非常感谢您的意见@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
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多