Sep 2, 2023 4 min read

How to Delete Files and Directories on Linux

Delete Files and Directories on Linux with our step-by-step tutorial. Deleting files & directories permanently removes them from storage.

Delete Files and Directories on Linux
Table of Contents

Introduction

Before we begin talking about how to delete files and directories on Linux, let's briefly understand – What is deleting files and directories?

"Delete files and directories" refers to the act of permanently removing files and folders from a computer or storage device. When you delete these items, they are no longer accessible and take up no space. This action is crucial for organizing data and freeing up storage capacity. Whether you're looking to remove unwanted files or cleanup cluttered folders, deleting files and directories is a fundamental process for managing digital content efficiently.

Please note that the files and directories deleted using the commands mentioned in this tutorial are deleted permanently and can only be recovered with a backup.

In this tutorial, you will delete files and directories on Linux. We will also address a few FAQs on how to delete files and directories on Linux.

How to Remove Files

You can use either the rm (remove) or unlink command to delete files. With unlink command you can only delete a single file, while with the rm you can delete multiple files at once.

  • Use the following command to delete a single file:

Using unlink

unlink filename

Using rm

rm filename

In case the file is write-protected, you will get a prompt for confirmation. Click Y and hit Enter to remove the file. If in case the file is not write-protected, it will be deleted without a prompt.

Ouput

rm: remove write-protected regular empty file 'filename'?

Remove (Delete) Multiple Files

  • Use the following command to delete multiple files at once:
rm filename1 filename2 filename3
  • You can also use regular expressions and wildcard (*) to find all .pdf files and remove them:
rm *.pdf
  • To confirm every file before deleting, add the -i option:
rm -i filename(s)
  • You can force delete files even if they are write-protected with -f (force) flag:
rm -f filename(s)
  • rm file doesn't show any messages while deleting the files. You can use the -v flag to know what the rm command is doing currently:
 rm -v {file-name}

Delete the Archive

  • You can use the -d flag to delete an empty directory:
$ rm -d {dir-name}

Locate and Delete Files

  • Locate command comes in handy when you have more complicated specifications. You can specify a path and follow a search pattern:
find {dir-to-search} -type f -name {pattern} -exec rm -f {} \;
  • Find command supports the delete feature. The delete flag can override the rm flag, while the -depth flag tells find to process the directory contents apart from the directory itself:
find {dir-to-search} -type f -name {file-name-pattern} -depth -delete

Deleting Hidden and Non-Hidden Files

Any file that starts with a dot character is treated as a hidden file on Linux. If you want to see all the hidden files, use -a with ls:

ls -a
  • If you want to remove all files apart from the hidden files, use either one of the below commands:
rm /path/to/dir/*
rm -rf /path/to/dir/*
rm *
  • To delete all files along with the hidden files, use the following command:
rm -rf /path/to/dir1/{*,.*}
rm -rfv /path/to/dir1/{*,.*}

How to Remove Directories (Folders)

Use the rm or rmdir commands to delete the directories.

With rmdir, you can only delete the empty directories. Use rm to delete the directories and their contents recursively.

  • Delete an empty directory with rmdir or rm -d followed by a directory name:
rm -d dirname
rmdir dirname
  • Use the -r flag to delete a non-empty directory:
rm -r {dir-name}
  • If you want to delete the non-empty directory, use the rm command with the -r flag, which will delete the files recursively:
rm -r dirname
  • To delete a non-empty directory without a prompt, use rm with -r recursive and -f flags:
rm -rf dirname

FAQs to Delete Files and Directories on Linux

Can I recover a deleted file on Linux? 

If you have not permanently deleted the file (using the -r option with rm), you can potentially recover it from the trash or by using data recovery tools. Otherwise, deleted files without a backup are generally irrecoverable.

Can I delete multiple files at once on Linux? 

Yes, you can delete multiple files at once on Linux using the rm command with the file names separated by spaces. For example, rm file1.txt file2.txt will delete both "file1.txt" and "file2.txt".

How do I delete an empty directory on Linux? 

To delete an empty directory on Linux, use the rmdir command followed by the directory name. For example, rmdir mydir will delete the empty directory named "mydir".

How can I delete a directory and its contents on Linux? 

To delete a directory and its contents recursively, you can use the rm command with the -r (or -rf) option. For example, rm -r mydir will delete the directory "mydir" along with all its files and sub-directories.

What happens when I delete a file/directory on Linux? 

When you delete a file or directory on Linux, it moves to the trash (or recycle bin) unless you use the rm command with the -r option to remove it permanently. You can recover files from the trash if needed.

How can I delete a hidden file on Linux? 

Hidden files in Linux start with a dot (e.g., .myfile.txt). To delete a hidden file, use the rm command followed by the file name. For example, rm .myfile.txt will delete the hidden file named ".myfile.txt".

What permissions do I need to delete a file or directory? 

To delete a file, you need write permission on the file itself. To delete a directory, you require write permission both on the directory and its parent directory.

Conclusion

We hope this detailed guide helped you understand how to delete files and directories on Linux.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.