{"id":21137,"date":"2023-09-27T08:04:24","date_gmt":"2023-09-27T08:04:24","guid":{"rendered":"https:\/\/linuxways.net\/?p=21137"},"modified":"2023-09-27T08:04:24","modified_gmt":"2023-09-27T08:04:24","slug":"how-do-you-delete-a-file-in-ubuntu-22-04-using-terminal","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-do-you-delete-a-file-in-ubuntu-22-04-using-terminal\/","title":{"rendered":"How do you Delete a File in Ubuntu 22.04 Using Terminal?"},"content":{"rendered":"<p>In Ubuntu, files and directories can be <strong>deleted <\/strong>using either the Graphical User Interface (GUI) or the Command Line Interface (CLI). Using the CLI to delete files is more powerful as with CLI we can run additional scripts while deleting a file or directories.<\/p>\n<p>This article explains how files can be deleted in Ubuntu using the Terminal.<\/p>\n<h2><strong>How do you Delete a File in Ubuntu 22.04 using Terminal?<\/strong><\/h2>\n<p>Files can be deleted in Ubuntu using different methodologies. Deleting a file or directory will remove the reference to that file or directory.<\/p>\n<p>Files can be deleted in Ubuntu Terminal using the methods mentioned below:<\/p>\n<ul>\n<li>Using \u201crm\u201d Command<\/li>\n<li>Using \u201cshred\u201d Command<\/li>\n<li>Using \u201ctrash-cli\u201d Command<\/li>\n<\/ul>\n<h2><strong>Method 1: Use the \u201crm\u201d Command to Delete a File<\/strong><\/h2>\n<p>The \u201crm\u201d stands for \u201cremove\u201d. It is used to remove a file or a directory in Ubuntu using the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">rm<\/span> <span class=\"sy0\">&lt;<\/span>filename<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>Use the file you want to delete in place of the &lt;filename&gt;. Here in the directory, there are three files:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"149\" class=\"wp-image-21138\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-1.png 903w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-1-300x50.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-1-768x127.png 768w\" sizes=\"auto, (max-width: 903px) 100vw, 903px\" \/><\/p>\n<p>To delete the file \u201cnamed1.pdf\u201d, the command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">rm<\/span> named1.pdf<\/div><\/div>\n<p>This will remove or delete the file from the directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"292\" class=\"wp-image-21139\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-2.png 800w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-2-300x110.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-2-768x280.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<h3><strong>Using \u201crm\u201d to Delete Files With a Specific Name<\/strong><\/h3>\n<p>Apart from providing the name of a file, the \u201crm\u201d command can also delete files with specific names. For this purpose, utilize the following syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">rm<\/span> <span class=\"sy0\">*&lt;<\/span>filename<span class=\"sy0\">&gt;*<\/span><\/div><\/div>\n<p>Having files in the directory with the same name can be deleted at once using the rm command. In the example illustrated below, we have three files \u201csame1.txt\u201d, \u201csame2.txt\u201d and \u201csame3.txt\u201d with \u201csame\u201d being the common name:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"770\" height=\"129\" class=\"wp-image-21140\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-3.png 770w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-3-300x50.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-3-768x129.png 768w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/p>\n<p>To delete all three use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">rm<\/span> <span class=\"sy0\">&lt;<\/span>strong<span class=\"sy0\">&gt;*<\/span>same<span class=\"sy0\">*&lt;\/<\/span>strong<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>All three files will be deleted:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"855\" height=\"258\" class=\"wp-image-21141\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-4.png 855w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-4-300x91.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-4-768x232.png 768w\" sizes=\"auto, (max-width: 855px) 100vw, 855px\" \/><\/p>\n<h3><strong>Using \u201crm\u201d to Delete Files of Specific Type<\/strong><\/h3>\n<p>With the \u201crm\u201d command, a specific type of file can be deleted. The example below showcases three ODT files with the extension \u201c.odt\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"751\" height=\"155\" class=\"wp-image-21142\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-5.png 751w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-5-300x62.png 300w\" sizes=\"auto, (max-width: 751px) 100vw, 751px\" \/><\/p>\n<p>To remove these files with the extension specified in the \u201crm\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">rm<\/span> <span class=\"sy0\">*<\/span>.odt<\/div><\/div>\n<p>This will remove all the files having an extension \u201c.odt\u201d, as demonstrated below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"245\" class=\"wp-image-21143\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-6.png 831w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-6-300x88.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-6-768x226.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/p>\n<h2><strong>Method 2: Use the \u201cshred\u201d Command to Delete a File<\/strong><\/h2>\n<p>The \u201cshred\u201d command is more powerful and useful as the files once deleted or removed cannot be recovered. \u201cshred\u201d overwrites the file\u2019s content multiple times before removing it. This example below utilizes the \u201cshred\u201d command to remove the \u201cnamed2.pdf\u201d file:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">shred<\/span> <span class=\"re5\">-u<\/span> named2.pdf<\/div><\/div>\n<p>The \u201c-u\u201d will ensure to delete the file after rewriting it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"265\" class=\"wp-image-21144\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-7.png 796w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-7-300x100.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-7-768x256.png 768w\" sizes=\"auto, (max-width: 796px) 100vw, 796px\" \/><\/p>\n<p>Once the shred command executes, it will overwrite and make two new files. When you will open the directory again these files will also be removed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"156\" class=\"wp-image-21145\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-8.png 773w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-8-300x61.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-8-768x155.png 768w\" sizes=\"auto, (max-width: 773px) 100vw, 773px\" \/><\/p>\n<h2><strong>Method 3: Use the \u201ctrash-cli\u201d Command to Delete a File<\/strong><\/h2>\n<p>The \u201ctrash-cli\u201d command will ensure the safe deletion of files by moving those files to the Trash first rather than deleting them permanently. \u201cTrash-cli\u201d packages are not included in Ubuntu and have to be installed manually using the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">apt-get install<\/span> trash-cli<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"362\" class=\"wp-image-21146\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-9.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-9-300x148.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>You can now delete a file using the \u201ctrash-cli\u201d command. The example below deletes the file \u201cnamed3.pdf\u201d using the trash command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">trash named3.pdf<\/div><\/div>\n<p>It will delete the file from the directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"790\" height=\"307\" class=\"wp-image-21147\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-10.png 790w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-10-300x117.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-10-768x298.png 768w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><\/p>\n<p>To verify if the file is moved to trash, open the Trash Application in Ubuntu:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"742\" height=\"667\" class=\"wp-image-21148\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-11.png 742w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-11-300x270.png 300w\" sizes=\"auto, (max-width: 742px) 100vw, 742px\" \/><\/p>\n<p>In the Trash, you can see the file exists there, after removing it using the \u201ctrash\u201d command:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"893\" height=\"197\" class=\"wp-image-21149\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-12.png 893w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-12-300x66.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-12-768x169.png 768w\" sizes=\"auto, (max-width: 893px) 100vw, 893px\" \/><\/p>\n<h2><strong>Bonus Tip &#8211; Removing a Directory from Ubuntu<\/strong><\/h2>\n<p>Using the \u201crm\u201d command, a directory can also be deleted. To remove a directory from Ubuntu, use \u201crm\u201d or \u201crmdir\u201d. The \u201crmdir\u201d command deletes an empty directory. The \u201crm\u201d command deletes a directory and all the subdirectories as well as files.<\/p>\n<h3><strong>Using \u201crm\u201d to Delete a Directory<\/strong><\/h3>\n<p>Consider the following directory \u201cFolder\u201d having files inside of it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"882\" height=\"142\" class=\"wp-image-21150\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-13.png 882w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-13-300x48.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-13-768x124.png 768w\" sizes=\"auto, (max-width: 882px) 100vw, 882px\" \/><\/p>\n<p>To remove the whole directory named \u201cFolder\u201d with all of its content. To do that, open the Terminal using \u201cCtrl+Alt+T\u201d, and navigate to the Documents folder in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"316\" class=\"wp-image-21151\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-14.png 850w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-14-300x112.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-14-768x286.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>To remove the \u201cFolder\u201d directory use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">rm<\/span> <span class=\"re5\">-r<\/span> Folder<\/div><\/div>\n<p>Here \u201c-r\u201d is for the recursive operation:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"232\" class=\"wp-image-21152\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-15.png 840w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-15-300x83.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-15-768x212.png 768w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/p>\n<h3><strong>Using \u201crmdir\u201d to Delete a Directory<\/strong><\/h3>\n<p>The \u201crmdir\u201d command deletes an empty directory. In the below-explained example, an Empty Folder named \u201cEmpFolder\u201d exists in the Document Directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"759\" height=\"134\" class=\"wp-image-21153\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-16.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-16.png 759w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-16-300x53.png 300w\" sizes=\"auto, (max-width: 759px) 100vw, 759px\" \/><\/p>\n<p>To delete this empty folder, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">rmdir<\/span> EmpFolder<\/div><\/div>\n<p>This will remove the folder from the directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"847\" height=\"255\" class=\"wp-image-21154\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-17.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-17.png 847w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-17-300x90.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21137-17-768x231.png 768w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>To delete a file in Ubuntu 22.04 using Terminal, first, open the terminal, and use the \u201crm\u201d, \u201cshred\u201d or \u201ctrash-cli\u201d commands. The terminal offers more power to delete a file or a folder and the \u201crm\u201d command is used mostly for deleting a file. This article discussed how we can remove specific files and folders through the Ubuntu terminal using different commands.<\/p>","protected":false},"excerpt":{"rendered":"<p>The terminal is mostly used for deleting a file or a folder as it offers more flexibility. The \u201crm\u201d command is used mostly for deleting a file. <\/p>","protected":false},"author":110,"featured_media":21155,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-21137","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=21137"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21137\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/21155"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=21137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=21137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=21137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}