How to Delete/Remove Files and Directories Using Linux Command Line

This tutorial will demonstrate how to delete files and directories in Linux using the rm, unlink, and rmdir commands.

How to Remove Files Using Linux Command Line

Use the rm (remove) or unlink commands on the Linux command line to delete (or remove) a file. While rm allows you to remove multiple files simultaneously, the unlink command only allows you to remove a single file. When deleting files or directories, use extra caution because once a file is deleted, it cannot be easily recovered.

To delete a single file, use the rm or unlink command followed by the file name:

unlink filename
rm filename

You will be prompted for confirmation if the file is write-protected. To delete the file, type y and press Enter. If the file does not have write protection, it will be deleted automatically. Use the rm command followed by the names of the files separated by a space to delete multiple files simultaneously.

rm filename1 filename2 filename3

To delete multiple files, you can also use a wildcard (*) and regular expansions. To delete all .zip files in the current directory, for instance, use the following command:

rm *.zip

To see what files will be removed when using regular expansions, it is best to first list the files using the ls command. To verify each file deletion, use the rm command with the -i option:

rm -i filename(s)

When using the rm command, the -f (force) option can be used to delete files without a confirmation prompt, even if the files are write-protected.

rm -f filename(s)

Additionally, rm parameters can be mixed and matched. One can use the following command in verbose mode, for instance, to delete all.txt files in the current directory without being prompted:

rm -fv *.txt

How to Remove Directories (Folders)

The rmdir and rm commands in Linux are used to delete directories. While rm can remove directories and their contents recursively, rmdir is a command-line utility for removing empty directories.

Either rmdir or rm -d followed by the directory name will delete the specified empty directory.

rm -d dirname
rmdir dirname

Use the rm command’s -r (recursive) option to delete non-empty directories along with all their contents:

rm -r dirname

To delete a write-protected directory or file within it, you will be asked to confirm the action. Using rm’s -r (recursive) and -f (force) options, you can delete a directory structure along with all of its contents without being prompted to confirm the action.

rm -rf dirname

The rm -r command, followed by the directory names separated by spaces, can be used to delete multiple directories at once.

rm -r dirname1 dirname2 dirname3

In the same way that wildcards (*) and regular expansions can be used to match multiple files, they can also be used to match multiple directories.

Summary

You should now be comfortable safely erasing files and directories from the command line using the Linux rm, rmdir, and unlink commands. If you have any questions, please feel free to post a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *