ディレクトリからファイルを読み取り、拡張子ごとにそのフォルダにクリーンアップします。

ディレクトリからファイルを読み取り、拡張子ごとにそのフォルダにクリーンアップします。

次の名前の複数のディレクトリを作成できるbash関数スクリプトを作成しようとしています。また、ファイルを拡張子ごとに適切なフォルダに整理しようとしました(例:.jpgは写真、.docは文書、.gifはメディアなど)。最初の部分は良いですが、ディレクトリが作成された後、2番目の部分は私を混乱させます。

    #!/bin/bash
    echo "Creating directory categories"

    function make_folder 
    {
        cd -; cd content; sudo mkdir ./$1
    }

    make_folder "documents"
    make_folder "other"
    make_folder "pictures"
    make_folder "media"

    echo "Directories have been made"; cd -
    exit

    ext="${filename##*.}" #set var ext to extension of files

    find ./random -name | #find and list all files in random folder
                          #pipe results of find into if statement

    if ext == ["jpg"; "jpeg"; "png"] #move ".jpg", etc to new destination
         then
           mv /path/to/source /path/to/destination

    elif ext == [".gif"; ".mov"] #move ".gif", etc to new destination 
         then
           mv /path/to/source /path/to/destination
    else                         #move other files into to new destination
           mv /path/to/source /path/to/destination
    fi

ベストアンサー1

私の頭の上で、私は「ケース」ドアを使用します。

つまり

case "$FILE" in
    *.jpg|*.jpeg)
        mv "$FILE" to where you want it
        ;;
    *.gif|*.mov)
        mv "$FILE" to where you want it
        ;;
    *)
        echo "Unmanaged file type: $FILE, skipping"
        ;;
 esac

...しかし、ループコンテナで梱包する必要があり、確かにfindを使用したい場合は適しています。

 find <your stuff> | 
     while read FILE
     do
         ...case statement goes here
     done

ちょうど私の$ 0.02

乾杯! /ダニエル

おすすめ記事