Operations 3 min read

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.

Open Source Linux
Open Source Linux
Open Source Linux
How to Safely Delete Files Starting with a Dash in Linux

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 -test

These 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 0

Both files are now removed.

Source: Linux Command Manual
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

file managementcommand-linerm
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.