I have directory structure like the following:
. ├── ParentDirectory │ ├── ChildDirectory1 │ │ ├── 01-File.txt │ │ ├── 02-File.txt │ │ ├── 03-File.adoc │ │ ├── 04-File.md │ │ └── 05-File.txt │ ├── ChildDirectory2 │ │ ├── 01-File.txt │ │ ├── 02-File.txt │ │ ├── 03-File.adoc │ │ ├── 04-File.txt │ │ ├── 05-File.txt │ │ └── 06-File.md │ ├── ChildDirectory3 │ │ ├── 01-File.txt │ │ ├── 02-File.txt │ │ ├── 03-File.adoc │ │ ├── 04-File.md │ │ ├── 05-File.md │ │ └── 06-File.txt You may have noticed that some file and directory has leading whitespace. What you may not have noticed is some file and directory has trailing whitespace as well.
For trailing whitespace I tried:
% find . -exec rename 's/ *$//' {} + I have to run this command multiple times. On first run it rename parent directory. On second run it rename the child. My directory structure actually goes much deeper. So, running a command multiple times is not a good solution.
For leading whitespace find . -exec rename 's/^ *//' {} + does not work at all.
How can I remove all leading and trailing whitespace from both file and directory names recursively?
find . -depth -exec rename 's/^ *//' {} +works. Stillfind . -depth -exec rename 's/^ *//' {} +not working.