Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to Delete Files and Directories in Linux?

  • Last Updated : 18 May, 2021

Linux comes with several tools that can assist us in removing files. We always need to delete many files and folders based on a set of requirements. To complete our mission quickly, knowing a few basic commands and their variations is beneficial.

  • Use caution when using the commands below, particularly those that use regular expressions or search patterns with the find command. An incorrect expression or pattern will result in the deletion of important data/system files and non-intended files.
  • Often have a current copy of critical data and device files.
  • Use caution when running those commands, particularly if you’re using Sudo or as the superuser (root).

1. Remove file by using “unlink”:

Not so well-liked. We may use the unlink command to permanently delete a single file.

$ unlink {file-name}

In Linux, how can I delete files and directories?

2. Delete a single file:

The rm command, which facilitates deleting one or more files simultaneously, is a more widely used command for removing files.

$ rm {file-name}

How To Delete Files And Directories In Linux



If the file is write-protected, rm will ask you to validate its deletion; otherwise, it will delete it without prompting. Using the “-i” flag to force rm to prompt for confirmation before removing a file:

$ rm -i {file-name}

How To Delete Files And Directories In Linux

The rm command deletes files without showing any messages. Using the rm command with the -v flag to see what the rm command is currently doing.

$ rm -v {file-name}

How To Delete Files And Directories In Linux

Using the -f flag to remove write-protected files without asking for clarification.

$ rm -f {file-name}

How To Delete Files And Directories In Linux

3. Multiple files can be deleted:

Bypassing multiple filenames as arguments to rm, you can delete multiple files.

$ rm {file-name-1} {file-name-2} {file-name-3} ... {file-name-N}

How To Delete Files And Directories In Linux



Regular expressions are also supported by rm. If you want to delete all files with the name file-name-*, type:

$ rm file-name*.ext

How To Delete Files And Directories In Linux

Regular expressions may also be used to define different directories. We can use something like to delete three files that fit file-name-1, file-name-2, and file-name-3.

$ rm file-name-[123]

How To Delete Files And Directories In Linux

4. Delete the archive:

The rm command with the -d flag can be used to remove an empty directory.

$ rm -d {dir-name}

Supported options for file deletion can also be combined with deleting the directory with the -d flag.

$ rm -idv {dir-name}

How To Delete Files And Directories In Linux

Using the -r flag to deleting a non-empty directory.

$ rm -r {dir-name}

How To Delete Files And Directories In Linux



If you do not want a prompt before deleting the directory and its contents, use the -rf flag. This will remove everything inside the directory, including the directory itself, without any confirmation. Use caution especially when using as a root.

$ rm -rf {dir-name}

5. Locate and delete files:

We can use the locate command with various choices for more complicated specifications. To delete all files in a path specified by {dir-to-search} that follow a pattern {pattern}.

$ find {dir-to-search} -type f -name {pattern} -exec rm -f {} \;

Example:

$ find luv -type f -name "*.txt" -exec rm -f {} \;

How To Delete Files And Directories In Linux

We may slightly change the above command to delete everything that fits the sequence {pattern}, including directories within {dir-to-search}:

$ find {dir-to-search} -name {pattern} -exec rm -rf {} \;

How To Delete Files And Directories In Linux

Internally, modern implementations of the find command support the delete feature. The -delete flag is used to override the rm instruction, while the –depth flag tells find to process the contents of the directory before the directory itself:

$ find {dir-to-search} -type f -name {file-name-pattern} -depth -delete



6. Empty files should be found and deleted:

You may use the following command to remove all empty directories within a given path dir-to-search:

$ find {dir-to-search} -type d -empty -delete

Instead, use the following command to remove all empty files within a given path dir-to-search:

$ find {dir-to-search} -type f -empty -delete

7. Permissions are used to locate and delete files:

We can now remove files based on special permissions, such as:

$ find {dir-to-search} -name {pattern} -perm {NNN} -delete

Consider the following scenario:

$ find /var/tmp -name "temp*" -perm 755 -delete

Easy (unlink), (rm), and (rmdir) commands are available in Linux, and they can be quickly expanded with regular expressions. For more specialized needs, you should use a variety of techniques such as (find) to accomplish your goals. Aside from the examples in this post, you can configure your quest by using find with any of the available flags.

Often run find commands without the rm or -delete flags and examine the output to determine which files or folders may be affected by the execution of a program. Backup setup and procedure are beneficial not just in the event of unintentional deletions, but also in the event of hardware errors and cyber-attacks.

My Personal Notes arrow_drop_up
Recommended Articles
Page :