How to Use Delete Command in Unix

In this tutorial we will show you how to use the unix delete command in Linux. Any files or extension in a directory can be removed through this unix delete file command.
In Linux based operating systems, the rmdir command is used to remove the directory. One important aspect of this command is that it will only remove the directory if it is empty.


Step 1 – List the contents

Let’s look at an example. Let’s list the contents of the directory here, using the “ls” command.

List the directory contents

Step 2 – Insert delete command

Over here, let’s delete the directory named Myscripts. For that, we will type the command
rmdir Myscripts and hit enter.
With that done you will see the error being generated that the directory could not be deleted because it was not empty.

Enter delete command

Step 3 – Remove directory contents

Now let’s remove the contents of the directory, for that first open up the directory by using the command.

cd Myscripts.

Remove contents from directory

Step 4

Now type the command dir and hit enter. With that done, you will see all the files present in the directory. Notice how all the file extensions in this folder are the same.

Step 5 – Delete a particular file extension

If you want to delete a particular file extension, you can add an attribute along with the “rm” command. Let’s see how it’s done using this unix delete command.
Type the command “rm” *.sh followed by the file extension. This will remove all files which have the .sh extension.

Delete a specified file extension

Step 6 – Return back to directory

Now let’s return back to directory where we previously were.

Go back to the directory

Step 7 – Insert the command

With that done, let’s type the command “rmdir -v Myscripts” and hit enter. The -v tag which we have used here is to see the output of the directory which is being processed.

Enter the command

Step 8 – Check if the folder has been removed

Now let’s check whether the directory has been removed or not. For that, let’s list the contents of the directory.
And you can see that the folder has been removed successfully by using the unix delete file command.

See if the folder has been removed