Skip to main content
4 of 8
added 324 characters in body
user avatar
user avatar

ERROR. Rename files AND DIRECTORIES with windows reserved characters

I had a hard drive that couldn't mount on Linux Mint and to fix it I had to go to Windows and do chkdsk /f e:. chkdsk fixed the problem but it renamed every file with special characters and moved those files to a found.000 directory. So now I have to move and rename again those files as they were before. If I run a command from time to time to rename the files with special characters I could avoid that happening again.

I want to recursively rename files AND DIRECTORIES from the current directory. The newlines should be replaced for space and the characters < > : " \ | ? * and the spaces at the start and end of a filename should be removed. According to this answer it should be something like this:

LC_ALL=C find . -depth -execdir rename -n 's/[:<>"\\|?*]//g' {} +

I need to ignore the ..Trash-1000 directory as this command is giving me input/output ERRORS and the command stops working. Having a script that handles those errors in case there are files in that directory that can be renamed would be perfect.

I was told to prune the ..Trash-1000 directory as in this answer. I also took a look at this one. But doing this is not working:

`LC_ALL=C find . -depth -path ./..Trash-1000 -prune -o -print -name '*[!a-zA-Z0-9 ._-]*' -execdir rename -n 's/[^a-zA-Z0-9 ._-]//g' {} +`

And it's still not deleting spaces at the start and end of the filenames.

I had to rewrite my question as the question Script to recursively replace invalid characters in filenames, not directories, with rename didn't answer mine.

user96101