0

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, the characters < > : " \ | ? * should be removed or better yet substituted for similar characters which aren't reserved and the spaces at the start and end of a filename should be removed. Here is an example of what I want:

rename(' Fîlenämè\n\r\n$@<>:"\|?* \n ', 'Fîlenämè $@')

Here is what would be even better:

rename(' Fîlenämè\n\r\n$@<>:"\|?* \n ', 'Filename $@')

According to this answer it should be something like this:

LC_ALL=C find . -depth -execdir rename -n 's/[\r\n]+/ /g; s/:/./g; s/[\|]/-/g; s/[<>"?*]//g; s/[ \f\t\v]+$//g; s/^[ \f\t\v]+//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 -execdir rename -n 's/[\r\n]+/ /g; s/:/./g; s/[\|]/-/g; s/[<>"?*]//g; s/[ \f\t\v]+$//g; s/^[ \f\t\v]+//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.

5
  • 1
    Does this answer your question? Rename files and directories with windows or linux reserved characters Mar 12, 2020 at 21:47
  • Again? I had to rewrite the question to emphasize the difference and I specifically said it isn't working.
    – user96101
    Mar 12, 2020 at 21:53
  • Are you asking how to prune the ..Trash-1000 path in your find command? If so, take a look at Explain find's -path and -prune options Mar 12, 2020 at 22:47
  • I still don't see the difference between this question, your previous one, and the marked duplicate. Perhaps examples of what you're trying to match would help. Mar 12, 2020 at 23:26
  • I added an example of what I'm trying to do. The prune is not working for me. Is it because I'm using a relative path?
    – user96101
    Mar 13, 2020 at 10:00

1 Answer 1

0

Instead of pruning the directory giving input/output errors the command can be executed once for each of the other directories.

This is the command that I used to remove windows reserved characters:

find . -execdir rename -n 's/[\r\n]+/ /g;
                           s/:/./g;
                           s/[\|]/-/g;
                           s/[<>"?*]//g;
                           s/[ \f\t\v]+$//g;
                           s/^[ \f\t\v]+//g' {} \;

It turns newlines to spaces, then substitutes : for ., then changes \| with -, then removes <>"?*, then deletes spaces at the end of the filename and then it does the same for spaces at the start.

You must log in to answer this question.