Linux如何查找處理文件名後包含空格的文件 當Linux下文件名中出現空格這類特殊情況話,如何查找或確認那些文件名後有空格呢? 又怎麼批量替換處理掉這些空格呢? 方法1: 輸入文件名後使用Tab鍵,如果使用Tab鍵後面出現\ \ \這樣的可見字元,那麼該文件名包含空格。當然,這個方法弊端很大,例如... ...
Linux如何查找處理文件名後包含空格的文件
當Linux下文件名中出現空格這類特殊情況話,如何查找或確認那些文件名後有空格呢? 又怎麼批量替換處理掉這些空格呢?
方法1: 輸入文件名後使用Tab鍵,如果使用Tab鍵後面出現\ \ \這樣的可見字元,那麼該文件名包含空格。當然,這個方法弊端很大,例如,效率低下,不能批量查找,只有當你懷疑某個文件名後有空格,這個方法才比較湊效。另外,不能查找文件中間包含空格的文件名。如下測試所示:
[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: 使用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
13
[root@DB-Server kerry]# for file in *; do echo $file; echo $file | wc -m ; done;
test.txt
9
[root@DB-Server kerry]#
所以,正確的替換空格的命令應該為如下:
方案1:
[root@DB-Server kerry]# rm -rf *
[root@DB-Server kerry]# cat >"test.txt "
123456