Skip to content
Step by Step Internet 🌐 Guides for learning to surf the Net

How to delete a directory in Linux safely and error free? Step by step guide

How to delete a directory in Linux safely and error free? Step by step guide

The directories are always present on our computer. Its presence is inevitable, because it is these that store the information necessary for our team, such as file location, file type, number of files, modification date and others.

>

Like all operating systems, Linux has directories . Many people prefer to delete these, because store a lot of information and others because they take up hard disk space . Whatever your case, today we will teach you how to eliminate them .

On this occasion we will show you how to delete these directories through the Linux terminal, using the appropriate commands to achieve it.

What is a Linux directory and why is it necessary to delete them?

We can say that the directories are the virtual containers that store and group all the computer files and other sub-directors that serve the contents, all this depends on the purpose or criteria that we as users determine.

Specifically, the work of the directory is to store all the information of each of the files that are on our computer: how they are, the attributes of the files or where the file can be physically found. storage device.

How are the directories in Linux?

Linux is one of the operating systems , which orders directories in simpler ways. However, many people may be confused by your organization.

For Linux almost everything is files , from directories, to devices (in some cases names devices as nodes , but they are still files). This operating system organizes the Linux file systems in a hierarchical structure similar to that of a tree, where the top or highest level are the root directories.

The other files are below the root directory , to explain it more illustratively, we can give as an example the following:

  • “/home/jebediah/cheeses.odt” shows us the full path of the file “cheeses.odt ” which is located in the “Jebediah”. At the same time it is under the home directory, which in turn is located in the root directory (/).

Which directories are under the root directory?

We can find under the root directory, different directories . Below we will show you a small list of the most common directories under the root directory (/):

  • / bin – Applies to important binary applications.
  • / boot – Shows us all boot configuration files, cores and other files necessary for the boot (boot) of the equipment.
  • / dev – Directory of the device files
  • / etc – These are the configuration files, boot scripts, etc.
  • / home – With this option we can see the personal directories (home).
  • / initrd – We can see this option or directory when creating a custom initrd boot process.
  • / lib – It is used to show the system libraries (libraries).
  • / lost + found – Provides a “lost + found” system for files that exist under the root directory ( /).
  • /media – Partitions mounted (loaded) au tomatically on the hard disk and removable media (media) such as CDs, digital cameras, etc.
  • / mnt – File systems manually mounted on the hard disk.
  • / opt – Provides a location to install optional (third-party) applications.
  • / proc – Special dynamic directory that maintains information about the system status, including currently running processes.
  • / root – Personal directory of the root user (superuser); also called «root-bar».
  • / sbin – Important system binaries.
  • / srv – May contain files that are served to other systems.
  • / sys – System files.
  • / tmp – Temporary files .
  • / usr – Applications and files that most users can access.
  • / var – Files variables such as log files and databases.

What are all commands to delete files and folders from the terminal? Linux?

The main job of operating systems such as Linux consists of commands that execute , programs, activities or some actions. So it is very important to understand how and what to use them for.

Next, we will show you the commands we can use to delete files and folders from the Linux terminal:

RM

The RM (Remove) command whose main function is to delete or delete files , directories or others as symbolic links.

In other words RM deletes the references that exist for each object within Linux, as they are, files with two names, and others, although by default this type of command does not delete the directories completely, so it is considered that if the files are deleted they can be recovered.

Shred

The Shared command is used to completely erase the files , because as we saw in the previous command, the directories are not completely deleted, while this one is. Many experts consider that this command is overwriting files to hide their contents and addresses.

This command has different options such as:

  • “F” – To change all permissions and allow writing to files if necessary.
  • “N” – Used to specify the number of times, which by default is 3.
  • “U” – This is used to directly delete files,
  • “V” – Shows the progress of the operation.
  • “Z” – It is used to hide the secure deletion of the data.

Sdmem

This command as indicated by its name is a secure memory cleaner , whose main function is to delete active data directly from RAM permanently, guaranteeing a impossible recovery.

Sswap

This command (Secure Swap Wipe) allows us to clean disk partitions , erasing data during information exchange (swap).

