Linux下解决文件名空格或含特殊字符的办法

2021年01月26日

刚才我在ls -l服务器网站安装目录下文件的时候,发现一个html文件名中含有一个特殊的空格,使用mv进行更名的时候返回错误信息,不关文件权限的问题,我猜测应该是mv在包含空格的文件名是它就中断了,所以并没有生效。文件夹中包含多个子文件夹而每个文件夹下面都有恼人的文件包含空格和小括号,一旦到了含空格文件名的时候全部卡壳或是执行失败,用过sed, awk, rename可是到了mv $file $dest_fn的时候要么文件全部被mv到 /dev/null 或被重命名为(null)。

mv /var/www/xiaozhen .html /var/www/xiaozhen.html
mv: target `/var/www/xiaozhen.html' is not a directory

解决办法很简单,使用一行shell命令就可以了。

ls -1 | awk '{a=$0;gsub("[() ]","",a);system("mv \""$0"\" "a)}'


sicnature ---------------------------------------------------------------------
Your current IP address is: 3.236.226.100
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://myzhenai.com.cn/post/3640.html

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注