If you’re managing servers, especially Virtual Private Servers (VPS), learning Linux commands is important because they help you manage the server more effectively.
While you can use a control panel with buttons and menus, Linux commands are quicker and more powerful for tasks like deleting files and organizing data.
Therefore, in this post, we will discuss How to use Linux commands to delete files and directories.
How to Remove Files and Folders in Linux with WPOven VPS
If you’re using WPOven’s VPS, you have full control over the server, so you can easily manage files and perform tasks using SSH, without restrictions.
Wondering How? We have a dedicated blog in this regard you must read: How To Use SSH To Connect To Your Server Securely
In case you want to generate an SSH key, utilize our Free SSH key generator tool.
How to remove a directory in Linux? (For all users)
To permanently delete a folder in Linux, you can use two commands: rmdir or rm.
- rmdir or rm -d is for deleting empty folders.
- rm -r is for deleting folders that have files inside them.
Before Deleting:
- Know the folder name: You need to know the exact name of the folder you want to delete.
- Check your location: Use the command pwd to see where you are in the system, and ls to see a list of files and folders.
Command Options:
- rm -d: Deletes an empty folder.
- rm -r: Deletes a folder and everything inside it.
- rm -f: Deletes a protected file without asking.
- rm -rf: Deletes a folder and everything inside it without asking.
- rm -i: Asks for confirmation before deleting each item.
- rm -I: Asks for confirmation only once before deleting more than three files.
- rm *: Deletes multiple files.
- rm ?: Deletes files that match a single character.
- rmdir -p: Deletes an empty folder and its parent folder.
- rmdir -v: Shows a message when a folder is deleted.
Using Commands:
To use these commands, connect to your server using SSH. You can do this with a special program or the terminal on your computer.
Note: Always make sure you have a backup before deleting anything, just in case you accidentally remove something important.
How to Delete an Empty Folder in Linux
In Linux, when you delete files or folders, they don’t go to a recycle bin or trash like on some other systems. This means that if you accidentally delete something, you can’t get it back unless you have a backup saved somewhere.
Using the rmdir Command
To help avoid such mistakes, you can use the rmdir command to delete only empty folders. If you try to delete a folder that has files in it, you’ll see this error message:
**”rmdir: failed to remove ‘Directory’: Directory not empty.”**
How to Use the rmdir Command
– The basic way to use the command looks like this:
rmdir DirectoryName
For example, to delete a folder called **Simple-Directory**, you would type:
rmdir Simple-Directory
– You can also delete multiple empty folders at once by listing them:
rmdir Directory_1 Directory_2 Directory_3
Extra Options for rmdir
– **-p**: This option allows you to delete a subfolder and its parent folder if both are empty. For example:
rmdir -p /Directory/SubDirectory
This will delete the **SubDirectory** and also remove its parent **Directory** if it’s empty.
– **-v**: This option gives you a confirmation message that the folder has been successfully deleted. For example:
rmdir -v Simple-Directory
The message will say something like **”rmdir: removing directory, ‘Simple-Directory’”** to confirm the deletion.
How to Delete a Folder with Files in It
To delete a folder that has files in it, you use the **rm** command. While this command is mainly for deleting files, you can add options to also remove folders. Here’s how to use it:
Basic Command Syntax
The basic way to write the command is:
rm option FileOrFolderName
Using the -r Option
– To delete a folder and everything inside it (including all files and subfolders), you use the **-r** option, which stands for “recursive.” For example:
rm -r Simple-Directory
This command will delete **Simple-Directory** and everything inside it.
**Warning**: Be careful when using this command! Deleting a folder with **rm -r** will also delete all its contents. You can only get them back if you have a backup.
Confirming Deletion
– If the folder you’re trying to delete is write-protected (meaning it has some protection against deletion), the system will ask you to confirm before deleting anything.
– If you want to delete a folder without being asked for confirmation, you can use the **-rf** option:
rm -rf Simple-Directory
This will delete the folder and all its contents without asking you first.
Deleting Empty Folders
– You can also delete empty folders using the **rm** command with the **-d** option:
rm -d Simple-Directory
Deleting Multiple Folders
– Similar to the **rmdir** command, you can delete multiple folders at once using **rm** by listing them all:
rm -r Directory_1 Directory_2 Directory_3
How to Delete a File in Linux using rm command
If you want to remove a specific file instead of deleting an entire folder, you can use the **rm** command. This method is safer because it helps avoid accidentally deleting everything in a folder.
Deleting a Single File
To delete one file in the current directory, you just type:
rm file.txt
Deleting Multiple Files
You can also delete several files at once by listing their names:
rm file1.txt file2.txt file3.txt
**Note**: These commands only work if you are in the folder where those files are located. If the file is in another folder, you can specify the path:
rm dir/subdir/file.txt
Adding Confirmation to Prevent Accidental Deletion
Since this command permanently deletes files, you can add the **-i** option to ask for confirmation before deleting:
rm -i file1.txt file2.txt file3.txt
When you run this command, the system will ask if you want to delete each file. Type **Y** and press Enter to delete or **N** to cancel.
If you want to confirm only once when deleting more than three files, use the **-I** option:
rm -I file1.txt file2.txt file3.txt
Skipping Confirmation
If you want to delete a file without being asked for confirmation, use the **-f** option:
rm -f file.txt
Using Wildcards to Delete Multiple Files
You can use wildcards to delete many files at once:
– **Asterisk (*)**: Represents multiple characters. For example, to delete all text files:
rm *.txt
This command will remove all files with the **.txt** extension in the current directory.
– **Question Mark (?)**: Represents a single character. You can use it with the asterisk to delete files with specific extensions. For example:
rm *.?
This command will delete files that have a single-character extension.
Warning About Wildcards
Be very careful when using wildcards because you might accidentally delete important files. Before running a command with wildcards, use the **ls** command to see what files are in the directory to ensure you aren’t deleting anything important.
Conclusion
Deleting or removing files and folders using Linux commands is not a big deal; all you have to do is understand the basic functionality of the rm and rmdir commands first. The rm command allows you to delete files and folders, while the rmdir command allows you to delete only empty folders.
This is not the end; if you are familiar with other essential Linux commands, there are many things you can do with files.
If you have any queries or suggestions regarding this topic, please let us know in the comment section below.
Rahul Kumar is a web enthusiast, and content strategist specializing in WordPress & web hosting. With years of experience and a commitment to staying up-to-date with industry trends, he creates effective online strategies that drive traffic, boosts engagement, and increase conversions. Rahul’s attention to detail and ability to craft compelling content makes him a valuable asset to any brand looking to improve its online presence.