Linux查找处理文件名后包含空格的文件(两种方法)

作者: 日期:2019-08-13

方法1:


[root@db-server kerry]# cat "test.txt "
it is only for test!
[1]+ stopped cat "test.txt "
[root@db-server kerry]# cat "tes t.txt"
it is only for test too!
[2]+ stopped cat "tes t.txt"
[root@db-server kerry]# ls test.txt
ls: test.txt: no such file or directory
[root@db-server kerry]# ls test
test~ test1.py test.py test.sh test.txt 
[root@db-server kerry]# ls test.txt\ \ \ \ 
test.txt 
[root@db-server kerry]# ls tes
test~ test1.py test.py test.sh tes t.txt test.txt 

方法2:


[root@db-server kerry]# find . -type f -name "* *" -print
./test.txt 
./tes t.txt 

find . -type f -name "* *" -print |
while read name; do
na=$
if [[ $name != $na ]]; then
mv "$name" "$na"
done 

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

[root@db-server kerry]# rm -rf *
[root@db-server kerry]# cat "test.txt "
[root@db-server kerry]# cat "tes t.txt"
[root@db-server kerry]# find . -type f -name "* *" -print
./test.txt 
./tes t.txt
[root@db-server kerry]# for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
[root@db-server kerry]# find . -type f -name "* *" -print
[root@db-server kerry]# ls -lrt
total 8
-rw-r--r-- 1 root root 0 nov 13 10:04 test.txt
-rw-r--r-- 1 root root 0 nov 13 10:04 tes_t.txt

[root@db-server kerry]# rm -rf *
[root@db-server kerry]# cat "test.txt "
[root@db-server kerry]# cat "tes t.txt"
[root@db-server kerry]# find . -type f -name "* *" -print
./test.txt 
./tes t.txt
[root@db-server kerry]# for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done
[root@db-server kerry]# find . -type f -name "* *" -print
[root@db-server kerry]# ls -lrt
total 8
-rw-r--r-- 1 root root 0 nov 13 09:29 test.txt
-rw-r--r-- 1 root root 0 nov 13 09:29 tes_t.txt
[root@db-server kerry]# 
[root@db-server kerry]#


[root@db-server kerry]# rm -rf *;
[root@db-server kerry]# cat "test.txt "
[root@db-server kerry]# for file in *; do echo "$file"; echo "$file" | wc -m ; done;
test.txt 
[root@db-server kerry]# for file in *; do echo $file; echo $file | wc -m ; done;
test.txt
[root@db-server kerry]# 



[root@db-server kerry]# rm -rf *
[root@db-server kerry]# cat "test.txt "
123456
[root@db-server kerry]# find . -type f -name "* *" -print
./test.txt 
[root@db-server kerry]# for file in *; do mv "$file" `echo "$file" | tr ' ' '
'` ; done
[root@db-server kerry]# find . -type f -name "* *" -print
[root@db-server kerry]# ls test.txt
test.txt
[root@db-server kerry]# 

[root@db-server kerry]# 
[root@db-server kerry]# rm -rf *
[root@db-server kerry]# cat "test.txt "
123456
[root@db-server kerry]# for file in *' '*; do mv "$file" `echo "$file" | sed -e 's/ /n/g'`; done
[root@db-server kerry]# find . -type f -name "* *" -print 

[root@db-server kerry]# 
[root@db-server kerry]# rm -rf *
[root@db-server kerry]# cat "tes t.txt"
123456
[root@db-server kerry]# for file in *; do mv "$file" `echo "$file" | tr ' ' '_'` ; done
[root@db-server kerry]# find . -type f -name "* *" -print
[root@db-server kerry]# ls -lrt 
total 8
-rw-r--r-- 1 root root 7 nov 13 16:00 tes_t.txt
[root@db-server kerry]# 
[root@db-server kerry]# rm -rf *
[root@db-server kerry]# cat "tes t.txt"
123456
[root@db-server kerry]# cat "test.txt "
654321
[root@db-server kerry]# find . -type f -name "* *" -print
./test.txt 
./tes t.txt
[root@db-server kerry]# for file in *; do mv "$file" `echo "$file" | tr ' ' '_'` ; done
[root@db-server kerry]# find . -type f -name "* *" -print
[root@db-server kerry]# ls -lrt
total 12
-rw-r--r-- 1 root root 0 nov 13 15:59 tes_t.txt
-rw-r--r-- 1 root root 7 nov 13 15:59 test.txt____

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

首页
电话
短信
联系