How to rm or Delete file in Linux ?

rm Stands for remove. This command is used to remove a file. You should be careful using this command. Once the rm command deletes a file, it deletes it permanently.

Syntax of rm command :

$ rm <file name>

Example of rm :

$ rm test1

Now we have removed file test1 permanently with the help of ‘rm’ command.

rm Command can understand the *, ., ..; So we can do like

$ rm *

This example deletes all files in the present directory

$ rm log*

Above the example deletes all files, whose name starts with log

$ rm *.txt

The above example deletes all files, whose name ends with .txt

$ rm ~/*

The above example removes all files in the home directory.

Not that, the above examples will not work, if the target directory has child directories. To delete all the child directories too, we need to use the -r option.

For example

$ rm -r /var/logs/

This deletes all files and child directories in the /var/logs directory.

Fair note: use the -r option with caution.

If you want to be asked before deleting, use -i option.

For example:

$ rm -ir /var/logs/

This command asks every time, it attempts to delete something in /var/logs directory.

Leave a Reply