Delete Files, Folder, and Directories in Linux Using Terminal/Command - Qiling  

How to Delete Files, Folders, and Directories in Linux Using Command


PAGE CONTENT:
How to Delete a File in the Linux Terminal
How to Remove Folders and Directories in Linux with Command Lines
How to Recover Deleted Files, Folders, and Directories in Linux

Learning how to utilize the Linux command line is a must-have skill for all Linux users and system administrators. Simple maintenance duties, such as creating a file, deleting a file, browsing a folder, and transferring a file, may be completed using the program.

Due to the popularity of Linux as an operating system for virtual server hosting, being familiar with the core Linux commands is essential for server administration. This article will teach you how to delete a file in Linux using the Terminal command line.

how to delete a file in Linux

How to Delete a File in the Linux Terminal

Keep in mind the following tips before proceeding with the file deletion in Linux:

1. Delete a Single File Using rm Command

The rm function, which allows you to delete one or more files simultaneously, is the most often used command for eliminating files from your computer.

rm {file-name}

delete a file in Linux using rm commands

Step 1. Write-protected files will be asked for confirmation before being deleted; otherwise, rm will remove the file without asking for confirmation. With the "-i" flag, you may compel rm to ask you for confirmation before it removes a file from your computer.

rm -i {file-name}

delete a file in Linux using rm-i commands

Step 2. The rm command removes files from the system without displaying any notifications. In order to view what the rm command is presently doing, it is necessary to use it in conjunction with the -v flag.

rm -v {file-name}

delete a file in Linux using rm-v commands

Step 3. Using the -f flag to delete write-protected files without first requesting an explanation is not recommended.

rm -f {file-name}

delete a file in Linux using rm-f commands

2. Remove Files by Employing unlink

Here is how you can deploy the "unlink" command when deleting a file permanently using Linux.

$ unlink {file-name}

delete a file in Linux using unlink commands

How to Remove Folders and Directories in Linux with Command Lines

The rmdir and rm commands in Linux allow you to remove or delete folders. Removing empty directories using the rmdir command-line program while removing directories with their contents with the rm command-line utility is recursive.

Step 1. Use the rm command with the -r (recursive) option to completely delete non-empty folders and all of the files contained inside them:

rm -d dirname

Step 2. If you attempt to delete a directory or a file inside a directory that is write-protected, you will be requested to confirm your decision:

rmdir dirname

Step 3. Use the rm command with the -r (recursive) and -f (forceful) arguments to delete non-empty folders and all of the files without being prompted:

rm -r dirname

Step 4. Use the rm -r command followed by a space and the directory names you want to delete to remove several directories at the same time.

rm -rf dirname

Step 5. In the same way you can use a wildcard (*) to match multiple files, you can also use normal expansions to match multiple directories.

rm -r dirname1 dirname2 dirname3

Important to Remember: When the command rmdir returns an error with the message "Directory not empty," it stops processing the directories that were supplied to it on the command line. It would not accomplish anything if you asked it to remove four directories, and the first one contained files.

rmdir would give you an error message and would not do anything else. You may compel it to ignore these problems by using the —ignore-fail-on-non-empty option, which will allow other directories to be processed while these errors are ignored.

How to Recover Deleted Files, Folders, and Directories in Linux

Deleting files is always risky. Sometimes you end up deleting important files that you never wanted to delete in the first place. The same can happen when you are employing all these commands in Linux. So, how can you recover these lost files? Regardless of the failure, we suggest that you restore the game data. You will need expert help to achieve this, and we suggest that you utilize the Deep Data Recovery to recover your Deleted data.

Deep Data Recovery makes it simple to recover deleted files or data on Linux, including MX Linux, Manjaro, Linux Mint, Ubuntu, Debian, and more. If any files, folders, or directories on Linux are accidentally destroyed, users may effortlessly recover them using the software. Using this application, users may recover lost data from a Linux computer running on a Windows PC.

Note: Linux Recovery is a new feature of Deep Data Recovery, included in the latest version. You can contact our online customer service to get the installation package.

Step 1. Install and Launch Deep Data Recovery on your Windows PC. Choose "Linux Recovery" on the left panel. 

Select file types that you want to recover

Step 2. This software will detect your IP and Port automatically. You need to input the user name and password. If the IP address or Port is not right, you can correct it manually. Then, click "Connect Now". 

choose linux recovery

Step 3. After successfully connecting to your Linux device, Qiling recovery software will start scanning the disk to find all lost and deleted files. When the scan process completes, click "Filter" > "Type" to specify lost file types.

Recover lost/deleted data

Step 4. You can preview the scanned files first. Then, select the target data and click the "Recover" button to restore lost files in Linux. 

Main Features of the Deep Data Recovery:

Conclusion

The rm, rmdir, and unlink Linux commands should be familiar to you at this point, and you should be able to properly delete files and folders from the command line using these commands after reading through this how to Delete a File in Linux guide.

The Deep Data Recovery may be used to recover accidentally deleted data from any Linux distribution, including Ubuntu, Fedora, Debian, and Slackware. Make sure you use the software to your advantage when in need!

Related Articles


Is this information helpful?     

What can we do to improve this information? (Optional)
Refresh Please enter the verification code!