Click here for "Safe Air Conditioner Repair and Proper Freon Recovery".

Deleting files that cannot be normally deleted on Linux

Deleting files that cannot be normally deleted on Linux

To delete a file, run the rm command.
However, files whose names begin with "-" or contain spaces or metacharacters (meta-characters) cannot be deleted because the shell cannot recognize them correctly.

However, in Windows and MacOS, files and directories can be created, so if a file is shared or uploaded, a file with such a name may exist in Linux.

In this case, if the name contains a hyphen "--", you can either delete the file by adding the "--" option to the rm command and specifying the file containing the hyphen as an argument, or you can prefix the file name with". /" at the beginning of the file name to delete it.

Files containing spaces can be removed by enclosing the file name in quotation marks '' or double quotation marks "", or by prefixing the space with an escape sequence.

●Deleting files that cannot be deleted by normal processing on Linux

[root@Lion ~]# ls
-prive.txt sample index html.txt     ← There are "-prive.txt" and "sample index html.txt".


[root@Lion ~]# rm -prive.txt  ←Perform normal process deletion

rm: different options -- 'l'      ←error message and cannot delete
Try `rm ./-prive.txt' to remove the file `-prive.txt'.
more information, please run `rm --help'..

[root@Lion ~]# rm sample index html.txt  ←Perform normal process deletion
rm: cannot remove `sample: no such files or directories
rm: cannot remove `index': no such files or directories
rm: cannot remove `html.txt': no such files or directories
↑error message and cannot delete
[root@Lion ~]# rm -- -prive.txt  ←delete with the "--" option

[root@Lion ~]# ls
sample index html.txt    ← "-prive.txt" has been deleted.
[root@Lion ~]# rm This\ is\ filetest.txt  ←Remove spaces preceded by an escape sequence
[root@Lion ~]# ls
[root@Lion ~]#                        ← sample index html.txt has been deleted

●Delete hyphenated files with ". /" to remove them

[root@Lion ~]# ls
-prive.txt      ←

[root@Lion ~]# rm ./-prive.txt ←add ". /" to delete
[root@Lion ~]# ls
[root@Lion ~]#                        ←The file has been deleted

●Remove files containing spaces by enclosing them in double quotation marks "".

[root@Lion ~]# ls
sample index html.txt
[root@Lion ~]# rm "sample index html.txt "   ← Enclose in double quotation marks "" and delete
[root@Lion ~]# ls
[root@Lion ~]#                        ←The file has been deleted
タイトルとURLをコピーしました