![查找并替换包含与 Windows 不兼容字符的文件夹和文件名称](https://linux22.com/image/918692/%E6%9F%A5%E6%89%BE%E5%B9%B6%E6%9B%BF%E6%8D%A2%E5%8C%85%E5%90%AB%E4%B8%8E%20Windows%20%E4%B8%8D%E5%85%BC%E5%AE%B9%E5%AD%97%E7%AC%A6%E7%9A%84%E6%96%87%E4%BB%B6%E5%A4%B9%E5%92%8C%E6%96%87%E4%BB%B6%E5%90%8D%E7%A7%B0.png)
我的 Ubuntu (18.04) 驱动器与 Windows 驱动器同步。Ubuntu 中的某些文件夹和文件名包含 Windows 中不允许的字符,这会导致同步过程中出现问题。我想找到这些字符,然后从文件/文件夹名称中删除它们,或者用“x”替换这些字符。这需要在文件夹/目录树中递归完成。
我怎样才能快速完成此操作?
以下是 Windows 中禁止使用的字符 (修改自 Christopher Oezbek 的回答):
< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
答案1
尝试rename
,您需要先安装它。
sudo apt-get install rename
下面是一个示例,您可以更改*
为文件/文件夹所在的位置。
~$ 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