I can run a command in all directories named _QWE
using:
find . -name '_QWE' -type d -execdir touch {}/1234.txt \;
However, I need to run -execdir rename 's!^\./(\d+ -)\s(\d+\.)!$1!' {} \;
in all the immediate sub-directories of _QWE
to rename the sub-directories of the sub-directories of _QWE
.
I mean suppose I have a directory structure like:
├── 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
Here I want to rename:
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
Here for example, 3 - 2. Conclusion of Course X2
will be renamed to 3 - Conclusion of Course X2
. This is what 's!^\./(\d+ -)\s(\d+\.)!$1!'
does.
Just to be clear, 3 - 2. Conclusion of Course X2
is a directory name and not file name.
How can I do that?
Update 1:
I could get the paths using:
for dir in $(find . -name '_QWE' -type d)/*/ ; do echo $dir ; done
or,
for dir in $(find . -name '_QWE' -type d)/*/ ; do (cd "$dir"; pwd); done
But,
for dir in $(find . -name '_QWE' -type d)/*/*/ ; do rename 's!^\./(\d+ -)\s(\d+\.)!$1!' $dir ; done
Not yielding any output.
for dir in $(find ...)
is a bad idea. See Why is looping over find's output bad practice?. Use find's-exec
/-execdir
or usereadarray
/mapfile
to populate an array with NUL-separated output from find, then iterate over the array. BTW,rename
produces no output on stdout unless you tell it to with the-v
option. Also, if you want only the immediate sub-dirs of _QWE then use find's-maxdepth 1
option.