Javatpoint Logo
Javatpoint Logo

How to Remove Directory in Linux?

Introduction to Directory

In Linux/Unix OS, files are files, even directories are files, everything is a file, and devices such as a printer, keyboard, mouse, etc., are files as well. We will explain the file structure or directory structure of Linux.

File Types in Linux

How to Remove Directory in Linux?
  • General Files: Also, it is known as ordinary files. A general file may be a simple text, program, video, or image file. They can be in Binary or ASCII format. It's the most generally used file in Linux.
  • Directory Files: Directory files are a warehouse for many other file types. They may be a directory file in a directory or subdirectory.
  • Device Files: Devices like hard drives and CD-ROM are illustrated as drive letters, such as F : G : H in a Windows-like OS. However, system devices are illustrated as files in the Linux System. For instance, /dev/sda1, /dev/sda2, etc.

Files are stored in distinct folders on distinct data drives such as C : D : E: in Windows-like OS. However, files are organized in a tree-like structure, beginning with the root directory in the Linux operating system.

The base of the Linux file system hierarchy starts from the root, and everything begins with the root directory.

The following are the basic top-level directories related to the root directory:

  • /bin: executable or binary programs.
  • /home: It is the default current home directory.
  • /etc: system configuration files.
  • /opt: third-party or optional software.
  • /usr: user-associated programs.
  • /tmp: temporary space, cleared on reboot typically.
  • /var: log files.

A few other directories in Linux:

  • /boot: It includes every boot-related information folder and file like grub, conf, etc.
  • /dev: It is the device file location like /dev/sda1, /dev/sda2, etc.
  • /lost+found: It can find corrupted files recovered bits.
  • /lib: It includes a shared library and kernel modules.
  • /media: It includes subdirectories in which removable media devices are embedded.
  • /mnt: It includes temporary mount directories to mount the file system.
  • /run: It can store the volatile runtime data.
  • /proc: It is a pseudo and virtual file system that includes information on the running processes using a specific PID or process ID.
  • /sbin: These are binary executable programs.
  • /srv: It includes server-related and server-specific files.
  • /sys: It's a virtual filesystem to store and permits device modifications linked to the system for modern distributions of Linux.

Parent, Root, and Subdirectories

At the highest hierarchy level, the directory is known as the root directory in a file system. The root directory includes every other file and folder. All directories range to another directory except the root directory. The directory containing the current directory is known as the parent directory, and the directory placed inside the current directory is known as the subdirectory.

These subdirectories branch off the directory tree root. The directory tree is upside down, unlike an actual tree. So, the root is on the top, and the branches reach down. We are moving downward the tree when we move into any subdirectory. We are moving towards the tree when we move into the parent directory of a directory.

Directories in Linux

  • / Directory
    It is a root directory represented by a / single forward slash. In the Linux directory structure, it's the uppermost directory. It includes every directory, subdirectory, and file on our Linux system. It is from the root directory in which the hierarchy of the Linux directory begins.
    Important: The root directory shouldn't be confused with the home directory of the root (/root).
  • /boot Directory
    It is one of the most crucial directories in the Linux system. As its name implies, the boot directory includes the boot files of Linux, such as the kernel, bootloader, and related files. Files like vmlinuz illustrate the compressed Linux kernel image.
  • /etc Directory
    This directory includes system configuration files for every service, third-party application, and script that are installed. It is considered the Linux system nerve center.
  • /home Directory
    It is a directory that includes the personal files and folders of the user. By default, the home directory includes folders like Public, Videos, Pictures, Downloads, Documents, and Desktop on a graphical Linux system. Also, the home directory includes personal configuration files prefixed with a (.) dot symbol. For the login shell session, they are hidden files that include user-specific settings.
  • /root Directory
    For the root user, it is the home directory which is also known as the home directory of the root user. Also, the root account is known as the superuser, system administrator, administrative user, or only the root user has access to system files and commands in Linux.
  • /dev Directory
    For devices, this directory includes special files or device files that are connected to the system, like the mouse, keyboard, and hard drive. They are not ordinary that any user can write and read to. They are standard device abstractions that applications on our system interact with from output and input system calls.
  • /opt Directory
    It is a directory that's reserved for third-party software and add-on package applications that are not added to the official repositories of the system by default. For example, when we install applications like Java, Spotify, Discord, and Skype, they get saved in the /opt directory.
  • /var Directory
    This directory can store system-generated variable files, which add spool files, caches, and log files.
  • /bin Directory
    This directory includes basic system commands, executable programs, and binaries that are used by every user in the system. These contain history, wc, rpm, tar, df, du, cp, mv, cd, mkdir, cat, pwd, ls, etc.
  • /sbin Directory
    It includes system commands, utilities, and executable files that are reserved for a user with sudo privileges or the root user. Such commands contain swapon, ip, ifconfig, iptables, halt, fdisk, fsck, mkfs, reboot, etc.
  • /usr Directory
    This directory is one of the most crucial directories because of the enormous data amount it contains. The directory includes read-only system-wide files. These contain user binaries, libraries, and their documentation, system utilities, and programs.
  • /proc Directory
    This directory is kind of a strange directory. It's a pseudo or virtual filesystem that includes vital information about active processes. For the Linux kernel, the /proc directory is considered the information and control center. The filesystem is made on the fly under system startup and damaged once the device is powered off.

Analyzing directories and their benefits

