I am trying to convert all files to lowercase in a directory tree and subsequently move spaces to underscores or hyphens. The first part I was able to get by searching which uses below code.
find . -depth -print0 | xargs -r0 perl-rename -n 's/(.*)\/([^\/]*)/$1\/\L$2/;'
Now I am trying to extend it to move spaces to _ also in a single regex but with no success till now. I tried with various combinations of \K operator as I think it'd be possible with that. Could you please help me out. The last command I tried was,
find . -depth -print0 | xargs -r0 perl-rename -n 's/(.*)\/([^\/]*)/$1\/\L$2/;s/(.*)\/\K(\S+)\s+\g1/_/g;'
but it doesn't seem to work.
You can use:
find . -depth -print0 |
xargs -0 perl-rename -n 's~([^/]+)$~\L$1~;s~(?:.*/|\G)\K(\S+)\s+(?![^/]*/)~$1_~g'