To be able to use it you first need to execute the swap command in order to exchange the partitions that have active data, and be able to erase those files that we want to be no longer on our hard drive.

Secure-Delete

This command allows us to delete all the files that we need to delete safely and permanently. Although to use it you will need to install some packages and then be able to use it.

Although it seems a lie, this is one of the most insistent commands at the time of deletion , and this is considered so, because it eliminates as many times as necessary in the same space a file or object until 80 or 90% of it disappears.

This command has two options, which are:

  • ”srm mis-passwords.txt” – This is used to delete folders with everything and their contents.
  • “stm –r folder -personal / ” – It is a command that allows us to erase information, although it takes a long time in its function, but it happens because it deletes a file several times.

Sfill

We can use this command to permanently delete the files , and leave free spaces on the disk, so that the deleted information cannot be recovered.

This command has the following options:

  • “I” – It is used to make two random passes and one with zeros.
  • “V” – We shows the progress of the operation.
  • “Z” – It is used to hide the secure deletion of all data.

Wipe

Finally, there is the Wipe tool whose command is used to securely delete files at a low level, as well as the secure delete command requires an installation package to use it.

Steps to use the rm command to delete a directory from the Linux terminal

The most used command to delete files from the Linux terminal is rm (remove). In this section we will show you the steps that we must follow to learn how to use this command:

Let’s go to the directory

When we enter the terminal, we go to the directory or path that indicates the exact location of the file we want to delete. rm “Archive”

We use the Is command

Now we will use the Is command to see a list of the files , then delete the selected files and use Is again to confirm that they have been deleted.

We indicate the address again

If with the previous step the deletion of files did not work, we will have to re-enter your route and address.

ingresemos la ruta y direccion nuevamente

Delete the files

As we have been doing, we can delete several files simultaneously , so we must select files and execute the commands:

  • rm file6 file7 file8

borramos los archivos

Delete directories with the rm command

We had already commented at the beginning that this type of commands does not delete directories , by default it only deletes files , but this does not mean that it is impossible to delete the directories with this command .

Follow the steps:

  • To delete the empty directories we execute the command as follows: rm –d directory.
  • We make a list of the directories to be able to do the removal of all the data, in order to verify that everything has been deleted.

comprobamos que ha sido borrado todo

  • When we indicate more than one name in the directories, all those we have selected will be automatically deleted. If our case is that we want to delete more than one directory that is not empty, we must use the option –r (recusive), so that from it are removed all the files and subdirectories that are hosted in the directory: rm –r directory.
  • We verify that is deleting the directory , using the parameter –D where we will receive a message that tells us that the directory cannot be deleted because it is not empty, so we will use the rm command along with the –r parameter and so it will be removed.

verificamos que el directorio se este eliminando

  • If we get a warning that our directory is write-protected , we must combine –r and –f (force) > to force the system to erase it. rm –rf directory.
  • Deleting directories by means of the command is very simple , so if we want to delete it we will also have to understand its structure. To do this we can use the tree command, which we must install using the following: Sudo apt install tree.
  • Already installed, run tree so that the directory structures are displayed, where will show us in detail how the directory is organized:

Let’s use rmdir to remove directories

Rmdir is almost the same as the rm command only this time more focused on directories, so it is known as remove directory .

Follow our instructions:

  • We execute the command inside the terminal, in order to delete all the directories we want. rmdir directory. If we want to delete multiple directories , we do as in the previous cases, enter the directory names, according to the order we want them to be deleted.
  • >

  • When we try to delete a directory or a folder that is not empty rmdir will display a message , where it tells us that it cannot be deleted, because the directory is not empty.

el directorio no esta vacio

  • If we are shown many errors in the process to delete the directories, we only execute the option -ignore-fai-on-non-empty that will allow us to omit the error that we are given is presenting.
  • Also we can use the –p (parent) option that will allow us to delete the selected directories and their parent directories. rmdir –p directory / directory

How to delete directories permanently from the Linux terminal?

With the previous command, what we get is to delete the file or directory partially. Now we will teach you how to do it permanently:

With Shred

This command is already presented to you before. What you must do to apply it is to follow the steps below: