rmdir and rm command

Linux rmdir and rm command examples

In this section, we will look at more on how to remove a file or directory on the Linux/UNIX flavor machines.

Let’s start with rmdir command.

This is used for removing of an empty directory. An empty directory means, having no files and subdirectories in it.

Syntax:

rmdir directory-name

 

rmdir command execution,

[rreddy@abclearn abclearn_dir1]$rmdirabclearn_dir2/

 

In practical, we don’t use this rmdir command so frequently. Main reason is,
Most of the times, every directory will be having at least few files and subdirectories under it. So to use this command, we have to manually eliminate each and every file and subdirectory of it. which is a tedious task.

The best solution is using “rm command” with “-rf” option.

 

rm command:

rm command is primarily used to remove single or multiple files under a directory.

Syntax:

rm file-name

 

Let’s understand some of its practical usages with examples.

 

Example-1:

Removing a particular file:

Removing a file is a straightforward requirement and there are so many of the cases for this.

Some practical usage cases,

We have worked on a sample file and want to delete after its usage.

We might find a corrupted configuration file and want to remove it and copy a new file from backup.

Syntax:

rm filename

 

sample rm command output is given below,

[rreddy@ebclearn ebclearn_dir1]$rm abc_lab1.txt

 

The one problem is, it will be removing the file without even prompting and once the file is deleted in Linux/UNIX flavor OS, we can’t pull it back.

So always best practice is to prompt for deletion of a file.

 

rm command with “-i” option, will prompt the user whether to delete a file or not.

[rreddy@abclearn abclearn_dir1]$rm -i abc_lab2.txt
rm: remove regular file ‘abc_lab2.txt’? y

 

Example-2:

Force removing a file:

We can remove the file using "-f" command with rm command it won't prompt for confirmation to delete it or not.

Syntax:

rm -f filename

 

Observe the following case for the proper understanding.

[rreddy@abclearn abclearn_dir1]$rm -f abc_lab2.txt

 

Example-3:

Removing multiple files at a time:

For removing multiple files, we will use rm command followed by file names

Syntax:

rm -i file-name1 file-name2

 

Observe the following usage for more understanding.

[rreddy@abclearn abclearn_dir1]$rm -i abc_lab2.txt abc_lab3.txt

 

For example:

If we want to delete all files under a particular directory using rm command, we can use directory name followed by a star as indicated below.

$ rm -rf Directory_Name function selectElementText(el){ var range = document.createRange(); range.selectNodeContents(el); var selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); } var field = document.getElementById('textcopy'); selectElementText(field); var succeeded; try { succeeded = document.execCommand("copy"); } catch (e) { succeeded = false; } var tooltip,hidetooltiptimer; if (succeeded) { tooltip = document.createElement('div'); tooltip.style.cssText = 'position:absolute; background:black; color:white; padding:4px;z-index:10000;' + 'border-radius:2px; font-size:12px;box-shadow:3px 3px 3px rgba(0,0,0,.4);' + 'opacity:0;transition:opacity 0.3s'; tooltip.innerHTML = 'Copied!'; document.body.appendChild(tooltip); showtooltip(evnt); } function showtooltip(evnt){ var evt = evnt || event; clearTimeout(hidetooltiptimer); tooltip.style.left = evt.pageX - 10 + 'px'; tooltip.style.top = evt.pageY + 15 + 'px'; tooltip.style.opacity = 1; hidetooltiptimer = setTimeout(function(){ tooltip.style.opacity = 0; }, 2000) } }); }