はじめに
Linux系のOSで誤って~
、-
、?
などのような記号で始まるファイルが出来てしまった場合に、bash等のシェルから削除する方法を調べてみました。
削除方法
inodeの番号を利用した削除方法
やり方としては、削除対象ファイルのinode
番号を調べ、そのファイルをfind
コマンドでinode
番号(ファイルの作成日時、パーミッション、サイズ等々のメタ情報の管理番号)で抽出してxargs
、またはexec
に渡してrm
で削除するというものになります。
ここでは、以下の~
というファイルを削除します。
% ls | head
~
Applications
Desktop
Documents
Downloads
Dropbox
Fiddler2
Library
Movies
Music
ls
コマンドの-i
オプションでファイルのinode
番号を表示できます。
-i, --inode
各ファイルの i ノード番号を表示する
(man ls参照)
これで文字化けしたファイルのinode
番号を調べます。左の数値がinode
番号になります。
% ls -i | head
31290093 ~
30980372 Applications
379474 Desktop
379270 Documents
379367 Downloads
390474 Dropbox
11789816 Fiddler2
379464 Library
379514 Movies
379516 Music
find
コマンドの-inum
オプションとinode
番号を指定することで該当ファイルを抽出できます。
% find . -inum 31290093
./~
inode
番号で抽出する対象に間違いがないことが確認できたらxargs rm
の引数に渡して削除を実行します。
% find . -inum 32735580 | xargs rm
その他ある特定の文字で始まるファイルの削除方法
-で始まるファイルの削除方法
-
で始まるファイルはrm
に--
オプションを付与して、その後に-
で始まるファイルを指定することで削除できます。
NOTE
The rm command uses getopt(3) to parse its arguments, which allows it to accept the `--' option which will cause it to stop processing flag options at that point.
This will allow the removal of file names that begin with a dash (`-'). For example:
rm -- -filename
The same behavior can be obtained by using an absolute or relative path reference. For example:
rm /home/user/-filename
rm ./-filename
(man rm参照)
以下の-
で始まるファイルを削除します。
% ls | head
-
Applications
Desktop
Documents
Downloads
Dropbox
Fiddler2
Library
Movies
Music
マニュアル通りrm
コマンドの--
の後に-
で始まるファイルを削除します。
% rm -rf -- -
おわりに
以上、単なるメモでした。コマンドオプションの使いかたをもっときちんと覚えるだけでやれること増えるなーと思いました。