How to Delete Files and Directories in Linux? - GeeksforGeeks

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).
Not so well-liked. We may use the unlink command to permanently delete a single file.
$ unlink {file-name}
The rm command, which facilitates deleting one or more files simultaneously, is a more widely used command for removing files.
$ rm {file-name}
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}
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}
Using the -f flag to remove write-protected files without asking for clarification.
$ rm -f {file-name}
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}
Regular expressions are also supported by rm. If you want to delete all files with the name file-name-*, type:
$ rm file-name*.ext
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]
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}
Using the -r flag to deleting a non-empty directory.
$ rm -r {dir-name}
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}
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 {} \;
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 {} \;
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
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
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.
How To
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install FFmpeg on Windows?
How to Add External JAR File to an IntelliJ IDEA Project?
How to Set Git Username and Password in GitBash?
How to Install Jupyter Notebook on MacOS?
How to Create and Setup Spring Boot Project in Eclipse IDE?
Sed Command in Linux/Unix with examples
AWK command in Unix/Linux with examples
grep command in Unix/Linux
cut command in Linux with examples
cp command in Linux with examples