How to remove files and directories in Linux

Introduction

Linux reaches with different tools that can help us in extracting files. We are still required to delete multiple files and folders based on conditions. Comprehending infrequent primary commands and their interpretations is valuable to complete our task quickly.

  • Utilize caution when operating the commands below, especially those using regular expressions or search patterns with the find command. An incorrect expression or practice will delete important data/system and non-intended files.
  • Usually have a recent copy of essential data and device files.
  • Use caution when driving those commands, especially if you’re using Sudo or as the superuser (root).

Removing a directory in Linux is a relatively simple task utilizing the GUI. Yet, if you don’t have credentials to the GUI, you can also drag directories using terminal commands.

How to remove files and directories in Linux

There are several additional ways to remove directories in Linux networks. If you utilize a Desktop file manager such as KDE’s Dolphin or Gnome’s Files, you can delete directories and files using the manager’s graphic user interface. But, if you are performing on a headless server or desire to withdraw numerous directories at one time, your most suitable option is to delete the folders (directories) from the command line.

This article will demonstrate how to delete directories and files in Linux using the rm, radio, and find commands.

 Before We Start, Keep in Mind

When dismissing a directory while operating a desktop file manager, the directory is transferred to the waste and fast healed.

Be additional cautious when removing directories or files or from the command line. Once the directory or file is deleted using the commands described in this article, it cannot be entirely retrieved.

Most Linux file systems are deleting a directory or file demands authoring a license on the directory and its content. Otherwise, you will accumulate an “Operation not authorized” error.

In Linux, if we desire to remove the directories and files, we can use the rm command.

The subsequent command is operated to remove a directory recursively, which suggests it will remove its subdirectories too, which indicates complete discarding:

rm -rf /path/of/directory/

The -r flag is for recurring removal, and -f is for militant removal. So employ this command to delete/remove a directory or file and whatever is in it.

To remove an individual file, run the following command is used,

rm .myfile.txt

The rm (remove) command can also remove directories and files and both. You can operate the whole path or a relative file path to determine the file’s place to delete.

The rm command doesn’t delete a file or directory by insolvency. To remove a directory or file, we should use the -r flag or the -d flag, which we operated in the sample above.

The rm command is a quiet assassin. It operates silently and will not publish any output notification on the screen when it finishes successfully. The rm command develops messages just in the case of a misconception, so you should be cautious while operating this command.

 Tools for Removing Files and Directories

One immediate file system administration chore affects constructing, altering, and deleting additional files and directories. Comprehending some fundamental tools and ideas for file deletion arrives convenient and preserves your time.

Linux presents different tools that can assist us in accomplishing file removal chores. Usually, we require to remove not simply a single but a lot of files and directories, established on some standards. Understanding a few typical commands and their combinations is helpful to get our tasks done quickly.

  • Operate the given commands with the warning, particularly regular operating profiles or some search techniques with the find command. An incorrect declaration or method may delete essential data/system files or simply non-intended files.
  • Always secure the latest backup of essential data and system files.
  • Take caution before running such commands, particularly with superuser (root) or as a Sudo.

Remove a file using the unlink command.

Not so prevalent. To remove a single file eternally, we can operate the unlink command.

$ unlink {file-title}

Remove single file using rm command

There’s a better naturally operated command for removing files, i.e., rm command, which sustains removing one or additional files simultaneously.

$ rm {file-title}

rm encourages you to ensure file deletion for write-protected files or directories; else, it cruises to remove the file literally. To create rm, always prompt before deleting a file, operate the -i flag:

$ rm -i {file-title}

The rm command deletes without showing any notification on the screen. To document the rm command, operate rm with the -v flag.

$ rm -v {file-title}

To remove write-protected files or directories without prompting for verification, use the -f flag.

$ rm -f {file-title}

Remove multiple files by using the rm command

Multiple files can be removed by identifying multiple filenames to the  rm as arguments.

$ rm {file-title-1} {file-title-2} {file-title-3} … {file-title-N}

The rm also hold up uniform expressions. If you willing to delete all files term as file-title-*, you can use:

$ rm file-title-*

We can also select multiple files operating uniform expressions. If we desire to delete three files that match file-title-1, file-title-2, and file-title-3, we can run something like:

$ rm file-title-[123]

Remove directory using rm command

$ rm -d {dir-title}

An unclogged directory can be deleted utilizing the rm command with the -d option.

Possibilities supported for file deduction can also be connected with directory removal using the -d flag. For sample:

$ rm -idv {dir- title}

To delete a directory that is non-empty operating the-r flag.

$ rm -r {dir-title}

