Remove Directory Linux: A Guide to Deleting Files and Folders

Remove Directory Linux: A Guide to Deleting 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 managing the system’s data. In this article, we will explain how to remove files and directories efficiently using the Linux command line. 

We will also explain how to simplify Linux management tasks using Hostinger’s VPS built-in features. For example, you will learn how to get full root access using the Browser terminal and generate commands with our AI Assistant.

How to Remove Files and Folders in Linux With Hostinger VPS

Hostinger offers various features that help system administrators easily manage their remote server files. All our VPS plans provide full root access via SSH, allowing users to execute various administrative commands without permission issues.

Users can connect to the VPS using a third-party SSH application like PuTTY or Terminal. For beginners unfamiliar with both applications, we also offer a built-in Browser terminal that allows users to run commands remotely via web browsers.

The Browser Terminal location in hPanel's VPS management menu

Since this feature is web-based, it works across all operating systems. To access the tool, open your web browser and follow these steps:

  1. Log in to the Hostinger Members Area and click VPS on the top menu.
  2. Select the VPS plan in question.
  3. Click Browser terminal on the right side of the VPS Dashboard. 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 the remote system’s files and folders. Hostinger also offers an AI Assistant, which lets you utilize various AI prompts for VPS management to generate Linux commands or instructions quickly.

If you don’t want to use a command-line interface, Hostinger VPS hosting plans also support different operating systems with various control panels. Users can install them in one click via the Operating System menu in the VPS Dashboard.

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 task easier, especially for new VPS users.

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 -iPrompt for confirmation before each removal.
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, Terminal, or Hostinger’s Browser terminal. Then, execute the commands from the application.

Before running the deletion command, ensure you have a restore point or data reserve in case of accidental removal. Hostinger VPS plans include free weekly automatic backups, enabling you to protect files when running risky commands.

We also offer a free snapshot to help you easily restore your VPS configuration to the previous state. Accessible via the VPS Dashboard, this feature provides a graphical user interface, making it easy to use for beginners.

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.

Suggested Reading

Check out our guide to learn more about how to create and restore a backup on Hostinger’s VPS hosting.

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 critical files. Before proceeding, check all the files in the directory using the ls command to ensure there are no important ones.

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.

Moreover, use Hostinger’s AI Assistant to quickly generate the necessary commands or instructions. If you prefer working with a graphical user interface, install a control panel via our VPS Dashboard.

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.

Which Task Removes a Folder and Its Contents Completely?

To completely remove a folder and its contents in Linux, you can use the rm command with the -r or -rf option.

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 Nugraha

Leo is a Content Specialist and WordPress contributor. Armed with his experience as a WordPress Release Co-Lead and Documentation Team Representative, he loves sharing his knowledge to help people build successful websites. Follow him on LinkedIn.