Linux 4 All

Best Linux tricks source

Posts in the rm category

rm -rf is gone

It looks like you cannot run rm -rf as root on CentOS or RHEL6 boxes.

# cat /etc/redhat-release 
CentOS release 6.5 (Final)
# rm -vrf /               
rm: it is dangerous to operate recursively on `/'
rm: use --no-preserve-root to override this failsafe
# 

Don’t try this on a RHEL5 boxes.

There are multiple ways of removing a file that starts with a dash sign (-).

1. Use — option to signal the end of options and disable further options processing

[root@linux4all test]# ls -l
-rw-r--r-- 1 root root 0 Nov  5 23:35 -rf file with space

[root@linux4all test]# rm -f -- -rf\ file\ with\ space

[root@linux4all test]# ls -l
total 0
[root@linux4all test]#

2. Put the full file path between single quotes.

[root@linux4all test]# ls -l
total 0
-rw-r--r-- 1 root root 0 Nov  5 23:43 -rf file with space

[root@linux4all test]# rm -f './-rf file with space'

[root@linux4all test]# ls -l
total 0
[root@linux4all test]#

This is one question you are most likely to get in a job interview, so next time you’ll be prepared.