ディレクトリを独自のサブディレクトリに移動またはコピーします。

ディレクトリを独自のサブディレクトリに移動またはコピーします。

次のディレクトリがあります。

├── test
│   └── third2
│       ├── sec2
│       │   ├── sec
│       │   │   └── Backup
│       │   │       └── third3
│       │   │           └── sec2
│       │   │               └── sec
│       │   │                   └── Backup
│       │   ├── sec5
│       │   ├── third
│       │   └── third3
│       │       └── sec2
│       │           ├── sec
│       │           ├── sec5
│       │           └── third
│       └── sec3
└── test.sh

"test/third2"ディレクトリをサブディレクトリに移動したいです"test/third2/sec2"

したがって、次のようになります。

├── test
│   └── sec2
│       ├── third2
│       │   ├── sec
│       │   │   └── Backup
│       │   │       └── third3
│       │   │           └── sec2
│       │   │               └── sec
│       │   │                   └── Backup
│       │   ├── sec5
│       │   ├── third
│       │   └── third3
│       │       └── sec2
│       │           ├── sec
│       │           ├── sec5
│       │           └── third
│       └── sec3
└── test.sh

これを行うと、次のエラー メッセージが表示されます。

mv: cannot move 'third2' to a subdirectory of itself, 'third2/sec2/third2'

test.shコード:

cd test/
#mkdir third2/sec3
#mkdir test3
for f in *; do
        if [ -d "$f" ]; then
        cd "$f"
        echo "cd directories $f"
                for i in *; do
                        if [ -d "$i" ]; then
                                echo "Dir $i"
                                cd -
                                mv "$f" "$f"/"$i"
                        fi

                done
        fi
done

どうすればいいですか?私はUnixを初めて使用します。

Third2の親ディレクトリはsec2で、sec 2の親ディレクトリはtestでなければなりません。

編集:新しい例を使う:答えてくれてありがとう。単一ディレクトリの名前は変更できますが、複数のディレクトリは変更できません。正しく実行されませんでした。

次のディレクトリがあります。

├── songs
│   ├── Song 1
│   │   └── 1950
│   │       ├── A.txt
│   │       ├── B.txt
│   │       ├── C.txt
│   │       ├── D.txt
│   │       ├── E.txt
│   │       ├── F.txt
│   │       ├── G.txt
│   │       └── H.txt
│   ├── Song 2
│   │   ├── 1920
│   │   │   ├── A.txt
│   │   │   ├── B.txt
│   │   │   ├── C.txt
│   │   │   └── D.txt
│   │   └── 2000
│   │       ├── A.txt
│   │       └── B.txt
│   ├── Song 3
│   │   └── 2015
│   │       ├── A.txt
│   │       ├── B.txt
│   │       └── C.txt
│   ├── Song 4
│   │   └── 2013
│   │       ├── A.txt
│   │       ├── B.txt
│   │       ├── C.txt
│   │       └── D.txt
│   ├── Song 5
│   │   ├── 2012
│   │   │   ├── A.txt
│   │   │   └── B.txt
│   │   └── 2019
│   │       ├── A.txt
│   │       └── B.txt
│   └── Song 6
│       ├── 2011
│       │   └── A.txt
│       └── 2012
│           └── A.txt
├── songs.txt

次のコードを使用してsongs.shを実行した後:

cd songs/


for i in *; do
        if [ -d "$i" ]; then
                #echo "i == $i"
                cd "$i"
                for k in *; do
                        if [ -d "$k" ]; then
                                echo "test"
                                mv -f  "$k" "$i"
                        fi
                done
                cd ..
        mv -f "$i" "$k"
        fi
done
cd ..

私は次のような結果を得ます。

── songs
│   ├── 1950
│   │   └── Song 1
│   │       ├── A.txt
│   │       ├── B.txt
│   │       ├── C.txt
│   │       ├── D.txt
│   │       ├── E.txt
│   │       ├── F.txt
│   │       ├── G.txt
│   │       └── H.txt
│   ├── 2000
│   │   └── Song 2
│   │       ├── 2000
│   │       │   ├── A.txt
│   │       │   └── B.txt
│   │       ├── A.txt
│   │       ├── B.txt
│   │       ├── C.txt
│   │       └── D.txt
│   ├── 2012
│   │   └── Song 6
│   │       ├── 2012
│   │       │   └── A.txt
│   │       └── A.txt
│   ├── 2013
│   │   └── Song 4
│   │       ├── A.txt
│   │       ├── B.txt
│   │       ├── C.txt
│   │       └── D.txt
│   ├── 2015
│   │   └── Song 3
│   │       ├── A.txt
│   │       ├── B.txt
│   │       └── C.txt
│   └── 2019
│       └── Song 5
│           ├── 2019
│           │   ├── A.txt
│           │   └── B.txt
│           ├── A.txt
│           └── B.txt
├── songs.txt

欲しいです。 :

├── songs
│   ├── 1920
│   │   └── Song 2
│   │       ├── A.txt
│   │       ├── B.txt
│   │       ├── C.txt
│   │       └── D.txt
│   ├── 1950
│   │   └── Song 1
│   │       ├── A.txt
│   │       ├── B.txt
│   │       ├── C.txt
│   │       ├── D.txt
│   │       ├── E.txt
│   │       ├── F.txt
│   │       ├── G.txt
│   │       └── H.txt
│   ├── 2000
│   │   └── Song 2
│   │       ├── A.txt
│   │       └── B.txt
│   ├── 2011
│   │   └── Song 6
│   │       └── A.txt
│   ├── 2012
│   │   ├── Song 5
│   │   │   ├── A.txt
│   │   │   └── B.txt
│   │   └── Song 6
│   │       └── A.txt
│   ├── 2013
│   │   └── Song 4
│   │       ├── A.txt
│   │       ├── B.txt
│   │       ├── C.txt
│   │       └── D.txt
│   ├── 2015
│   │   └── Song 3
│   │       ├── A.txt
│   │       ├── B.txt
│   │       └── C.txt
│   └── 2019
│       └── Song 5
│           ├── A.txt
│           └── B.txt
├── songs.txt

ベストアンサー1

いくつかのディレクトリの名前だけを変更したいようです。

mv test/third2/sec2 test/third2/third2
mv test/third2 test/sec2

おすすめ記事