以下被寫入到是易於如下(作爲二次目標),並且正確的角的情況下(作爲主要目標):
# because "find"'s usage is dense, we're defining that command in an array, so each
# ...element of that array can have its usage described.
find_cmd=(
find # run the tool 'find'
. # searching from the current directory
-depth # depth-first traversal so we don't invalidate our own renames
-type d # including only directories in results
-name '*[[:space:]]' # and filtering *those* for ones that end in spaces
-print0 # ...delimiting output with NUL characters
)
shopt -s extglob # turn on extended glob syntax
while IFS= read -r -d '' source_name; do # read NUL-separated values to source_name
dest_name=${source_name%%+([[:space:]])} # trim trailing whitespace from name
mv -- "$source_name" "$dest_name" # rename source_name to dest_name
done < <("${find_cmd[@]}") # w/ input from the find command defined above
參見:
- 一般描述
while read
循環的語法,以及上述具體修改的目的(IFS=
,read -r
等)。
- BashFAQ #100,描述如何在bash中執行字符串操作(特別是包括
${var%suffix}
語法,稱爲「參數擴展」,用於修改上述值中的後綴)。
- Using Find,對使用
find
及其與bash的集成提供了一般性介紹。
'mv - 「$ source_name」「$ dest_name」',這就是爲什麼查爾斯如何做到這一點總是一個好的經驗....你確實說過「*糾正在角落案件*」,完成。 –
@Charles非常感謝您的回答。按照您提供的定義,我嘗試查找帶有尾部空格的目錄。我還創建了一個尾隨空格的目錄,以確保一切正常。我使用了以下語法:** find。 '* [[:space:]]'-depth -type d -print0 **。當我運行命令時,我得到了磁盤中所有目錄的列表,不區分它們是否有尾隨空格,並且我收到以下消息:**'[[:space:]]''無此文件或目錄**。你能幫我弄清楚什麼是錯的嗎? – Leia
@Leia,在''* [[:space:]]'之前直接使用'-name'是很重要的 - 否則,它將被視爲要搜索的單個目錄的名稱,而不是用於過濾的條件。 (爲了解釋上面報告的結果,那麼:在你的評論中給出的命令是從*兩個*頂級目錄中搜索:'.'作爲第一個,'* [[:space:]]'作爲第二個;沒有單個目錄以後者名稱存在,因此錯誤和未過濾的輸出。) –