How to Remove/Delete a files and directory in linux

“rm” command is frequently used one but very dangerous. If you use wrongly rm, this will Remove/Delete everything and you can’t stop it. so keep in mind while using rm command.

Syntax for rm command

rm [option] [file-name or directory-name]

-f >> ignore non existent files, never prompt
-i >> interactive, prompt before any removal
-r -R >>recursive, remove directories and their contents recursively
-v >> verbose, explain what is being done

To delete empty folder use the below command.

rmdir [option] [empty-directory-name]

Now, i’m going to delete a “test” directory in my server. use below command.

# rmdir test

If a directory is not empty you will be get an below error

# rmdir test
rmdir: failed to remove `test': Directory not empty

To delete a file in linux, use below command

# rm -i index.php

To delete a directory/folder in linux, use below command. If you are beginner use this format because this will popup all the files/folders will be removed.

# rm -Ri test
rm: descend into directory `test'? yes
rm: remove regular file `test/index.php'? yes
rm: remove directory `test'? yes

To delete a directory/folder in linux, use below command, If you expert use this format

# rm -Rf test

Note : If you wrongly deleted the files using “rm” command, there is no way to recover a file, so make sure whether the files/folder not use before deleting it.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...