How to Remove Files and Directories in Linux

Removing files and directories through the command line in Linux can be haunting if you are not used to it. Thus, in this guide you’ll learn how to remove files and directories in Linux.

Deleting Files through the Command Line

In order to remove files in Linux, we can use the most popular command which is the rm (or remove) command. Alternatively, there’s the command unlink that can also be used to delete files.

The difference between the rm and unlink command is that with the unlink command you are only able to delete a single file at once, in contrast, with the rm command you are able to delete multiple files at the same time.

The syntax for both commands are similar, which is:

rm/unlink filename
Example

rm file.txt

unlink file.txt

In order to remove multiple files at a time with the rm command, you can do it by entering all the names of the files you’d like to delete:

rm file.txt photo.png video.mp4

How to remove files in linux with a common extension (wildcard)

Futhermore, in order to delete, for example, all the files that have a common extension, such as JPG, MP4, etc. You can use wildcard and regular expressions to do so:

rm *.mp4

The asterisk (*) indicates that it will remove all the files that have the same extension, in this case .mp4.

Once you have removed or deleted the files, there is no way back. In order for us not to delete an important file by accident, we can use the -i option so that it prompts for confirmation before deleting the file. This way, you get to see the filename before deleting it.

rm -i video.mp4
output

[j@jh Desktop]$ rm -i file.txt
rm: remove regular file 'file.txt'?

Deleting files without any prompt

In order to delete files without getting any prompts, you can use the -f (force) to delete all the files without prompting anything:

rm -f video.mp4 file.txt script.pdf

How to remove directories in Linux

Removing or deleting directories in linux is as simple as deleting a file or multiple files, it has the same syntax. Though, we can either use the rmdir or the rm command.

The main difference between these two commands, is that the rmdir command can only delete/remove directories/folders that do not have anything in them (empty), on the other hand, the rm command will delete recursively (with the -r option) the directory and its content.

rmdir example

[j@jh Desktop]$ echo 'test' > rmdir/file.txt
[j@jh Desktop]$ rmdir rmdir
rmdir: failed to remove 'rmdir': Directory not empty
[j@jh Desktop]$ 
rm example

[j@jh Desktop]$ echo 'test' > rm/file.txt
[j@jh Desktop]$ rm -r rm/
[j@jh Desktop]$ 

Force delete a directory and deleting with prompt

Similar when deleting files, we can also use the -f and -i options to force, and/or prompt confirmation:

Force delete example:

[j@jh Desktop]$ echo 'test' > rm/file.txt
[j@jh Desktop]$ rm -rf rm
Deleting with prompt example

[j@jh Desktop]$ echo 'test' > rm/file.txt
[j@jh Desktop]$ rm -r -i rm/
rm: descend into directory 'rm/'? y
rm: remove regular file 'rm/file.txt'? y

Deleting multiple directories

Lastly, in order to delete multiple directories you can keep naming them one after another with the recursive (-r) option:

rm -r directory1 directory2 directory3 . . . directory100

Summary

This guide covered the basics on how you can remove files and directories in Linux. Feel free to visit the die.net site for each of these two commands to learn more!

Leave a Comment