linux 中删除文件名中的空格

 

001、

[root@PC1 test]# ls
a b c d.txt  x y.txt
[root@PC1 test]# ll -h                ## 测试数据,两个文件名中都有空格
total 8.0K
-rw-r--r--. 1 root root  9 Dec 31 20:56 a b c d.txt
-rw-r--r--. 1 root root 25 Dec 31 20:56 x y.txt

 

 

002、删除文件名中的空格

[root@PC1 test]# ls
a b c d.txt  x y.txt
[root@PC1 test]# rename \  "" *                 ## 转义字符\后面一共有两个空格,第一个空格是进行删除的空格, 第二个空格是rename命令的语法要求
[root@PC1 test]# ls                             ## 结果, 只删除了文件名中的第一个空格
ab c d.txt  xy.txt

 

 

003、删除文件名中所有的空格

[root@PC1 test]# ls             ## 测试数据
a b c d.txt  x y.txt
[root@PC1 test]# find *.txt | awk '{if(NR == 1) {max = gsub(" ", "&")}; s = gsub(" ", "&"); if(s > max) {max = s}} END {print max}' | xargs seq | while read i; do rename \  "" *; done
[root@PC1 test]# ls         ## 删除文件名中的所有空格
abcd.txt  xy.txt

 

posted @ 2022-12-31 21:29  小鲨鱼2018  阅读(898)  评论(0编辑  收藏  举报