Home Beginner's Guide How to delete files and directories in Linux

How to delete files and directories in Linux

by Pulkit Chandak
delete file and directories linux

Deleting files on a Linux based system can take multiple forms. The good old graphical file manager can do that pretty quickly, but on the other hand, the command line has an arsenal of programs and options for just that.

We are going to show the various useful options, as well as basic commands that can be used for pretty much everything.

Deleting Files and Directories using command-line in Linux

rm command

The primary deleting command is the rm command. This is used with multiple options for various tasks, the most straightforward syntax being this:

rm file_name
Basic rm syntax

Basic rm syntax

This only works when that file is in the working directory, and if you are trying to use this on a directory that has files in it, this will not be much useful. Only using this command doesn’t delete directories.

The different useful flags of the rm command are:

-r

This option stands for recursive. When this command is used, any directory that meets the other criteria is also deleted, along with the files inside it. For example, there is a directory named test_dir, in which there are several files (as seen in the screenshot), and this can be deleted using the -r flag.

Recursive deletion

Recursive deletion

-d

This flag can be used to delete empty directories.

Removing empty directories

Removing empty directories

-f

This is a particularly useful flag. There are often files mentioned in the command (among multiple files), that may not exist. There can be other errors too. This can cause the other files not to be deleted. So to fix that, we can use the -f (force) flag, which removes files that match the criteria and ignores errors (if there are any).

-v

The verbose flag (-v) can be used to display the filenames as they are being deleted. This is useful for seeing the progress of the process, and I like to use this in case of large (or a high number) of files, as I can get an idea of the progress.

Verbose deletion

Verbose deletion

-i

Using the interactive (-i) flag provides a prompt before the files being deleted. This can be used mainly to clean up the files because you’ll get a prompt every time a file is being removed.

Interactive deletion

Interactive deletion

Useful tip:

This is a somewhat helpful shortcut. The ‘*’ character can be used for the input of multiple files. The ‘*’ actually denotes any or any number of characters. If you enter ‘test*‘ as the name, it will include files that start with ‘test’ and has anything after that. Similarly, using ‘*test*‘ will consist of any file/directory that has a ‘test‘ in the name, whatever may be before or after it.

Deleting files with name pattern

Deleting files with a common name pattern

rmdir command

The rmdir command is used to delete empty files. The basic syntax goes like this:

rmdir directory_name

The one flag that is quite useful for the rmdir command is this:

-p

The parent flag (-p) removes directories that have other (obviously empty) directories inside it. If there is a structure as a/b/c, where ‘c‘ is inside ‘b,’ and ‘b‘ in ‘a’ the command can be constructed as follows:

rmdir -p a/b/c

find command

The find command (as evident) is used only to find files with specific conditions, but the result after that can be used as input to the rm command. This is a little more tricky, but it can be accomplished quite easily. Basic syntax:

find [path] -type [-d/-f] -name [name-conditions] -exec rm -r {} +

I know, I know, this seems complicated, but let me explain:

  • [path]: This needs to be replaced by the location of the directory that you need to search the files in. If this is the directory you are currently in (quickly check using command pwd), then replace [path] with ‘.’ and if you want to use your home directory, use ‘~.’
  • -type: Use the ‘d’ flag if you only want to remove directories only, and ‘f‘ if you want to delete files only (after ‘-type,’ for ‘d‘ or ‘f,’ the ‘‘ is not required). If you want to delete both directories and files, don’t use this option at all.
  • -name: This includes the conditions for the file name. If you have a specific keyword that the files’ and folders’ names contain, use it in the following manner: ‘*keyword*’
    (The single quotations are necessary).

    Just for information, ‘*’ here means practically everything. That is, this command will search for files whose names have everything and anything before the keyword, and everything and anything after the keyword.

The rest of the command has to be used as it is, but just for information, the ‘-exec‘ part instructs that the output from the command before is used as input for the following command. The next part, ‘rm -r‘, deletes everything that is given as an input from the earlier part, and the ‘{} +‘ part instructs the rm command to use the output from the find command.

Deletion using find command

Deletion using find command

Example:

find ~ -type -f -name *test* -exec rm -r {} +

This finds files that have ‘test‘ in any part of the name, in the home directory, and only files, not directories.

Note

There is something to note, though, that some characters that might be in the file name are treated as special characters by the Linux command interface. For example, if your file name is ‘-test,’ the Terminal will interpret the ‘-‘ part in another way. To use this as a part of the file name, enter a backslash before that special character. Example:

rm \-test

This can be used for other characters such as *, (, ), #, $, and even blank spaces.

Names with special characters

Names with special characters.

Conclusion

This concludes everything required to delete files in any format, from any location, of any kind. Even deeper levels of configurations can be provided, but that requires a deeper understanding of the Linux shell.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.