How to Safely Delete Files Starting with a Dash in Linux
This guide shows why files whose names begin with a dash cannot be removed with a regular rm command and demonstrates the correct use of the '--' option to delete such files safely on a Linux system.
First, notice the two special files whose names start with a dash.
[root@node_119 test]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 3 08:59 -rf *
-rw-r--r-- 1 root root 0 Aug 3 09:00 -testThese files cannot be removed with the usual rm command because the leading dash is interpreted as an option.
[root@node_119 test]# rm '-rf *'
rm: invalid option -- ' '
Try 'rm ./'-rf *'' to remove the file ‘-rf *’.
Try 'rm --help' for more information.
[root@node_119 test]# rm -rf\ \*
rm: invalid option -- ' '
Try 'rm ./'-rf *'' to remove the file ‘-rf *’.
Try 'rm --help' for more information.To delete them, prepend -- before the filename to signal the end of options.
[root@node_119 test]# rm -rf -- '-rf *'
[root@node_119 test]# rm -rf -- -test
[root@node_119 test]# ll
total 0Both files are now removed.
Source: Linux Command Manual
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
