您的位置:首页 > 运维架构 > Linux

Linux如何查找处理文件名后包含空格的文件

2017-11-13 17:07 603 查看
Linux如何查找处理文件名后包含空格的文件

当Linux下文件名中出现空格这类特殊情况话,如何查找或确认那些文件名后有空格呢?又怎么批量替换处理掉这些空格呢?

方法1:输入文件名后使用Tab键,如果使用Tab键后面出现\\\这样的可见字符,那么该文件名包含空格。当然,这个方法弊端很大,例如,效率低下,不能批量查找,只有当你怀疑某个文件名后有空格,这个方法才比较凑效。另外,不能查找文件中间包含空格的文件名。如下测试所示:

[root@DB-Serverkerry]#cat>"test.txt"
itisonlyfortest!
[1]+Stoppedcat>"test.txt"
[root@DB-Serverkerry]#cat>"test.txt"
itisonlyfortesttoo!
[2]+Stoppedcat>"test.txt"
[root@DB-Serverkerry]#lstest.txt
ls:test.txt:Nosuchfileordirectory
[root@DB-Serverkerry]#lstest
test~test1.pytest.pytest.shtest.txt
[root@DB-Serverkerry]#lstest.txt\\\\
test.txt
[root@DB-Serverkerry]#lstes
test~test1.pytest.pytest.shtest.txttest.txt






方法2:使用find命令查找文件名中包含空格的文件。

[root@DB-Serverkerry]#find.-typef-name"**"-print

./test.txt

./test.txt

那么如何将这些空格替换掉呢?下面脚本可以替换文件中间的空格,用下划线替换空格,但是只能替换文件中间的空格,并不能替换文件名后面的空格。如下测试所示:

find.-typef-name"**"-print|
whilereadname;do
na=$(echo$name|tr'''_')
if[[$name!=$na]];then
mv"$name""$na"
fi
done






上面脚本只能将文件名中间有空格的替换为下划线。那么如何解决文件名后有空格的情况呢?可以用其它shell脚本实现,如下所示:

[root@DB-Serverkerry]#rm-rf*
[root@DB-Serverkerry]#cat>"test.txt"
12
[root@DB-Serverkerry]#cat>"test.txt"
12
[root@DB-Serverkerry]#find.-typef-name"**"-print
./test.txt
./test.txt
[root@DB-Serverkerry]#forfilein*;domv"$file"`echo$file|tr'''_'`;done
[root@DB-Serverkerry]#find.-typef-name"**"-print
[root@DB-Serverkerry]#ls-lrt
total8
-rw-r--r--1rootroot0Nov1310:04test.txt
-rw-r--r--1rootroot0Nov1310:04tes_t.txt






如上所示,虽然文件名中间的空格被替换为了下划线,但是后面的空格没有替换为下划线,而是将那些空格直接截断了。Why?下面使用sed命令也是如此

[root@DB-Serverkerry]#rm-rf*
[root@DB-Serverkerry]#cat>"test.txt"
12
[root@DB-Serverkerry]#cat>"test.txt"
12
[root@DB-Serverkerry]#find.-typef-name"**"-print
./test.txt
./test.txt
[root@DB-Serverkerry]#foriin*''*;domv"$i"`echo$i|sed-e's//_/g'`;done
[root@DB-Serverkerry]#find.-typef-name"**"-print
[root@DB-Serverkerry]#ls-lrt
total8
-rw-r--r--1rootroot0Nov1309:29test.txt
-rw-r--r--1rootroot0Nov1309:29tes_t.txt
[root@DB-Serverkerry]#
[root@DB-Serverkerry]#






其实,这个是因为读取文件名是$file与"$file"是不同的,$file不会识别文件名后面的空格,而"$file"才会失败文件名后面的空格。所以上面脚本其实只是取巧而已。

[root@DB-Serverkerry]#rm-rf*;
[root@DB-Serverkerry]#cat>"test.txt"
123
[root@DB-Serverkerry]#forfilein*;doecho"$file";echo"$file"|wc-m;done;
test.txt
13
[root@DB-Serverkerry]#forfilein*;doecho$file;echo$file|wc-m;done;
test.txt
9
[root@DB-Serverkerry]#






所以,正确的替换空格的命令应该为如下:

方案1:

[root@DB-Serverkerry]#rm-rf*
[root@DB-Serverkerry]#cat>"test.txt"
123456
[root@DB-Serverkerry]#find.-typef-name"**"-print
./test.txt
[root@DB-Serverkerry]#forfilein*;domv"$file"`echo"$file"|tr'''\n'`;done
[root@DB-Serverkerry]#find.-typef-name"**"-print
[root@DB-Serverkerry]#lstest.txt
test.txt
[root@DB-Serverkerry]#


方案2:

[root@DB-Serverkerry]#
[root@DB-Serverkerry]#rm-rf*
[root@DB-Serverkerry]#cat>"test.txt"
123456
[root@DB-Serverkerry]#forfilein*''*;domv"$file"`echo"$file"|sed-e's//n/g'`;done
[root@DB-Serverkerry]#find.-typef-name"**"-print


但是对于文件名中间包含空格的情况,上面两个脚本都无法完美解决。如下所示:

[root@DB-Serverkerry]#
[root@DB-Serverkerry]#rm-rf*
[root@DB-Serverkerry]#cat>"test.txt"
123456
[root@DB-Serverkerry]#forfilein*;domv"$file"`echo"$file"|tr'''_'`;done
[root@DB-Serverkerry]#find.-typef-name"**"-print
[root@DB-Serverkerry]#ls-lrt
total8
-rw-r--r--1rootroot7Nov1316:00tes_t.txt
[root@DB-Serverkerry]#
[root@DB-Serverkerry]#rm-rf*
[root@DB-Serverkerry]#cat>"test.txt"
123456
[root@DB-Serverkerry]#cat>"test.txt"
654321
[root@DB-Serverkerry]#find.-typef-name"**"-print
./test.txt
./test.txt
[root@DB-Serverkerry]#forfilein*;domv"$file"`echo"$file"|tr'''_'`;done
[root@DB-Serverkerry]#find.-typef-name"**"-print
[root@DB-Serverkerry]#ls-lrt
total12
-rw-r--r--1rootroot0Nov1315:59tes_t.txt
-rw-r--r--1rootroot7Nov1315:59test.txt____


当然对于这两种特殊情况,上面脚本都不能一起处理,如上所示,后面的空格会被替换成了下划线。这反而不是我们想要的,反而最上面的那两种脚本,可以误打误撞的解决这两种问题。当然让前提是你得知其然知其所以然!

参考资料:

http://www.eygle.com/digest/2006/11/linux_replace_blank.html

https://stackoverflow.com/questions/1806868/linux-replacing-spaces-in-the-file-names

https://www.keakon.net/2011/10/20/bash%E4%B8%8B%E5%A4%84%E7%90%86%E5%8C%85%E5%90%AB%E7%A9%BA%E6%A0%BC%E7%9A%84%E6%96%87%E4%BB%B6%E5%90%8D
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: