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:

  • The following commands should be used with care, especially those that employ regular expressions or search patterns with the "Find" command. As a consequence of an improper phrase or pattern, data/system and non-intended files are deleted.
  • Having a current copy of vital data and device files is always good.
  • Run these commands with extreme care, especially if you're using root (super-user privileges) and Sudo.

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 EaseUS Data Recovery Wizard to recover your Deleted data.

 Download for Win Recovery Rate 99.7%
 Download for Mac Trustpilot Rating 4.7

EaseUS Data Recovery Wizard 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.

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

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, EaseUS 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.

choose the recovered data type

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. 

recover windows 10 lost data

Main Features of the EaseUS Data Recovery Wizard:

  • Recover data from hard disks, SSDs, SD cards, flash drives, and USB devices
  • Repair corrupted files after recovery, support documents, video, and photo repair  
  • Preview restored files before 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 EaseUS Data Recovery Wizard 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!

 Download for Win Recovery Rate 99.7%
 Download for Mac Trustpilot Rating 4.7

Was This Page Helpful?

Updated by Brithny

Brithny is a technology enthusiast, aiming to make readers' tech lives easy and enjoyable. She loves exploring new technologies and writing technical how-to tips. In her spare time, she loves sharing things about her game experience on Facebook or Twitter.

Read full bio

Approved by Evan Galasso

Evan Galasso is a digital forensics and data recovery engineer with over 10 years of experience in the field. He presents opinions on the current state of storage media, reverse engineering of storage systems and firmware, and electro-mechanical systems of SSDs and HDDs.

Read full bio