Remove Directory in Linux: How to Delete Files and Folders

Remove Directory in Linux: How to Delete Files and Folders

Learning the Linux command line is essential for system administrators, as it’s widely used for virtual private server hosting. While you can use a hosting control panel, server management is more efficient with Linux commands.

These commands are useful for various basic tasks, like creating or moving a file or folder.

This article will explain how to remove files and directories using the Linux command line. We will also explain how to do it efficiently in Hostinger’s VPS plans using hPanel’s features.

How to Remove a Directory in Linux

To permanently remove a directory in Linux, use either the rmdir or rm command. The rmdir or rm -d command is for removing empty directories, while the rm -r command deletes non-empty directories.

Before removing a directory, you must know its name. To discover files and directories, use the ls command. To know the current working directory, use the pwd command.

The options you use with these commands also determine how they work. Here’s a quick recap of rm command options:

Command and OptionDescription
rm -dRemove an empty directory using the rm command.
rm -rRemove a non-empty directory and its content.
rm -fIgnore any prompt when deleting a write-protected file.
rm -rfIgnore any prompt when deleting a write-protected non-empty folder.
rm -iOutput a prompt before deleting every file.
rm -IOutput a prompt only once before deleting more than three files.
rm *Wildcard that represents multiple characters.
rm ?Wildcard that represents a single character.
rmdir -pRemove an empty subdirectory and its parent directory.
rmdir -vPrint the information that the specified directory was deleted.

To use these commands for VPS management, connect to your remote server using an SSH client or Terminal. Then, execute the commands from the application.

How to Remove an Empty Directory Using the rmdir Command

Linux environment doesn’t have a recycle bin or trash folder, so you can’t restore deleted files and directories. If you accidentally delete a file or directory, the only recovery method is restoring a backup.

Using the rmdir command prevents such accidents since it only works for empty directories. It will return the following error message if the directory contains files:

rmdir: failed to remove ‘Directory’: Directory not empty

The rmdir command syntax is as follows:

rmdir option DirectoryName

You may use the rmdir command without an option by removing it from the syntax. Since the command line is case-sensitive, remember to type the folder name accordingly.

For example, here’s a command for deleting the Simple-Directory folder:

rmdir Simple-Directory

The rmdir command also lets you delete multiple empty directories simultaneously. To do so, add the directories as additional arguments after rmdir. The syntax looks as follows:

rmdir Directory_1 Directory_2 Directory_3

There are various options you can combine with the rmdir command. For example, use -p to delete a subdirectory and its parent.

For example, the following command will delete the /Directory/SubDirectory directory path:

rmdir -p /Directory/SubDirectory

The above command will delete the SubDirectory folder in the Directory path. Then, it will also delete the Directory folder if it is empty.

The next option is -v or verbose. This option tells the command line interface to print a confirmation message to verify that rmdir has successfully deleted the specified directory. Here’s an example of this command:

rmdir -v Simple-Directory

The output message will appear similar to the following:

rmdir: removing directory, ‘Simple-Directory’

How to Remove a Non-Empty Directory Using the rm Command

Use the rm command to remove non-empty directories. This command is meant for removing files, but we can combine it with options like -r, -rf, and -d to delete a directory. Here’s the syntax for the command:

rm option FileOrFolderName

Use the -r or recursive option to remove the directory and its content. For example, the following command will delete Simple-Directory, including the subdirectories and files within it:

rm -r Simple-Directory

Warning! Deleting a directory using the rm -r command will also wipe its content. Execute this command with caution since you can only recover them using a backup.

If the directory is write-protected, the command line interface will request confirmation before deleting. To delete a directory in Linux without confirmation, use the -rf option.

rm -rf Simple-Directory

You can also use the rm command to remove empty directories in Linux. However, you need the -d option.

rm -d Simple-Directory

Similar to the rmdir command, you can use rm to remove multiple directories. Add the directory names as new arguments in the command line:

rm -r Directory_1 Directory_2 Directory_3

How to Delete a File in Linux With the rm Command

You may want to use the rm command to manually remove files instead of deleting the folder. This method is safer as it prevents accidental file removals.

