How to Use Command to Delete File in Linux Terminal

Deleting files or directories is a necessary task when you are working with Linux. To delete a file or directory in Linux, you are able to use the rm command or unlink command. The rm command deletes each specified file. By default, it does not delete directories. Also, it does not work on directories which contain files.

The rm command (short for delete) is a Linux command which is used to delete files from a file system. Usually, on most file systems, removing a file needs write permission on the parent directory and execute permission, in order to submit the directory in the first place.

How to Use Command to Delete File in Linux Terminal

You need to be careful with the rm command as it does not ask you for confirmation when deleting files. Be sure that you really want to delete your files before you use rm.  When the files are gone, they are not coming back. Remember that the multi-user nature of Linux does not offer for file recovery as in DOS.

As soon as you let go of the space concerned by a file, the operating system is likely to utilize it for something else. Of course, Linux command lines are absolutely important for every system administrator and advanced Linux user. Today we are going to explain how to delete files and directories in Linux using the Command.

THE RMDIR COMMAND

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

The basic syntax of this command is very easy to conceive. Here is the basic syntax of the rmdir command:

rmdir [option] DirectoryName

  • rmdir is the command.
  • [option] is an optional modifier which changes the way the command acts.
  • DirectoryName is the directory you want to delete.

If no option is provided, easily rmdir command removes the directory whose name is provided as the destination. Before using the rmdir command, you have to log into your VPS server by using SSH. Here is a text to help you out.

DELETE DIRECTORIES IN LINUX USING THE RMDIR COMMAND

Before you use the rmdir command, you need to check the files present in a directory with the ls command. Say that you have a directory called Dir1.

rmdir Dir1

This command is going to delete the empty directory called Dir1. Also, you are able to delete multiple directories by separating their names by spaces. For example:

rmdir Dir1 Dir2 Dir3

After running this command, the directories called Dir1, Dir2 and Dir3 are going to be removed. Now, let’s say that you have a directory called Dir3. Remember that Dir3 has subdirectories and files inside of it. If you use rmdir Dir3 command, then you are going to get an error like this: rmdir: failed to delete dir3: Directory not empty.

As we said before that rmdir only works with empty directories. The rmdir command is a smart utility. It only offers you to remove empty directories as a built-in safeguard to prevent accidental loss of data.

Please remember it is almost impossible to recover removed data on any Linux distribution. The -p option allows you to remove the directory as well as its parent directories.

The command below is going to remove Dir3 and its parent directories Dir2 and Dir1.

rmdir -p Dir1/Dir2/Dir3

For your information, the -v option outputs a diagnostic text for every directory processed. If you use this option, it will be able to output a confirmation listing all the directories that were removed.

 THE RM COMMAND

The rmdir command is nice for safely deleting unused and empty directories. If you want to delete files or directories containing files, you are able to use the rm command.

The basic syntax of the rm command is similar to rmdir command:

rm [option] FileName

REMOVE FILES IN LINUX USING THE RM COMMAND

Use the rm command to delete the file named article.txt:

rm article.txt

If you have a directory with the name Dir1 containing subdirectories and files, you have to attach the -r modifier. The command would look like this:

rm -r Dir1

The -r option recursively deletes directories and their content.

Another helpful option is -i. This is going to ask you to confirm the files that will be removed individually. Doing that you are able to avoid any unpleasant mistakes. Also, you are able to delete empty directories by using -d option. The following command is going to delete an empty directory with name Dir1:

rm -d Dir1

You are able to use a wildcard (*) and a regular expansion to match multiple files. For example, the command below will be able to delete all pdf files placed in the current directory.

rm *.pdf

Aside from that, you are able to utilize all the commands above to delete files with other extensions such as .odt, .txt, .doc, etc.

The -f option allows you forcefully remove everything placed in a directory. The command would look like this:

rm -rf Dir1

The above command will be able to remove everything recursively and forcefully, residing under the Dir1 directory without prompting anything on the terminal.

If you want to delete more than one directory at a time, you are also able to do that. The command below will be able to delete three directories Dir1, Dir2 and Dir3 in a single command.

rm -rf Dir1 Dir2 Dir3

Finally, you mastered all the basic functions of the rm and rmdir commands successfully.

The last word, we are going to remember you that in Linux removing a single file by accident is able to lead to major problems. That is why it is essential to learn and master the two main commands for file and directory deletion, rm and rmdir commands. In this page, we cover those two commands and the various options which can be used with them. We hope you found this page useful. Once again remember, when you delete a file or directory from Linux, you cannot recover it, so be extra careful.

Leave a Reply

Your email address will not be published. Required fields are marked *