Ubuntu – Find and replace folder and file names with Windows incompatible characters

batch-renamecommand linedirectoryfilenamespecial characters

My Ubuntu (18.04) drive is synchronised with a Windows drive. Some of my folder and file names in Ubuntu contains characters not allowed in Windows and this causes issues during sync. I want to find these characters and either remove them from the file/folder names or replace these characters by an "x". This needs to be done recursively across the folder/directory tree.

How can I do this quickly?

Here are the characters forbidden in Windows (modified from Christopher Oezbek's answer):

< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

Best Answer

Try rename, you need to install it first.

sudo apt-get install rename

Below is an example, you can change * to where your file/folder located.

~$ ls
'1<.txt'   3:.txt     '5\.txt'  '7??.txt'
'2>.txt'  '4"d".txt'  '6|.txt'  '8*.txt'

~$ rename -v 's/[?<>\\:*|\"]/x/g' *
1<.txt renamed as 1x.txt
2>.txt renamed as 2x.txt
3:.txt renamed as 3x.txt
4"d".txt renamed as 4xdx.txt
5\.txt renamed as 5x.txt
6|.txt renamed as 6x.txt
7??.txt renamed as 7xx.txt
8*.txt renamed as 8x.txt

~$ ls
 1x.txt   2x.txt   3x.txt   4xdx.txt  '5x.txt'   6x.txt   7xx.txt   8x.txt
Related Question