CentOS is a popular and highly regarded community-driven Linux distribution. It provides a stable and secure platform for servers, desktops, and applications. One of the key features of CentOS is its robust file system, which is organized according to a standard directory structure.
This tutorial will cover the basics of deleting file and directory on Linux CentOS. Whether you're new to the Linux operating system or need a refresher, this guide will help you easily understand the process. Additionally, we also cover the methods on how to recover deleted files on Centos.
Part 1. Command to Delete Files and Directories on CentOS Linux
To remove files from CentOS Linux, use the rm and unlink commands listed below. But first, launch the terminal program (bash shell).
- To delete the file in the current directory, use the following commands.
● If no absolute path is specified, the rm command will search for the file in the current working directory. Alternatively, specify the file path you want to remove.
● When removing files in CentOS with the rm command, be extra careful because there is no command to undo it.
The safest action is to use -i, which will prompt you for confirmation before removing the file. It is the default behavior in CentOS when deleting files; the rm command will ask for confirmation even if the -i option is not used.
rm -i filename
- However, if you don't want to be prompted before deleting files, use the -f This time, the rm command will not ask for confirmation.
rm -f filename
Let's look at all the rm command options for deleting files on CentOS Linux.
Part 2. Examples to Delete File(s) on CentOS Linux Using rm
Delete a Single File on CentOS Linux
To delete a single file on Centos Linux, enter the rm command.
- Assume you want to delete “file7”. Then, you’ll need to type the following command and hit Enter.
- When prompted for confirmation, type y, and press Enter for yes.
Delete a File that Begins With a Dash or Hyphen
- Use the following syntax if the name of a file, directory, or folder begins with a dash (-) or hyphen (—):
rm -- -filename
rm -- --filename
rm -rf --dirname
rm -rf ./--dirname
Delete Multiple Files on CentOS Linux
- To delete multiple files, such as foo.txt, bar.doc, and dance.jpg, in the current directory, use the following command:
rm foo.txt bar.doc dance.jpg
- You can also specify a path. If there is a file named foo.txt in the /tmp/ directory, you can run the following:
rm /tmp/foo.txt /home/vivek/dance.jpg /home/vivek/data/bar.doc
Delete a File and Prompt Before Every Removal
Run the rm -i command option on CentOS Linux to obtain confirmation before removing each file.
- For example:
rm -i filename
rm -i foo.txt
Delete a File and See the Summary
To delete the file and view the summary, use the command rm -v.
- For instance, to delete “file8” without prompting for confirmation and to see the summary, type the following command and press Enter.
rm –vf file8
- Check the summary to see if "file8" has been deleted.
Delete All Files in a Folder or Directory on CentOS Linux
- To delete all files in a folder or directory on CentOS Linux, you must select the following options:
rm -rf dir1
rm -rf /path/to/dir/
rm -rf /home/vivek/oldschoolpics/
- It deletes all files and subdirectories in a directory. So be cautious. Keeping backups of all important data on CentOS Linux is highly recommended.
Part 3. Examples to Remove Directory on CentOS Linux Using rm
Remove a Non-empty Directory
To remove non-empty directories on Centos Linux, use the rm -r command.
- For example, to delete a non-empty directory “dir3”, enter the following command:
rm –r dir3
- It will prompt you for confirmation before descending to all available objects in dir3 and removing them. Type y for YES and press Enter.
Remove Empty Directories
To remove empty directories, use the rmdir command.
- As an example, to remove the “Directory DIr5”, run the following command:
Remove Directories Without Asking for Confirmation
To delete without prompting the terminal, use the rm -f command.
- For example, to delete “file6”, type the command and press Enter. You will notice that "file6" is deleted without your confirmation.
rm –f file6
Note: DO NOT run the “rm -rf /” command as an administrator/root or normal CentOS Linux user because it is frequently used in anecdotes about CentOS Linux disasters. If run by an administrator, the rm -rf / variant of the command would delete the contents of every writable mounted filesystem on the computer.
- Try not to run the following commands on CentOS Linux:
rm -rf /
rm -rf *
Part 4. How To Recover Deleted Files on CentOS Linux
However, sometimes accidents happen, and you may accidentally delete an important file. In such cases, it's good to know how to recover deleted files on CentOS Linux. One of the best tools for this task is Recoverit Linux File Recovery. Recoverit assists you in restoring files quickly and easily without causing further damage to your system or your files.
Recoverit Linux Recovery is an advanced and powerful tool with features designed to make file recovery on CentOS Linux fast, simple, and effective.
Wondershare Recoverit - Your Safe and Reliable Linux Recovery Software
5,481,435 people have downloaded it.
Recovers lost or deleted documents, photos, videos, music, emails, and other 1000+ file types effectively, safely, and completely.
Compatible with all mainstream Linux distros, including CentOS, Ubuntu, Linux Mint, Debian, Fedora, Solus, Opensuse, Manjaro, etc.
Assists in 500+ data loss scenarios, such as deletion, disk formatting, OS crash, power outage, virus attack, lost partition, and many more.
The simple point-and-click interface allows you to recover data from Linux hard drives in just a few clicks.
Works through a remote connection. You can recover lost data even when your Linux device is crashed.
The following section will demonstrate how to recover deleted files, folders, and directories using Recoverit Linux Recovery software. You can also watch a comprehensive tutorial video to better understand the process.
For Windows XP/Vista/7/8/10/11
For macOS X 10.10 - macOS 13
Getting your lost data back with Recoverit can be completed in three steps.
Step 1: Select Linux Recovery
First, download and install Recoverit on your Linux computer. Then, launch the program and select NAS and Linux from the main interface. Click on the Linux Recovery button to proceed.
Step 2: Connect the Linux Computer
In the next window, enter the necessary information to establish a remote connection. Once done, click Connect. The connection will be established, and Recoverit will automatically scan for lost files.
Step 3: Preview and Recover
The scanning process may take some time, depending on the amount of data being scanned. You can stop scanning anytime when you find the file you want to recover.
After scanning, you can preview the files and select the ones you wish to recover. Click Recover and select a path to save the restored files.
Hit the Recover button again to complete the process. It's recommended to choose a different path than where the original data was lost.
Except for Wondershare Recoverit, many other methods exist to recover deleted files in Linux. Feel free to check them out.
In conclusion, deleting files and directories on Linux CentOS is a simple process that can be accomplished using the different commands discussed above. Before proceeding with any deletion, it's advisable to double-check the files or directories you wish to delete and to make a backup of any important data if necessary. In case you delete the wrong files, Recoverit Linux Recovery will come to help you. Download it now!