_QWEというディレクトリのサブサブディレクトリの名前を変更します。

_QWEというディレクトリのサブサブディレクトリの名前を変更します。

次の名前のすべてのディレクトリでコマンドを実行できます_QWE

find . -name '_QWE' -type d -execdir touch {}/1234.txt \;

しかし、-execdir rename 's!^\./(\d+ -)\s(\d+\.)!$1!' {} \;_QWE_QWE

私は、次のようなディレクトリ構造があるとしましょう。

├── folder_1
│   └── _QWE
│       ├── Course 1
│       │   ├── 1 - Introduction
│       │   ├── 2 - 1. Basics of Course 1
│       │   ├── 3 - 2. Conclusion of Course 1
│       ├── Course 2
│       │   ├── 1 - Introduction
│       │   ├── 2 - 1. Basics of Course 2
│       │   ├── 3 - 2. Conclusion of Course 2
├── folder_2
│   └── folder_3
│      └── _QWE
│          ├── Course X1
│          │   ├── 1 - Introduction
│          │   ├── 2 - 1. Basics of Course X1
│          │   ├── 3 - 2. Conclusion of Course X1
│          ├── Course X2
│          │   ├── 1 - Introduction
│          │   ├── 2 - 1. Basics of Course X2
│          │   ├── 3 - 2. Conclusion of Course X2

ここで名前を変更したいです。

1 - Introduction
2 - 1. Basics of Course 1
3 - 2. Conclusion of Course 1
1 - Introduction
2 - 1. Basics of Course 2
3 - 2. Conclusion of Course 2
1 - Introduction
2 - 1. Basics of Course X1
3 - 2. Conclusion of Course X1
1 - Introduction
2 - 1. Basics of Course X2
3 - 2. Conclusion of Course X2

たとえば、ここでは3 - 2. Conclusion of Course X2名前がに変わります3 - Conclusion of Course X2。これが行われる作業です's!^\./(\d+ -)\s(\d+\.)!$1!'

明確に言えば、3 - 2. Conclusion of Course X2ファイル名ではなくディレクトリ名です。

どうすればいいですか?

アップデート1:

以下を使用してパスを取得できます。

for dir in $(find . -name '_QWE' -type d)/*/ ; do echo $dir ; done

または、

for dir in $(find . -name '_QWE' -type d)/*/ ; do (cd "$dir"; pwd); done

しかし、

for dir in $(find . -name '_QWE' -type d)/*/*/ ; do rename 's!^\./(\d+ -)\s(\d+\.)!$1!' $dir ; done

出力は生成されません。

ベストアンサー1

-path修飾子を使用して、一致するディレクトリのすぐ下にあるディレクトリを選択できます。

find . -depth -type d -path '*/_QWE/*/*' ! -path '*/_QWE/*/*/*' -exec rename 's!(/\d+)\s+-\s+\d+\.\s+([^/]*)$!$1 $2!' {} +

REを少し変更して、パスと一致するファイル名の部分に固定しました。 (ここで使用されているPCRE式の場合、\s+1つ以上の空白文字と一致、\d+1つ以上の数字と一致、[^/]*0個以上の文字で構成される文字列と一致とは別に /.)

サンプルディレクトリツリーを使用すると、次を使用するとその出力が表示されますrename -n

./folder_1/_QWE/Course 1/2 - 1. Basics of Course 1 renamed as ./folder_1/_QWE/Course 1/2 Basics of Course 1
./folder_1/_QWE/Course 1/3 - 2. Conclusion of Course 1 renamed as ./folder_1/_QWE/Course 1/3 Conclusion of Course 1
./folder_1/_QWE/Course 2/2 - 1. Basics of Course 1 renamed as ./folder_1/_QWE/Course 2/2 Basics of Course 1
./folder_1/_QWE/Course 2/3 - 2. Conclusion of Course 1 renamed as ./folder_1/_QWE/Course 2/3 Conclusion of Course 1
./folder_2/folder_3/_QWE/Course X1/2 - 1. Basics of Course 1 renamed as ./folder_2/folder_3/_QWE/Course X1/2 Basics of Course 1
./folder_2/folder_3/_QWE/Course X1/3 - 2. Conclusion of Course 1 renamed as ./folder_2/folder_3/_QWE/Course X1/3 Conclusion of Course 1
./folder_2/folder_3/_QWE/Course X2/2 - 1. Basics of Course 1 renamed as ./folder_2/folder_3/_QWE/Course X2/2 Basics of Course 1
./folder_2/folder_3/_QWE/Course X2/3 - 2. Conclusion of Course 1 renamed as ./folder_2/folder_3/_QWE/Course X2/3 Conclusion of Course 1

folder_4テストのために、aの下に同じ名前のサンプルディレクトリセットを追加しましたが、aディレクトリの下には追加しませんでした_QWE。予想通り、これらは正しく無視されます。

おすすめ記事