Site icon moneyslow.com

find文件名包含空格并替换

暴强的3000部小人书连环画全集

暴强的3000部小人书连环画全集

使用Perl版的`rename`命令来批量替换空格:

批量替换文件名中的空格为横线:
find . -type f -name '* *' -exec rename 's/ /-/g' {} +
批量替换文件夹名中的空格为横线:
find . -type d -name '* *' -exec rename 's/ /-/g' {} +

使用find命令查找文件名中包含空格的文件。
[root@DB-Server kerry]# find . -type f -name "* *" -print
./test.txt
./tes t.txt
那么如何将这些空格替换掉呢? 下面脚本可以替换文件中间的空格,用下划线替换空格,但是只能替换文件中间的空格,并不能替换文件名后面的空格。如下测试所示:
find . -type f -name "* *" -print |
while read name; do
na=$(echo $name | tr ' ' '_')
if [[ $name != $na ]]; then
mv "$name" "$na"
fi
done
上面脚本只能将文件名中间有空格的替换为下划线。那么如何解决文件名后有空格的情况呢? 可以用其它shell脚本实现,如下所示:
[root@DB-Server kerry]# rm -rf *
[root@DB-Server kerry]# cat >"test.txt "
12
[root@DB-Server kerry]# cat >"tes t.txt"
12
[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
如上所示,虽然文件名中间的空格被替换为了下划线,但是后面的空格没有替换为下划线,而是将那些空格直接截断了。Why?下面使用sed命令也是如此
[root@DB-Server kerry]# rm -rf *
[root@DB-Server kerry]# cat >"test.txt "
12
[root@DB-Server kerry]# cat >"tes t.txt"
12
[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]#
其实,这个是因为读取文件名是$file 与"$file"是不同的,$file不会识别文件名后面的空格,而"$file"才会失败文件名后面的空格。所以上面脚本其实只是取巧而已。
[root@DB-Server kerry]# rm -rf *;
[root@DB-Server kerry]# cat >"test.txt "
123
[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]#
所以,正确的替换空格的命令应该为如下:
方案1:
[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 ' ' '\n'` ; done
[root@DB-Server kerry]# find . -type f -name "* *" -print
[root@DB-Server kerry]# ls test.txt
test.txt
[root@DB-Server kerry]#
方案2:
[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____

Exit mobile version