Step by Step Guide How To Delete Directory in Linux

The directories they are always present on our computer His presence is inevitable because these are the ones that store the information necessary for our teams, 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 they store a lot of information and others because they occupy hard drive space. Whatever your case, today we will teach you how to remove them. This time 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 us and group all the computer files and other sub-directors, who attend to the contents, all this It 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 one of the files that are on our computer: as they are, the attributes of the files or where the storage device can be physically found.

How are directories in Linux?

Linux It 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 filed, from directories to devices (in some cases name devices as nodes, but still they are files).

This operating system organizes the Linux file systems in a hierarchical structure similar to that of a tree, where the highest or highest level They are the root directories. Other files are below the root directory, to explain it more illustratively, We can give as an example the following:

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

Which directories are under the root directory?

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

  • / bin – Applies for important binary applications.
  • / boot – It shows all the configuration files of the boot, cores and other files necessary for the boot (boot) of the equipment.
  • / dev – Device files directory
  • /etc – They 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 display system libraries (libraries).
  • / lost + found – Provides a “lost + found” system for files that exist under the root directory (/).
  • /half – Partitions mounted (loaded) automatically on the hard disk and removable media (media) such as CDs, digital cameras, etc.
  • / mnt – File systems mounted manually on the hard disk.
  • / opt – Provides a location to install optional (third-party) applications.
  • / proc – Special dynamic directory that maintains information about the state of the system, including the processes currently running.
  • / 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 – Variable files such as log files and databases.

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

The main job of operating systems like Linux consists of commands that execute, programs, activities or some actions. So that 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 removes references for each object within Linux, as they are, files with two names, and others, although by default this type of command does not delete directories completely, so it is considered that if the files are deleted they can be recovered.

Shred

The Shared command It is used to completely erase files, as we saw in the previous command, directories are not completely deleted, while this one. Many experts consider that what this command does is overwrite the files to hide their contents and addresses. This command has different options such as:

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

Sdmem

This command as its name indicates It is a safe memory cleaner whose main function is to delete active data directly of RAM permanently, guaranteeing an impossible recovery.

Sswap

This command (Secure Swap Wipe) allows us to clean disk partitions, deleting the data during the information exchange (swap). To be able to use it first you 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 files that we need to eliminate safely and permanently. Although to use it will be necessary that install some packages and then be able to use it.

Although it seems a lie, this is one of the most insistent commands when deleting, 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 its contents.
  • “Stm –r personal-capeta /” – It is a command that allows us to delete information, although it takes a long time to function, but it happens because it deletes a file several times.

Sfill

We can use this command. to delete files permanently, and leave free spaces on the disk, so that it ensures that the deleted information cannot be recovered. This command has the following options:

  • “I” – It is used to make two passes randomly and one with zeros.
  • “V” – It 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 safely erase files at a low level, as well as the secure delete command you need 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 are going to show you the steps that we must complete To learn how to use this command:

Let’s go to the directory

When we enter the terminal, let’s 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 view a list of files, then delete the selected files and use Is again to confirm that they have been deleted.

We indicate the address again

Yes, with the previous step file deletion did not work, we will have to re-enter your route and address. let's enter the route and address again

We delete the files

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

  • rm file6 file7 file8

we delete the files

We delete directories with the rm command

We had already commented at the beginning that this type of command does not delete directories, default just delete 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 delete all the data, in order to verify that everything has been deleted.

we check that everything has been deleted

  • 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 resort to the –r (recusive) option, so that from it be removed All files and subdirectories that are hosted in the directory: rm –r directory.
  • We verify that is deleting the directory, using the parameter –D where will we receive a message that tells us that the directory cannot be deleted because it is not empty, so we will use the command rm together with the parameter –R and so it will be removed.

we verify that the directory is being deleted

  • If we get, a notice that our directory is write-protected, we must combine –R Y –F (force) to force the system to erase it. rm –rf directory.
  • Delete directories through command It is very simple, so if we want to eliminate it we will also have to understand its structure. For this, we can use the tree command, that we must install it using the following: Sudo apt install tree.
  • Already installed, we run tree so that the structures of the directories are deployed, where will show us in detail How the directory is organized:

