How to delete files and directories in Linux

Deleting files in Linux can take multiple forms. The good old GUI can do that quickly, but the command line has an arsenal of options for just that and more.

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.

Pulkit Chandak
Pulkit Chandak is a Linux enthusiast and has been using and experimenting with open source software, and hardware too since a long time. He is a huge admirer of open source software and wants to ventilate it to all around him. He is interested in reviewing and writing tutorials on Linux and its innumerable distributions. He believes that freedom in software leads to freedom of the mind from the chains of limits.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,848FansLike
265FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Free up hard disk space ubuntu
Hard disk space can easily get filled up with cached package files, old kernels, and other obsolete files that occupy unwanted hard disk space. Here are top five best and safer ways to clean and free up hard disk space in Ubuntu. We show you Terminal and GUI way of cleaning up system.
Best video editors for linux
This article is for all the Linux users out there who are looking for video editors to create their own professional videos, which can range from short documentaries to even movies.