How to Delete a File in Linux?Deleting a file is necessary when it will no longer be used. There may be other reasons for deleting a file from your system on a Linux operating system. In this tutorial you will be provided with several ways to delete a file in Linux. In addition, we will also see a command to delete a file or folder and all of its contents. Deleting a file in a Linux OS is a basic task that every user should be aware of. When we delete a file from the device, we remove its source from the file system. The original file is not permanently removed from the storage device, but the location is marked for reuse. There are various commands and GUI methods to delete a file in Linux OS. Let's examine them one by one. Using GUIMany Linux distros have GUI features that allow you to easily delete a file without executing any commands. To delete a file using graphical user interface, go to the location where the file is located, right-click on the file you want to delete and select the Move to Trash option. Another option: You can delete the file using the keyboard keys. For this, select the file you want to delete and click the Delete button from your keyboard. Use rm CommandUse the rm command to delete files and directories in Linux. Change the directory where your files are kept to change the directory using the cd command. For example, the files that I need to delete are kept in the Documents folder so that the command will be: Command: Here, replace /path/to/directory with your directory name. For example: cd Documents Similarly, you need to enter the directory name wherever your files are kept, such as on your Desktop, Home or in another folder. Go with the below-given command to delete the desired file. Command In the above command, replace the filename with the actual name of the file you want to delete. Example Here, we want to delete a single file named demo.txt. rm demo.txt Note: If you are deleting a file but that is protected, at that time a prompt will show there and will ask you to confirm for deleting that file.Options:-To not get a confirmation message for deletion of a file. You can force delete without prompt by using the -f option: Command Example rm -f demo.txt Use the "-i" flag with the rm command to see a confirmation before deleting a file: Command Example rm -i sample.txt The rm command does not show messages when deleting a file. To see a message, use the rm command with the -v flag. Command Example rm -v demo.txt It will show what has been deleted, i.e., removed 'demo.txt' To remove a directory named demodir and all its contents, use the below command: Command Example rm -r sampledir Delete File by Combining the OptionsYou can combine multiple options for more complex operations: Command Example rm -rfv demodir Delete Multiple FilesTo delete multiple files at once, use the below command: Command Example rm demo1.txt demo2.txt demo3.txt Delete Files Using WildcardsYou also have an option to delete multiple files with the same extension. For example, if you have more than one .txt file to delete, use the following command: Command Example rm *.txt Delete a File with Root PermissionsIf you want to delete a file that requires root permissions, go with the sudo: Command Example sudo rm demo.txt Using shred CommandThe shred command is another way to delete files. It overwrites the file's contents multiple times before deleting them, which makes it difficult for everyone to recover the deleted file. To delete a file, type the following command in the terminal: Command Example shred -u demo.txt Here, replace the filename with the actual file name that you need to delete. The -u option is enabling shred command to delete the file after overwriting it. Using trash-clithe trash-cli command will move deleted files to the trash instead of permanently deleting them. To proceed with the trash-cli command, you must install it first. To install, execute the command given below: Command After installing trash-cli, you can delete a file using the following command: Command In the above command, replace the filename with the name of the file you want to delete from the file manager. Using unlinkThere is another method to delete a file in Linux OS called unlink. Using this command, you can delete only one file at a time. This command does not allow you to pass multiple arguments. Use the cd command to go to the directory where you have kept the files. For example, cd Documents. Now, use the unlink command to delete the file. Command Example unlink demo.txt Now, verify that the file has been deleted using the ls command: Locate and Delete FilesNow, we will see how to locate and then delete the files. To delete all files in a path specified by {dir-to-search} that follow a pattern {pattern}. Command Example find /home/vivek/Documents -type f -name "demo.txt" -exec rm -f {} \; To delete all files with the same file extension at once, use the following command. Command Example find /home/vivek/Documents -type f -name "*.txt" -exec rm -f {} \; To find and delete files in a directory that match the pattern, you can use the '-depth' and then The '-delete' action to remove any matching files. This command allows you to avoid unintentional data loss. Command Example find /home/vivek/Documents -type f -name "*.txt" -depth -delete You can use the find command with the -empty option to find and delete empty files within a specific directory. Command Example Here, I want to find and delete all empty files within the /home/vivek/Documents directory; the command will be: find /home/vivek/Documents -type f -empty -delete Deleting Files with Specific PermissionsTo delete files with specific permissions, you can combine the -perm option with the -delete action. Command Example Here, I want to find and delete all files with permissions 644 within /home/vivek directory: find /home/vivek/ -name "demo*" -perm 644 -delete Merge the OptionsYou can combine and run multiple options in a single line of command. For example, to find and delete files, the command will be: Command Example To find and delete files named example.txt with permissions 644 within /home/vivek: find /home/vivek/Documents -type f -name "demo.txt" -perm 644 -delete Using -execFor more control over the deletion process, you can use -exec instead of -delete flag: Command Example To find and delete all files with permissions 644 within /home/vivek: find /home/vivek -type f -perm 644 -exec rm {} \; Remove DirectoriesYou can easily remove a directory using the rm command. Here are all the commands that you need. To remove an empty directory, run the rmdir command in the terminal: Command Example rm mydir Including ContentsIf the directory has included content and you want to remove the directory along with content, use the -r or -R option with the rm command. To remove a directory named mydir and all its contents: Command Example rm -r demodir Remove a Directory ForcefullyIf the directories are protected and you want to remove them. You can use the -f including the -r option to remove the directory along with contents without seeing a confirmation message: Command Example rm -rf mydir To see the message that you have deleted at the time of removing a directory, use the -v option with -r: Command Example rm -rv mydir Combine the OptionsTo forcefully and recursively remove a directory named backup with verbose output: Command ConclusionDeleting a file in Linux can be done using the rm, shred and unlink commands. However, there are many other options that you can utilize, such as finding and deleting files. Also, you can use specific terms like deleting the file by checking if it is empty or not; if the file is password protected, you can delete the file using special permissions. You can also delete the file by conforming it with a yes/no option if the file is crucial and you are going to accidentally delete it. These options, which can be very important to beginners as well as professional Linux users, are provided in this tutorial.
Next TopicSteamOS
|