To delete a single file in the current working directory, use the rm command with the file name:

rm file.txt

You can also delete multiple files in the directory by listing their names:

rm file1.txt file2.txt file3.txt

All these commands only work if you are in the specified files’ directory. However, you can put the file path as an argument to delete a file in another folder:

rm dir/subdir/file.txt

Since this command will permanently delete your files, add the -i option to enable removal confirmation. It adds an extra step to prevent accidental deletion. Here’s the command:

rm -i file1.txt file2.txt file3.txt

Terminal will prompt you with an option to proceed or cancel the deletion. Type Y and press Enter if you want to delete the file. Otherwise, hit N and Enter.

Alternatively, use -I if you want the confirmation for deleting more than three files to show only once. While this option is less safe than -i, it is still relatively effective to prevent accidents. Here’s what the command looks like:

rm -I file1.txt file2.txt file3.txt

The system will still prompt the confirmation if you are removing write-protected files. To skip the confirmation prompts, use the -f option:

rm -f file.txt

Instead of deleting specific files individually, you can use wildcards to remove multiple items with a single command. There are two types of wildcards – the asterisk (*) and the question mark (?).

The asterisk represents multiple unknown characters, commonly used to delete files with a certain extension. For instance, the command below will delete all TXT files in the current working directory:

rm *.txt

You can also use the asterisk to delete all files beginning with a specific letter.

rm a*

In the example above, the asterisk represents all unknown characters following the letter a. The command will delete all files beginning with an a, regardless of their extensions, such as amazon.txt, alligator.png, and aaron.zip.

Meanwhile, the question mark wildcard represents a single character. Combining it with the asterisk wildcard lets you delete files with a single character extension, such as S, O, and C. Here’s an example command:

rm *.?

Warning! Be careful when using wildcards, as you may end up accidentally deleting important files. Before proceeding, check all the files in the directory using the ls command to ensure there are no important ones.

How to Remove Files and Folders With Hostinger VPS

Hostinger offers various features that help system administrators easily manage their remote server files. In addition to SSH support, our VPS Hosting plans also offer various control panels and a built-in Browser terminal.

The Browser Terminal location in hPanel's VPS management menu

A control panel provides a graphical user interface (GUI) that helps administrators manage their VPS files efficiently without executing commands. The visual interface makes the process easier, especially for new VPS users.

If you prefer using commands, Browser terminal lets you run them without Terminal or SSH applications. This feature works the same regardless of your VPS operating system or control panel.

To access it, open your web browser and follow these steps:

  1. Open hPanel and click VPS on the top menu.
  2. Select the VPS plan in question.
  3. Click Browser terminal on the right side of hPanel. A browser pop-up asking for your permission may appear.
  4. The command line interface will open. Enter your username and password to connect.

Once connected, you can run bash commands to manage your remote system’s files and folders. To assist you, Hostinger also offers a GPT-powered AI Assistant, which lets you generate commands based on simple prompts.

Conclusion

To delete files or directories on a Linux system, you must understand the rm and rmdir commands. The rm command lets you delete files and folders, while rmdir is only for empty directories.

You can attach various options with them, such as -rf, -i, and -l. Since Linux permanently deletes the files, create a backup and use the -i option to enable confirmation before removal.

System administrators commonly execute these commands via Terminal or SSH Client. At Hostinger, you can use the Browser terminal to run them directly via your web browser. Alternatively, use a hosting control panel to delete files without commands.

Linux Remove Directory FAQ

In this section, we will answer a commonly asked question about removing directory in Linux.

How to Delete All Files and Folders in Linux?

For empty folders, use either the rmdir or rm -d commands followed by the folder’s name. The rm command with the -r dir option can also delete folders with content.

Author
The author

Ignas T.

Ignas is a part of Hostinger's Technical team. He takes care of server administration and monitoring with a particular focus on software virtualization. In his free time, he's always up for a skiing or kayaking adventure or a board game night.

Author
The Co-author

Leonardus N.

Leo is a WordPress fanatic and contributor. He likes keeping up with the latest WordPress news and updates, and sharing his knowledge to help people build successful websites. When he's not working, he contributes to WordPress documentation team and pampers his dogs.