Assume you don’t want any prompts before deleting the directory and its scopes. Operate the flag. This will delete everything inside the directory without confirmation, including its directory. Do help notification while using it predominantly as root.

$ rm -rf {dir-title}

Find and remove files using the find command

For complex requirements, we can operate the find command with typical options. To remove all files and directories matching pattern {pattern} inside a track given by {dir-to-search}

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

Example:

$ find /var/tmp -type f -title”*.tmp” -exec rm -f {} \;

If we demand to remove something that matches a {pattern} involving directories inside {dir-to-search}, we can directly modify the given command as:

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

Current versions of the find command reinforce the delete function entirely. Here -delete flag returns rm command while -depth guides find command to first approach the contents of the directory before the directory itself :

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

The given example is better efficient when operating with many files as it doesn’t generate a new external method for the rm command for each checked file. But not all interpretations of the find command (previously) may support -the delete flag. As a replacement, we can utilize the xargs command with find as shown in the following example below:

$ find {dir-to-search} -type f -title{pattern} -print | xargs -0 rm

Or use exec terminator’s + command to chain together anything found by the find command like the xargs:

$ find {dir-to-search} -Title{pattern} -exec rm -rf {}/ +

By insolvency, the find command utilizes -the print flag, which we usually ignore writing. With the xargs, we require to evade new-line characters between every filename. The print0 flag guides you to publish null characters after every experienced filename.

Similarly, we restrict the -0 flag with the xargs to pair both commands. The rm command deletes the file passed on to directed find input. For example, the given command will research and remove all *.tmp files from the existing user’s home directory (provided by ~ sign).

$ find ~ -title”*.tmp” -print0 | xargs -0 rm

The find command shows several methods to search directories and files, including permission, ownership, timestamp, etc. We’ll be wrapping occasional strategies to assist us with detailed file removal tasks.

Find and remove files and directories by a user.

To delete every single thing inside a given directory posses by a user, use:

$ find {dir-to-search} -mindepth 1 -user {user-title} -delete

In the given example, the -mindepth flag with value 1 preserves the directory given by {dir-to-search} from cancelation if it corresponds to the find pattern and standard. Or to delete everything inside a given directory be owned by a particular group, use:

$ find {dir-to-search} -mindepth 1 -group {group-title} -delete

If you do not want to cross the sub-directories beneath the search path, you also can operate the -max depth with the suitable directory deep value.

Here’s an example run:

$ find tempdir/ -mindepth 1 -max depth 1 -user Ahsan Rauf -group Ahsan Rauf -delete

If you desire to set down user-id and group-id alternatively, you can try something like:

$ find {dir-to-search} -mindepth 1 -UID {user-id} -gid {group-id} -delete

Find and draw empty directories.

In this example, my folder is an empty directory. It is impossible to delete a directory by just using the rm command. So for deleting a directory, you have to use the -dir option along with the command.

Find and remove files and directories older than x

Occasionally, you may require to delete files older than x days. Find has prospects to read the file’s access (time), creation(time),  and modification (time) times. We can use the time prospect to find and delete files altered more than x days ago.

$ find {dir-to-search} -type f -time +{x} -delete

For example, to delete all files and directories with extension log inside /var/tmp with a restriction time of 30 days ago or more, we can utilize:

$ find /var/tmp -title”*.log” -type f -mtime +30 -delete

Find and remove files and directories with authorization.

Now, we can also delete files and directories based on some certain permission, like:

$ find {dir-to-search} -title{pattern} -perm {NN} -delete

Example:

$ find /var/tmp -title”temp*” -perm 755 -delete

Or if we desireto use a symbolic form for authorization, use:

$ find {dir-to-search} -title {pattern} -perm u={rwx},g={rwx},o={rwx} -delete

Example:

$ find /var/tmp -title “temp*” -perm u=rwx,g=rw,o=rw -delete

Find and Delete file beginning with a – (hyphen) symbol

If any file uses -(hyphen) in its title, we can not delete it instantly by utilizing the rm command. We must use — (double hyphen) individually before the file title, just like choices, to remove the file.

The rm command does not misunderstand the file name as an alternative.

Verdict

Linux offers rm, unlink, and rmdir easy and quickly developed with common expressions. For more progressive needs, you have the opportunity to use an assortment of tools like discovery and the xargs to accomplish what you require. You’ve got the chance to use find with any of its available flags to customize your research.

 

Consistently find commands without the rm or -delete flag and interpret its output to understand which implementation of an actual order will affect files or directories. Having reasonable backup configuration and approach benefits not only in case of unexpected deletions but also in cases of hardware failures and cyber-attacks

You May Also Like

About the Author: admin

Leave a Reply

Your email address will not be published.