如何簡單清理檔案名稱?

如何簡單清理檔案名稱?

作業系統:Kubuntu 22.04.4 LTS x86_64

上面顯示:
neofetch --stdout |grep 'OS:'

如何簡單地清理檔案名稱而不是檔案內容?
透過從檔案名稱中刪除:

  • \n 換行符
  • \t 選項卡
  • 不可列印字符
  • 空間

對於 Microsoft Windows, < > : " \ / | ? * 檔案名稱中沒有。

< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

範例 1:
如何建立問題檔案名稱。 在終端機中
使用 mv 將換行符號新增至檔案名稱:

touch a

mv a $'b\nc'  # move (rename) files 
ls            # 'b'$'\n''c'
ls -b         # b\nc

使用 GUI,按 F2 = 重新命名:

b
c

範例 2:
較長的名稱。

touch 'This filename will have Tabs and Newlines_.txt'

mv 'This filename will have Tabs and Newlines_.txt' $'This\tfilename\twill\thave\tTabs\nand\nNewlines_.txt'  
ls            # 'This'$'\t''filename'$'\t''will'$'\t''have'$'\n''Tabs'$'\n''and'$'\n''Newlines_.txt'
ls -b         # This\tfilename\twill\thave\nTabs\nand\nNewlines_.txt

使用 GUI,按 F2 = 重新命名:

This    filename    will    have    Tabs
and
Newlines_.txt

範例 3:
更多參與。
touch 'This filename will have Tabs and Newlines & SPACES & colon: _.txt'

mv 'This filename will have Tabs and Newlines & SPACES & colon: _.txt' $'This\tfilename\twill\thave\tTabs\nand\nNewlines & SPACES & colon: _.txt'
ls         # 'This'$'\t''filename'$'\t''will'$'\t''have'$'\t''Tabs'$'\n''and'$'\n''Newlines & SPACES & colon: _.txt'
ls -b      # This\tfilename\twill\thave\tTabs\nand\nNewlines\ &\ SPACES\ &\ colon:\ _.txt

使用 GUI,按 F2 = 重新命名:

This    filename    will    have    Tabs
and
Newlines & SPACES & colon: _.txt

範例 3 中的 bash 清理檔案名稱:

#!/bin/bash   
clear
# FILE : original_filename comes from inotifywait command, On access, auto detect a file in /home/xxx/Downloads to eventually do a CLAM virus scan on FILE. 
filename1=$FILE
filename1=$'This\tfilename\twill\thave\tTabs\nand\nNewlines\ &\ SPACES\ &\ colon:\ _.txt'
echo "$filename1" 
filename2="${filename1//[$'\t'$'\n'$'\e'$'\r'$'\f'$'\v'$'\b'$'\a'$'\0']/-}"  # Replace Non Printable Characters with dash - 
echo "$filename2" 
filename3="${filename2//[$'\ ']/_}"    # Replace space with underscore _                                                   
echo "$filename3" 
filename4="${filename3//[$':']/_}"     # Replace colon : with underscore _                                                  
echo "$filename4" 

bash 結果範例 3:

This    filename        will    have    Tabs
and
Newlines\ &\ SPACES\ &\ colon:\ _.txt  

This-filename-will-have-Tabs-and-Newlines\ &\ SPACES\ &\ colon:\ _.txt
This-filename-will-have-Tabs-and-Newlines__&__SPACES__&__colon:___.txt
This-filename-will-have-Tabs-and-Newlines__&__SPACES__&__colon____.txt

用破折號取代了不可列印的字元 - 包括

  • \n 換行符
  • \t 製表
    符 用底線 _ 替換空格 _用底線 _
    替換冒號:

參考 1:
完整的不可列印字元列表
https://fjolt.com/article/linux-non-printable-characters

Name                 Binary  Decimal  Hexadecimal  Octal  Caret     Escape
                                                          Notation  Sequence  
Null               000 0000        0           00    000     ^@           \0 
Beep(BEL)          000 0111        7           07    007     ^G           \a 
Backspace(BS)      000 1000        8           08    010     ^H           \b        
HorizontalTab(HT)  000 1001        9           09    011     ^I           \t 
LineFeed(LF)       000 1010       10           0A    012     ^J           \n 
VerticalTab(VT)    000 1011       11           0B    013     ^K           \v 
FormFeed(FF)       000 1100       12           0C    014     ^L           \f 
CarriageReturn(CR) 000 1101       13           0D    015     ^M           \r 
Escape(ESC)        001 1011       27           1B    033     ^[           \e 

9 Escape Sequences:  
\0 
\a 
\b 
\t 
\n 
\v 
\f 
\r 
\e 


used: 
column -t -o '  ' a.txt b.txt 

參考2:
https://www.linuxquestions.org/questions/general-10/remove-newline-and-tab-characters-from-filename-4175690259/

orig_filename=$'TN1\tThis\nFileName\nHas\tTabsandNewlines'
echo test > "$orig_filename"
new_filename="${orig_filename//[$'\t'$'\n']/-}"
mv --no-clobber "$orig_filename" "$new_filename"

參考3:
Shell參數擴展
${parameter//pattern/string}
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

問題:
如何簡單地清理檔案名稱?
有哪些比較簡單的指令?

--

答案1

您可以使用類似的應用程式排毒其中內建了您可能想要清潔的內容。他們叫序列。用法範例:

detox -s safe -v /some/folder/or/file

這只會使用使名稱對於 Unix 使用更安全的序列。-v詳細標誌準確地指定了受影響的名稱。

您也可以在detoxrc設定檔中建立自己的組合序列,如上面文件中所述。

相關內容