Let’s use rmdir to delete the directories

Rmdir it’s almost the same as the rm command only this time more focused on directories, so it is known how to remove the directory. Follow our directions:

  • We execute the command inside the terminal, to be able to delete all directories What we want. rmdir directory. If we want to delete multiple directories, we do as in the previous cases, we enter the directory names, according to the order we want 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 it’s not empty.

the directory is not empty

  • If we are shown many errors in the process to remove directories, we only execute the option –ignore-fai-on-non-empty which will allow us to omit the error that is being presented to us.
  • Too we can use the –p (parent) option which 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 earlier. What you must do to apply it is to follow the steps below:

  • We use the syntax to delete the files, as shown below: shred (options) file (…).

see command usage

The options are:

  • Force (f) – It allows us to modify all permissions until all files are deleted
  • n – With it, we indicate the numbers of times that the files should be deleted or overwritten.
  • or – Delete the files after overwriting it.
  • Y – With this option, we can see the progress of the operation.
  • z – Overwrite 0 to avoid leaving any trace of the file.
  • -s, –size = N – Define the uses of the bytes.
  • -x, –exact – Does not round file sizes up to a complete block.
  • –Help – We display a help list of the command.
  • -version – It indicates the current version of the command.

If we want to see the use of the command, we execute the following: shared –zvu –n (# of times) “Archive”. Finally, we execute the command process to finish. As indicated, the file name can be renamed in zeros and deleted.

With sdmem

This It is a widely used command, although it does not have breadth like many, its usefulness is of great importance. To be able to use it We need to execute the command as follows: Sudo sdmen –f -v

With sswap

We already know that this command helps us clean the partitions or the files inside the partitions of our hard drive. In order to do it we need to comply with the following sequence and command lines:

  • cat / proc / swaps
  • swapon
  • sudo swapoff “partition”
  • sudo sswap –V “partition”

With Secure-Delete

To be able to erase files safely With Secure-Delete we must comply with the following steps: We install the unit using the commands below:

  1. sudo apt-get install secure-delete (for systems like Debian and its derivatives)
  2. sudo yum install secure-delete (it is the command for centOS and RHEL).

We continue to execute it with the following command lines: srm –vz file / *. To finish we use the options we want:

  • -v –It allows us to enable the detailed model of what is happening.
  • -z –Eliminate until the last write by adding zeros in random places.
  • Although according to what we have in mind we can also use the following options:
  • -d –With the help of this option, we ignore all the characters and the command line.
  • -F – We can enable the fast mode, although it is not highly recommended since the process is less secure and very little allows synchronization.
  • -r – Enable recursive mode. Whose mode allows us to eliminate all the data that is within the sub-directors.
  • -l –This option allows us to execute two passes on the file.

*Note: we have to know that the secure-delete (srm) command does not delete all data from NFS file systems and it does not delete all the data in swap and / tmp.

With sfill

This is another very useful command that we can easily use from our Linux OS to permanently delete directories. What we must do is the following:

  • We execute the command on the root partition, using the command with the modification option –v To see the detailed mode: sudo sfill –v / home / directory / tmp.
  • If we have created a partition separate from / home we will have to apply the command as follows: sudo sfill –v / home / user.
  • We can use this command with the following parameters, according to what we want to do with the directories:
    • -F -We enable fast mode, although this is less secure and does not allow synchronization mode.
    • -i – Used to erase all free space.
    • -I –We delete only the free space that is on the hard disk.
    • -v –We see the process in detail

If we want to see more information about the process, we execute the following command: man sfill

Wipe

Remember that this must be installed, To do this we follow the following steps:

  • We install Wipe using the following commands:
    • sudo apt-get install wipe (for Debian and all its derivatives)
    • sudo yum install wipe (for CentOS and RHEL system)
  • We execute the following syntax: wipe –rfi “file” / *.
  • Finally, we choose the options we want to be executed:
    • -r –With this option, we indicate that you resort to the sub-directors.
    • -F -Used to force file deletion.
    • -i –We can see the process of deleting the files.