There are multiple texts with the name Hoge
in the directory Hoge
.
Here, I want to replace both the directory name and the file name with hoge
.
find `pwd` -name "*Hoge*" -print | perl -nle 'rename $_, s|Hoge|hoge|r'
At this point, a list of target directories and files will appear.
I don't use -print0
because I want perl to process each line.
% find `pwd` -name "*Hoge*" -print
/Users/omokawayasuaki/test/Hoge
/Users/omokawayasuaki/test/Hoge/Hoge1.txt
/Users/omokawayasuaki/test/Hoge/Hoge3.txt
/Users/omokawayasuaki/test/Hoge/Hoge2.txt
s|Hoge|hoge|r'
ofr
Is the default variable$_
Option to replace and return as a scalar value. The feature is that the original character string replaced is not changed.
https://perldoc.jp/docs/perl/5.18.1/perlreref.pod#OPERATORS
% tree
.
└── Hoge
├── Hoge1.txt
├── Hoge2.txt
└── Hoge3.txt
% find `pwd` -name "*Hoge*" -print | perl -nle 'rename $_, s|Hoge|hoge|r'
% find `pwd` -name "*Hoge*" -print | perl -nle 'rename $_, s|Hoge|hoge|r'
% tree
.
└── hoge
├── hoge1.txt
├── hoge2.txt
└── hoge3.txt
that's all
I couldn't overwrite the old directory name in one go, so I ended up hitting the command twice. To be honest, it's annoying. If you have a smarter solution, please let me know: bow:
Recommended Posts