
OS: 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-非印刷可能文字
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
。
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:
シェルパラメータ拡張
${parameter//pattern/string}
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
質問:
ファイル名を簡単にクリーンアップするにはどうすればいいですか?
より簡単なコマンドは何ですか?
--