Breaking News

How to delete a file/directory using rm and rmdir commands in Linux Operating System

Deleting a file or directory in Linux is very simple and it will take few steps!
I'm using a Kali Linux but don't worry, any version apart from this Kali, the command can work.

  • Open your Linux Machine
  • Now Open terminal (This will be used to write a command that will Delete a file/directory)

  • On Terminal, navigate to where your file/directory has been created and you need to delete. Example, I need to delete my file/directory located on Desktop, I will type the following command.
  • cd /root/Desktop (This will change the current directory to Desktop)

    • Now type the following command while you are at Desktop and press enter.
    • rm File  (rm is a command that will delete a file called File located at Desktop)
    • If you need to be asked for confirmation of deleting or not in every deletion, use switch -i and you will confirm by typing n or y representing no or yes respectively. Switch (Options) is a part in Linux Command that is used to modify the behavior of a command, and it always start with negative sign (-).

      • As you can see, a file has been deleted from Desktop. Now let us delete a directory called Emp_Dir. This directory is an empty so we use rmdir command to delete it. Type the following command and press enter.
      • rmdir Emp_Dir (rmdir is a command, stand for remove directory which will delete a directory called Emp_Dir located at Desktop)

      • On the above Example, we deleted an empty directory with rmdir command. This command only works to delete an empty directory but if a directory is a parent (not empty) we can't use rmdir command, instead we use rm command with switch -r (-r stand for recursive). This switch will help to delete a directory and its contents. Example, a directory named Folder is not empty, so if I will use rmdir command to delete it, an error will occur.

      • So I have to type the following command to delete a parent (not empty) directory.
      • rm -r Folder (Folder is a name of my directory, look on the above example on picture)

      • We have done!! As you can see now my Desktop is empty!!

      Please leave a comment and share with your friends!!
      Thank you!!

      No comments