Skip to main content
1 of 8
user avatar
user avatar

Rename files AND DIRECTORIES with windows reserved characters, is giving errors

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' {} +

It gives me a problem with filenames with newlines, it thinks each line is a different file and it's trying to rename files that don't exist. Also the rename should only execute for files that present problems in the filenames, so use the find flag -name. It also needs to remove the spaces at the start and end.

Also I need to ignore the ..Trash-1000 directory as it's giving me input/output error. Having a script that handles those errors in case there are files in that directory that can be renamed would be perfect.

user96101