How to use rm and rmdir commands in Linux?

If the users have to remove or delete any file or directory in Linux or Unix-like Operating Systems, users use the rm command or rmdir command. This segment of Unix articles explains how to delete a particular file on a Linux or Unix like machine using the Command Line Interface (CLI).

1. Using rm Command

rm command stands for REMOVE. It is a Unix/Linux command which is used to remove or delete any file or document from the system’s filesystem.

The rm utility  removes  the  directory  entry  specified  by  each file argument. If a file is  a  symbolic  link,  the  link  will  be  removed,  but  the  file  or  directory  to  which  it  refers  will  not  be deleted. If  multiple files  are  specified  and  removal  of  a file fails  for  any  reason, rm will  write  a  diagnostic  message  to  standard  error,  do  nothing   to  the  current file and  go  on  to  any  remaining files.

rm [option] File_Name

rm – Name of the command

[option] – It is a modifier that changes the manner in which the command works.

File_Name – It is the file you want to remove.

Main [options] which can be used with rm command are given below :

A. Suppose you have a file named etechwall.txt and you want to remove it.

B. Suppose you want to delete multiple files. You can do it this way.

C. Suppose you want to remove all files and subdirectories from a directory. The option ( -r ) for removing directories and their contents recursively and option ( -f ) for ignoring nonexistent files and arguments, you can use them together in this way.

D. Suppose you want to remove multiple directories, their all files and sub-directories. you can use the same above command in this way.

E. Suppose you want to request confirmation before attempting to remove each file. The option ( -i ) for prompting before every removal, you can use it in this way.

F. Suppose you want to see rm command to explain what is being done with the file. The option ( -v ) for explaining what is being done, you can use it in this way.

G. Suppose you want to delete empty directories. The option ( -d ) for removing empty directories, you can use it in this way.

H. Suppose you want to delete all files with a particular extension (like pdf, txt, doc etc.) in the current directory. The wildcard character ( * ) to match multiple files, you can use it in this way.

For more options, See the Manual Page.

2. Using rmdir Command

The command used to delete empty directories in Linux is rmdir.

The rmdir utility will  remove  the  directory  entry  specified  by  each dirname operand,  which  must  refer to  an  empty  directory. Directories  will  be  processed  in  the  order  specified.  If  a  directory  and  a  sub directory  of  that  directory  are specified  in  a  single  invocation  of rmdir,  the  sub directory  must  be  specified  before  the  parent  directory so  that  the  parent  directory  will  be  empty  when rmdir tries  to  remove  it.

rm [option] Directory_Name

rmdir – Name of the command

[option] – It is a modifier that changes the manner in which the command works.

Directory_Name – It is the directory you want to remove.

Main [options] which can be used with rmdir command are given below :

A. Suppose you want to remove a directory using the rmdir command which is used to remove empty directories, if the directory is not empty, it will give you the error. You can use it in this way.

In the same way, you can also delete multiple directories using rmdir by separating their names by spaces.

B. Suppose you want to remove a directory which has empty subdirectories. The option ( -p ) for removing directory and its ancestors, you can use it in this way.

For more options, See the Manual Page.

More on Related Topics :

1. Understanding the History, Versions, and Main Components of UNIX

2. How to Disable IPv6 Leak on Linux ?

3. Understanding the System Startup and Remote Login in UNIX

Biplab Das

Biplab Das

My name is Biplab Das. I’m a writer, Blogger, Youtuber and full time IT support engineer whose childhood obsession with science fiction never quite faded. A quarter-century later, the technology that I coveted as a kid is woven into the fabric of everyday life. I’ve spent the past years to learn these technologies, I recently published a book on computer science fundamentals. People say smartphones are boring these days, but I think everyone is beginning to take this wonderful technology marvel for granted.
Keep Digging Technology.

Sign up for our Newsletter

Sign Up for our Newsletter for daily Updates !

shares