PDA

View Full Version : batch remove 3 spaces from the end of folder names?



sohlinux
October 8th, 2014, 09:09 PM
Hello, how can I batch remove 3 spaces from the end of all folder names including sub folders?

thanks

varunendra
October 9th, 2014, 06:18 AM
I guess you must run something like this -

find <target directory>/ -type d -iname "*[[:graph:]] " -exec rename 's/ $//' "{}" \;
..multiple times until you get no more errors about 'no such file or directory'. Unless of course we can turn the above into a much more smart script that can perform the operation recursively from the maximum depth to the root of the target directory.

Or maybe some smart expression formula in GUI tools like 'Krename' can do the job. I tried its "Find and Replace" function and it ran into same error where it couldn't find the subdirectories because the name of their parent directory was changed.

ofnuts
October 9th, 2014, 08:04 AM
I guess you must run something like this -

find <target directory>/ -type d -iname "*[[:graph:]] " -exec rename 's/ $//' "{}" \;
..multiple times until you get no more errors about 'no such file or directory'. Unless of course we can turn the above into a much more smart script that can perform the operation recursively from the maximum depth to the root of the target directory.

Or maybe some smart expression formula in GUI tools like 'Krename' can do the job. I tried its "Find and Replace" function and it ran into same error where it couldn't find the subdirectories because the name of their parent directory was changed.

No time to come up with actual code, but getting the list of affected directories, sorting it in reverse (which will put the children first) and then applying rename to the result could work)(will require -print0, among other things)

sohlinux
October 9th, 2014, 09:03 AM
thanks guys, ill test that :)

Vaphell
October 9th, 2014, 11:28 AM
no need to rerun or sort, find -depth -exec to the rescue. It will go subdirs first, parents last.

ofnuts
October 9th, 2014, 01:13 PM
no need to rerun or sort, find -depth -exec to the rescue. It will go subdirs first, parents last.

Good to know :)

varunendra
October 9th, 2014, 05:32 PM
no need to rerun or sort, find -depth -exec to the rescue. It will go subdirs first, parents last.

Wow! The manpages are amazing, and so is ignorance!! Of the hundred times of having looked at the manpage of find, I never noticed the '-depth' option itself, let alone what it does and how. :-\"

Thanks for handing us the precious gem from our left pocket into our right hands! :p