Linux rm with examples

In this article, we are going to see Linux rm command with examples. rm remove files or directories.

Note: Once file(s) or directories removed using rm or rm -rf cannot be recovered after. Be cautious.

Syntax:

rm [OPTION]... [FILE]...

Options: These are available at the bottom of the article.

1. Files in the current directory.

[root@master test]# ls abc.sql adduser.sh logins.dat tested victory ab.txt login.dat script.sh tt.txt

2. To remove one file at a time.

[root@master test]# rm abc.sql rm: remove regular empty file 'abc.sql'? yes

3. To remove more than one file:

[root@master test]# rm tt.txt ab.txt rm: remove regular empty file 'tt.txt'? yes rm: remove regular empty file 'ab.txt'? yes

4. remove file forcibly

rm -f login.dat

5. Remove a directory which is not empty.

Try to remove the directory using rm.

[root@master test]# rm tested/ rm: cannot remove 'tested/': Is a directory

Solution is we have to use -r (recursive) and f options with rm command.

[root@master test]# rm -rf tested/

Linux rm with examples

Options

-f, –force
ignore nonexistent files and arguments, never prompt

-i prompt before every removal

-I prompt once before removing more than three files, or when removing recursively; less intrusive than -i,
while still giving protection against most mistakes

–interactive[=WHEN]
prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always
–one-file-system
when removing a hierarchy recursively, skip any directory that is on a file system different from that of
the corresponding command line argument

no-preserve-root
do not treat ‘/’ specially

–preserve-root[=all]
do not remove ‘/’ (default); with ‘all’, reject any command line argument on a separate device from its par‐
ent

-r, -R, –recursive
remove directories and their contents recursively

-d, –dir
remove empty directories