How to Delete Files and Directories in the Linux Terminal

Terminal Bash on the Ubuntu Laptop ConceptFatmawati Achmad Zaenuri / Shutterstock.com

The rm and rmdir commands delete files and directories on Linux, macOS, and others. Unix-like operating systems. They are similar to the del and deltree commands of Windows and DOS. These commands are very powerful and have several options.

It is important to note that files and directories deleted using rm and rmdir are not moved to the trash. They are immediately deleted from your computer. If you accidentally delete files using these commands, you will only be able to restore them from a backup.

How to delete files with rm

The simplest case is to delete a single file from the current directory. Type the rm command, a space, and then the name of the file you want to delete.

rm file_1.txt

If the file is not in the current working directory, specify a path to the file location.

rm ./path/to/the/file/file_1.txt

You can pass more than one file name to rm. This deletes all the specified files.

rm file_2.txt file_3.txt

Wildcards can be used to select groups of files to delete. The * represents several characters and the? represents a single character. This command would delete all png image files from the current working directory.

rm * .png

This command would delete all files with an extension to a character. For example, this would delete File.1 and File.2, but not File.12.

rm *.?

If a file is write-protected, you will be prompted to delete it before deleting it. You must answer with y or n and press "Enter".

Rm command with write-protected file

To reduce the risk of using rm with wildcards, use the -i (interactive) option. This requires that you confirm the deletion of each file.

rm -i * .dat

Rm command in interactive mode

The -f (force) option is the opposite of interactive. It does not ask for confirmation even if the files are write-protected.

rm -f file name

How to delete directories with rm

To delete an empty directory, use the -d (directory) option. You can use wildcards (* and?) In directory names, just as with file names.

rm -d directory

Providing more than one directory name deletes all specified empty directories.

rm -d directory1 directory2 / path / to / directory3

To delete directories that are not empty, use the -r (recursive) option. To be clear, this deletes the directories and all the files and subdirectories that they contain.

rm -r directory1 directory2 directory3

If a directory or file is write-protected, you will be prompted to confirm the deletion. To delete directories that are not empty and remove these prompts, use the -r (recursive) and -f (force) options at the same time.

rm -rf directory

Care is needed here. An error with the rm -rf command may result in data loss or system malfunction. It's dangerousand prudence is the best policy. To understand the directory structure and the files that will be removed by the rm -rf command, use the tree command.

Use apt-get to install this package on your system if you are using Ubuntu or another Debian-based distribution. On other Linux distributions, use the package management tool of your Linux distribution instead.

Installation tree sudo apt-get

Running the tree command produces a simple-to-understand diagram of the directory structure and files under the directory from which it is run.

tree

output of the tree command

You can also provide an access path to the tree command so that it starts the tree from another file system directory.

path of the tree / to / directory

The rm command also has the options –one-file-system, –no-preserve-root, –preserve-root, but these are only recommended for advanced users. If you encounter a problem, you can accidentally delete all your system files. Check the order manual page for more information.

How to delete directories with rmdir

There is another command, called rmdir, that you can use to delete directories. The difference between rm and rmdir Is rmdir can only delete empty directories. It will never delete the files.

The simplest case is to delete a single empty directory. As with rm, you can pass multiple directory names to rmdir or a path to a directory.

Delete a single directory from the current directory by passing its name to rmdir:

rmdir directory

Delete multiple directories by passing a list of names to rmdir:

rmdir directory1 directory2 directory3

Delete a directory that is not in the current directory by specifying the full path to this directory:

rmdir / path / to / directory

If you try to delete a folder that is not empty, rmdir will send you an error message. In the following example, rmdir succeeds and silently removes the client directory but refuses to delete the project directory because it contains files. The project directory is left as is and the files it contains are not modified.

Rmdir command with a non-empty folder

When rmdir generates a "non-empty directory" error, it stops processing the directories that were passed to it on the command line. If you asked him to delete four directories and the first contains files, rmdir will give you the error message without doing anything more. You can force it to ignore these errors with the -ignore-fail-on-non-empty option so that other directories are processed.

In the following example, two folders were passed in rmdir. These are work / reports and work / quotes. The option –ignore-fail-on-non-empty has been included in the order. The work / reports folder contains files. Rmdir can not delete it. The option – ignore-fail-on-no-empty force rmdir to ignore the error and move to the next folder to be processed, namely work / quotes. This is an empty folder and rmdir deletes it.

This was the command used.

rmdir – ignore-fail-on-non-empty work / reports / work / quotes

rmdir with option --ignore-fail-on-non-empty

You can use the -p option (parents) to delete a directory, as well as its parent directories. This trick works because rddir starts with the target directory, and then returns to the parent. This directory must now be empty, so it can be removed by rmdir and the process repeats to return to the supplied path to rmdir.

In the following example, the command passed to rmdir is as follows:

rmdir -p work / invoices

Rmdir command with option remove parents

Invoices and work directories are deleted as requested.

Whether you use Bash or any other shell, Linux provides flexible and powerful commands that allow you to delete directories and files directly from the terminal command line. Some people prefer to have a workflow that revolves around the terminal. Others may have no choice in the matter. They may be working on servers without a graphical interface or on a remote session on a headless system, such as a Raspberry Pi. These commands are perfect for this group of people.

But no matter what type of workflow you prefer, these commands are very well suited to be included in shell scripts. If a script is triggered by a cron job, it can help automate common maintenance tasks such as purging unwanted log files. If you study this use case, remember the power of these commands, test everything carefully and always keep a recent backup.

Advertisements

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.