欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Linux查找處理文件名后包含空格的文件(兩種方法)

 更新時間:2017年11月14日 11:14:07   作者:瀟湘隱者  
在linux中如何查找處理文件名后包含空格的文件呢?怎么批量替換處理這些空格呢?下面小編給大家?guī)砹藘煞N方法,需要的朋友參考下吧

當(dāng)Linux下文件名中出現(xiàn)空格這類特殊情況話,如何查找或確認(rèn)那些文件名后有空格呢? 又怎么批量替換處理掉這些空格呢? 

方法1:

輸入文件名后使用Tab鍵,如果使用Tab鍵后面出現(xiàn)\ \ \這樣的可見字符,那么該文件名包含空格。當(dāng)然,這個方法弊端很大,例如,效率低下,不能批量查找,只有當(dāng)你懷疑某個文件名后有空格,這個方法才比較湊效。另外,不能查找文件中間包含空格的文件名。如下測試所示: 

[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腳本實現(xiàn),如下所示:

[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]# 


所以,正確的替換空格的命令應(yīng)該為如下:

方案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____

當(dāng)然對于這兩種特殊情況,上面腳本都不能一起處理,如上所示,后面的空格會被替換成了下劃線。這反而不是我們想要的,反而最上面的那兩種腳本,可以誤打誤撞的解決這兩種問題。當(dāng)然讓前提是你得知其然知其所以然!

相關(guān)文章

  • Shell腳本中awk指令的用法

    Shell腳本中awk指令的用法

    今天小編就為大家分享一篇關(guān)于Shell腳本中awk指令的用法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • shell腳本實現(xiàn)Hbase服務(wù)的監(jiān)控報警和自動拉起問題

    shell腳本實現(xiàn)Hbase服務(wù)的監(jiān)控報警和自動拉起問題

    這篇文章主要介紹了shell腳本實現(xiàn)Hbase服務(wù)的監(jiān)控報警和自動拉起,主要是通過服務(wù)名監(jiān)控和端口監(jiān)控,通過企業(yè)微信消息通知腳本,對此內(nèi)容感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • 每天一個Linux命令之shell單引號和雙引號的經(jīng)典解釋

    每天一個Linux命令之shell單引號和雙引號的經(jīng)典解釋

    這篇文章主要給大家介紹了關(guān)于每天一個Linux命令之shell單引號和雙引號的經(jīng)典解釋,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Linux系統(tǒng)具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • crond構(gòu)建linux定時任務(wù)及日志查看腳本詳解

    crond構(gòu)建linux定時任務(wù)及日志查看腳本詳解

    這篇文章主要為大家介紹了crond構(gòu)建linux定時任務(wù)及日志查看腳本詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • Python執(zhí)行Linux系統(tǒng)命令的4種方法

    Python執(zhí)行Linux系統(tǒng)命令的4種方法

    這篇文章主要介紹了Python執(zhí)行Linux系統(tǒng)命令的4種方法,即在Python腳本中調(diào)用Shell命令,需要的朋友可以參考下
    2014-10-10
  • Linux?top命令詳解

    Linux?top命令詳解

    top命令是Linux下常用的性能分析工具,能夠?qū)崟r顯示系統(tǒng)中各個進程的資源占用狀況,類似于Windows的任務(wù)管理器,這篇文章主要介紹了Linux?top命令詳解,包括top命令的使用,需要的朋友可以參考下
    2022-10-10
  • 將shell腳本正確的放在后臺運行方式

    將shell腳本正確的放在后臺運行方式

    這篇文章主要介紹了將shell腳本正確的放在后臺運行方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 一文掌握Linux命令lsscsi

    一文掌握Linux命令lsscsi

    想要弄明白lsscsi命令,首先我們必須搞清楚什么是SCSI,以及常見的硬盤接口,常用的硬盤參數(shù),今天通過本文給大家介紹下Linux命令lsscsi,需要的朋友可以參考下
    2022-09-09
  • Linux下科學(xué)計數(shù)法(e)轉(zhuǎn)化為數(shù)字的方法

    Linux下科學(xué)計數(shù)法(e)轉(zhuǎn)化為數(shù)字的方法

    這篇文章主要介紹了Linux下科學(xué)計數(shù)法(e)轉(zhuǎn)化為數(shù)字的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Linux vim編輯命令模式

    Linux vim編輯命令模式

    vi(vim)是上Linux非常常用的編輯器,很多Linux發(fā)行版都默認(rèn)安裝了vi(vim)。這篇文章給大家介紹了Linux vim編輯命令模式,非常不錯,感興趣的朋友參考下吧
    2016-11-11

最新評論