Everyone knows that Linux is a very complicated system that needs an efficient way to begin, stop, reboot, and maintain any system, unlike Windows OS. In Linux, a few well-defined man pages, binaries, configuration files, and information files are present for all processes.

  • Device files
    • /dev/hdc: It is a pseudo device that results in garbage output which is redirected to the /dev/null directory.
    • /dev/hda: It is a device file for the initial IDE HDD.
  • Linux kernel file
    • /boot/vmlinux: It is the Linux kernel file.
  • System configuration files
    • /etc/crontab: It is a shell script to execute commands on a time interval.
    • /etc/bashrc: This file is utilized by the bash shell that includes system aliases and defaults.
    • /etc/exports: It includes information in the file system present on the network.
    • /etc/grub.conf: It's the bootloader configuration file of grub.
    • /etc/group: It's a text file to specify security group information.
    • /etc/fstab: It contains Disk Driver information and their mount point.
    • /etc/init.d: It is the service startup script.
    • /etc/hosts.deny: It contains a host list denied for accessing services in the local machine.
    • /etc/hosts.allow: It includes a host list permitted accessing services in the local machine.
    • /etc/hosts: It contains IP information and related hostnames.
    • /etc/lilo.conf: It includes the lilo boot loader configuration file.
    • /etc/inittab: It contains the INIT process and their communication at several run levels.
    • /etc/modules.conf: For the system modules, it includes the configuration files.
    • /etc/issue: It permits altering the pre-login message.
    • /etc/mtab: It includes mounted blocks information currently.
    • /etc/motd: It includes the message of the day.
    • /etc/password: It includes users inside a shadow file, system password, and username.
    • /etc/printcap: It includes printer information.
    • /etc/profile.d: It includes other scripts, such as application scripts, run after login.
    • /etc/profile: It represents the defaults of the bash shell.
    • /etc/rc.d/init.d: It represents the initialization script of the run level.
    • /etc/rc.d: It includes script duplication.
    • /etc/resolv.conf: DNS being utilized by the system.
    • /etc/skel: It represents the script that starts the home directory of a new user.
    • /etc/security: It includes the terminal names in which root login is also possible.
    • /etc/X11: For the X-window system, it's a directory tree that includes every conf file.
    • /etc/termcap: It is an ASCII file that specifies the behavior of different terminal types.
  • User-related files
    • /usr/sbin: It includes commands for system administration and super user.
    • /usr/lib: It includes object libraries and files.
    • /usr/share: It includes shareable architecture-independent text files.
    • /usr/include: It includes standard include files utilized by the C program.
    • /usr/bin/X11: It's the /usr/bin symbolic link.
    • /usr/bin: It includes almost all executable files.
  • Pseudo and virtual process related files
    • /proc/swaps: It includes swap file information.
    • /proc/stat: It shows the current system's detailed statistics.
    • /proc/mount: It contains the information of the mounted file system.
    • /proc/modules: using kernel module currently.
    • /proc/meminfo: It shows the memory usage information.
    • /proc/ioports: It includes every output and input address used on the server by devices.
    • /proc/interrupts: It contains the information of the interrupts per IRQ.
    • /proc/filesystems: It includes useful information on the processes that are currently running.
    • /proc/cpuinfo: It contains CPU information.
  • Version information file
    • /version: It shows the information of the Linux version.
  • Log files
    • /var/log/wtmp: It contains a history of logout and login information.
    • /var/log/messages: It includes every global system message.
    • /var/log/lastlog: It contains the last login information of the user.

Commands to remove a directory in Linux

We can use either the rm or rmdir command to permanently delete any directory in Linux:

  • Use the command, i.e., rm -r, to delete non-empty directories.
  • Use the command, i.e., rm -d or rmdir, to delete empty directories

We have to know the directory name before we delete it. To discover directories and files, we can use the command, i.e., ls, and we can use the command, i.e., pwd, to know the directory we are in.

Also, the options are important to see how they work that we use with these commands. The following is a short description of the options in the rm command:

  • rm -d: It removes an empty directory with the rm command.
  • rm -f: It avoids any prompt when removing a write-protected file.
  • rm -r: It removes a non-empty directory and related content.
  • rm -rf: It avoids any prompt when removing a non-empty write-protected folder.
  • rm -I: It results in a prompt just once before removing more than three files.
  • rm -i: It results in a prompt before removing all files.
  • rm ?: It is a wildcard that illustrates a single character.
  • rm *: It is a wildcard that illustrates multiple characters.
  • rmdir -v: It prints the details that the specified directory was removed.
  • rmdir -p: It deletes an empty directory and the related parent directory.

Syntax of the rm command

Important: To remove multiple directories of files with the rm command, we can add multiple directory and file names isolated by blank spaces.

  • Trying to use rm without options to delete a directory gives an error message.
    For example:
    How to Remove Directory in Linux?
  • If we wish to delete an empty directory, we can add the flag, i.e., -d, to rm.
    For example:
    How to Remove Directory in Linux?
  • We can also use the flag, i.e., -r, to remove a directory that includes files and subdirectories.
  • The following image displays the tree hierarchy of the directory, which includes two subdirectories along with multiple text files.
    For example:
    How to Remove Directory in Linux?
  • Using the flag, i.e., -r, deletes the whole directory, including files and subdirectories, whereas the flag, i.e., -v, will list all steps of the process as a result.
    For example:
    How to Remove Directory in Linux?
  • The option, i.e., -i, shows us a prompt confirming directory removal. We need to enter Y and click on the Enter button to continue.
    For example:
    How to Remove Directory in Linux?

Syntax of the rmdir command

  • Using rmdir on any non-empty directory generates an error.
    For example:
    How to Remove Directory in Linux?
  • The rmdir command permits to delete multiple directories with the same names with the help of the wildcards.
    For example:
    How to Remove Directory in Linux?






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA