How to delete files and directories in the Linux terminal

The rm and rmdir commands remove files and directories on Linux, macOS and other operating systems similar to Unix. They are similar to the deltreo commands of Windows and two. These commands are very powerful and have many options.

It is important to keep in mind that files and directories deleted using rm and rmdir are not moved to the Trash. They are immediately removed from your computer. If you accidentally delete files using these commands, the only way to restore them is from a backup.

How to remove files with rm

The simplest case is to delete a single file in 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, provide a path to the location of the file.

rm./ruta/al/archivo/archivo/archivo_1.txt

You can pass more than one file name to rm. By doing so, all specified files are deleted.

rm file_2.txt file_3.txt

Wildcards can be used to select groups of files to be deleted. The asterisk () represents several characters and the asterisk () represents a single character. This command would erase all png image files in the current working directory.

rm * .png

This command would erase all files that have a single character extension. For example, this would delete File.1 and File.2, but not File.12.

rm *.?

If a file is write protected, you will be asked to delete it. You must answer with s or n and press «Enter».

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

rm -i * .dat

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

rm -f file name

How to remove directories with rm

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

rm -d directory

If more than one directory name is provided, all empty directories specified are deleted.

rm -d directory1 directory2 / path / to / directory3

To delete directories that are not empty, use the -r (recursive) option. To be clear, this removes the directories and all the files and subdirectories contained in them.

rm -r directory1 directory2 directory3

If a directory or file is write protected, you will be asked to confirm the deletion. To delete the directories that are not empty and to suppress these indications, use the -r (recursive) and -f (force) options together.

rm -rf directory

Care is required here. If you make a mistake with the rm -rf command, you can cause data loss or system malfunction. It is dangerous, and caution is the best policy. To understand the directory structure and the files that will be deleted 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. In other Linux distributions, use the package management tool of your Linux distribution instead.

sudo apt-get install tree

Executing the tree command produces a simple-to-understand diagram of the directory structure and the files below the directory from which it is executed.

tree

You can also provide a path to the tree command to start the tree from another directory in the file system.

path of the tree / to / to / directory

The rm command also has the options -one-file-system, -no-preserve-root, -preserve-root, but they are only recommended for advanced users. If you make a mistake, you can accidentally delete all the files in the system. See the command manual page for more information.

How to remove directories with rmdir in Linux

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

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

Delete a single directory in 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 that directory:

rmdir / path / to / directory

If you try to delete a folder that is not empty, rmdir will give you an error message. In the following example, rmdir deletes the client directory silently and silently, but refuses to delete the project directory because it contains files. The project directory is left exactly as it was and the files in it are intact.

When rmdir gives an error "Directory not empty", it stops processing the directories that were passed to it on the command line. If you have asked him to delete four directories and the first one has files, rmdir will give you the error message and will not do anything else. 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 have been passed to rmdir, which are work / reports and work / quotes. The -ignore-fail-on-non-empty option has been included in the command.

The work folder / reports contains files, so rmdir can not delete it. The -ignore-fail-on-non-empty option requires you to ignore the error and move on to the next folder you need to process, which is work / quotes. This is an empty folder, and delete it.

This was the command used.

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

You can use the -p (parents) option to delete a directory and also to delete its parent directories. This trick works because rmdir starts with the destination directory and then backs up to the parent. That directory should now be empty, so that it can be deleted by rmdir, and the process is repeated backing down the path that was provided to rmdir.

In the following example the command that is passed to rmdir is:

rmdir -p work / invoices

Both the invoices and the work directories are deleted, as requested.

Whether you are using Bash or any other shell, Linux provides flexible and powerful commands to remove directories and files directly from the terminal's command line.

Some people prefer to have a workflow that revolves around the terminal. Others may have no other choice in the matter. They may be working on servers without a GUI installed or in a remote session on a headless system such as a raspberry Pi. These commands are perfect for that group of people.

But whatever the type of workflow you prefer, these commands lend themselves very well to being included in shell scripts. If a script is triggered by a cron job, it can help automate routine cleaning tasks such as purging unwanted log files.

If you investigate that use case, remember the power of these commands, test everything carefully and always keep a recent Linux backup.