How to delete files and folders using Linux command line
In this tutorial, TipsMake will show you how to delete files and folders using the command line on Linux. This tutorial is compatible with all Linux distributions, so it works the same way on Ubuntu, Debian, CentOS, AlmaLinux, Rocky Linux, etc.
Delete a file on Linux
In Linux, the rm command is used to delete files and directories. Navigate to the specific directory that contains the file you want to delete. Specify a location otherwise the command will start searching in the current working directory. For example, the author of this article has a file in the /tmp/ directory that he wants to delete. To delete the desired file, open Terminal and enter the following command:
# rm file.txt
Be careful while deleting files and folders from Linux because once deleted they cannot be recovered. So it will ask you to confirm before deleting the file:
# rm –i file.txt
If you don't want to see a confirmation message for deletion, use the following command:
# rm –f file.txt
The confirmation message will not appear.
Delete multiple files on Linux
To delete multiple files on Linux, the same rm command can be used.
# rm file.txt file1.txt file2.txt
Delete a directory on Linux
To delete a directory on Linux, the same command is used. But you need to add the -r and -f options to delete a directory.
# rm –rf /data
But be careful, this will delete the entire directory, including all the files and directories inside. You can use the above command without -f , as it will not prompt for confirmation. The -r option is used to delete a directory.
If you just want to delete an empty directory, use this command instead:
rmdir /data
The command will show an error in case the directory is not empty.
Summary
In all Linux distributions, the rm command is used to delete files and directories.
- If -i is used with rm, you will be prompted for confirmation before deleting.
- If -r is used with rm, the directory will be deleted without confirmation.
See also:
You should read it
- How to create and delete folders in Linux Terminal
- How to Delete Read-Only Files on Linux
- How to copy (and paste) files and folders from the Linux command line
- A simple way to delete a folder with a long name on Windows
- How to use the Linux command line on Android with Termux
- 12 things Linux is easier to do in the command line than graphical software
- Find and delete all .DS_Store files with a single command
- The Cat command in Linux
May be interested
- Microsoft Edge Game Assist is now available, what's new?back in late november 2024, microsoft announced edge game assist—a new feature that makes it easier to browse the internet while playing games on your computer.
- Samsung Galaxy Watch can now be customized for kids who don't yet have smartphonesthe answer lies in the galaxy watch for kids setting, which allows parents to share the smartwatch experience with their children without worry.
- This hidden setting in Windows will make videos play smoother!whether you're a streamer or enjoy diving into your personal video library curated over the years, customizing video playback settings on your windows 11 pc can significantly improve your multimedia experience.
- How to enable password leak detection on Chromepassword leak detection on chrome is a security feature that notifies users if any passwords are at risk of being leaked or in a data breach.
- How to fix iPhone email not updatingif your iphone email isn't updating, there could be a number of issues. these include incompatible system settings, software issues, differences in email protocols, etc.
- Google will let users control Chromebooks with their facesgoogle is announcing a number of chromeos features focused on the classroom and accessibility, and one of the standout features is the ability to control computers with your head and facial expressions.