Removing folders in linux

How to Delete Files and Folders in Linux

Linux command line fundamentals are absolutely essential for every future system administrator and advanced Linux user. Today we’ll cover another basic function – deleting files and directories in Linux using the command line.

The rmdir Command

The command used to delete empty directories in Linux is rmdir.

The basic syntax of this command is easy to grasp. here’s an example:

  • rmdir is the command
  • [option] is an optional modifier that changes the way the command behaves
  • DirectoryName is the directory you want removed

If no option is provided, rmdir simply deletes the directory whose name is provided as the destination. Before using this command, you’ll have to log into your VPS server using SSH. Here’s an article to help you out.

Remove Folders in Linux Using the rmdir Command

Before using the rmdir command we suggest you check the files present in a directory with the ls command. In our case, we have a directory named Dir1.

This command will delete the empty directory named Dir1. Simple enough, right?

You can also delete multiple directories by separating their names by spaces. For instance:

After executing this command, the directories named Dir1, Dir2 and Dir3 will be deleted.

Let say we have a directory named Dir3. Dir3 has subdirectories and files inside of it. Now if we use following command:

We will receive an error like this:

As you may have guessed from the output, rmdir only works with empty directories.

The rmdir is a smart utility, and only allows you to delete empty directories as a built-in safeguard to prevent accidental loss of data. Remember it’s almost impossible to recover deleted data on any Linux distribution.

The -p option lets you delete the directory as well as its parent directories.

This above command will delete Dir3 and its parent directories Dir2 and Dir1.

The -v option outputs a diagnostic text for every directory processed. Using this option will output a confirmation listing all the directories that were deleted.

The rm Command

The rmdir command is great for safely removing unused and empty directories. If you want to remove files or directories containing files, you would have to use the rm command.

The basic syntax of this command is similar to rmdir:

Remove Files in Linux Using the rm Command

Use the rm command to remove the file named article.txt:

If we have a directory with the name Dir1 containing subdirectories and files we will have to attach the -r modifier. The command would look like this:

The -r option recursively removes directories and their content.

Читайте также:  Кнопка свернуть все окна linux

Another helpful option is -i. This will ask you to confirm the files that will be deleted individually. That way you can avoid any unpleasant mistakes.

You can also delete empty directories using -d option. The following command will delete an empty directory with name Dir1:

You can use a wildcard (*) and a regular expansions to match multiple files. For instance, the following command will delete all pdf files placed in the current directory.

You can use variation of all the commands listed above to delete files with other extensions like .txt, .doc, .odt, etc.

The -f option lets you forcefully delete everything placed in a directory. The command would look like this:

The above command will delete everything recursively and forcefully, residing under the Dir1 directory without prompting anything on the terminal.

You can also delete more than one directory at a time. The following command will delete three directories Dir1, Dir2 and Dir3 in a single command.

Congratulations, you successfully mastered all the basic functions of the rm and rmdir commands!

Wrap Up

In Linux deleting a single file by accident can lead to major issues. That’s why it’s essential to master the two main commands for file and directory removal – rm and rmdir. In this article we cover these two commands and the various options that can be used with them.

We hope you found this article useful! Remember, once you delete a file or directory from Linux, you can’t recover it, so be extra careful! Good luck.

Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Источник

Linux Delete Folder Using rmdir and rm Command

Linux delete folder using rmdir

Let us see some examples that show how to remove a folder in Linux.

Linux remove folder

In this example, delete the directory called /tmp/letters/
rmdir /tmp/letters
For example, the following command would remove two empty folders named alpha and delta in the current directory:
rmdir alpha delta

How to get additional information about what is happening when running rmdir

Pass the -v (verbose) option as follows:
rmdir -v dir1
rmdir -v foo bar

Remove Folder and Its Ancestors

The -p option can delete directory and its subdirectories/sub-folders:
rmdir -p dir1/dir2/dir3
Where rmdir command command options are as follows:

  • -p : Linux remove folder i.e. remove the parent folders of the specified directory
  • -v : Output a diagnostic for every directory processed
  • —ignore-fail-on-non-empty : Ignore each failure that is solely because a folder is non-empty.

Delete All Files and Folders Including Subdirectories

Use the following syntax:
rm -rf /path/to/dir
For example, delete /home/vivek/docs and all its subdirectories including files, enter:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Where rm command options are as follows,

  • -r : Recursively delete a directory by first deleting all of its contents
  • -f : Linux delete folder forcefully
  • -v : Verbose output

GUI File Manager

The Nautilus file manager (GNOME desktop) provides a simple and integrated way to manage your files and applications. Just open it from Places menu and select folder and hit delete key.

Читайте также:  Не могу обновить manjaro linux

Fig.01: Gnome File Browser

Conclusion

In this tutorial, you learned how to delete folders in the Linux using rmdir and rm command-line options. The rmdir command is used to remove empty folders in Linux. The rm command used to delete both files and folders even if they are not empty. See rmdir help page here for more info.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Delete / Remove a Directory Linux Command

Commands to remove a directory in Linux

There are two command to delete a folder in Linux:

  1. rmdir command – Deletes the specified empty directories and folders in Linux.
  2. rm command – Delete the file including sub-directories. You can delete non-empty directories with rm command in Linux.

Let us see some examples and usage in details delete the directories.

rmdir command syntax to delete directory in Linux

The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:
rmdir directory-name
rmdir [option] directory-name
Open the terminal application and run command to delete given directory. For example, delete a folder named dir1:
rmdir dir1

Delete directory Linux Command

Open a command line terminal (select Applications > Accessories > Terminal), and then type the following command to remove a directory called /tmp/docs:
rmdir /tmp/docs
If a directory is not empty you will get an error message that read as follows:
rmdir letters
Sample outputs:

You can cd to the directory to find out and list all files:
$ cd letters
$ ls
Delete those files or directories. In this next example, remove data, foo and bar if bar were empty, foo only contained bar and data only contained foo directories:
cd /home/nixcraft
rmdir -p data/foo/bar

Where,

  • -p : Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component.

How to see a diagnostic message for every directory processed

Pass the -v option to the rmdir command:
$ rmdir -v dir1
Sample outputs:

Removing directories with rmdir and wildcards

We can use wildcards such as ‘*’ and ‘?’ to match and delete multiple directories. For example:
$ ls -l dir*
We have three dirs named dir1, dir2, and dir3. To delete all directories starting with ‘dir’ in the current, you would use the following command:
rmdir -v dir*

Linux remove entire directory including all files and sub-directories command

To remove all directories and subdirectories use the rm command. For example, remove *.doc files and all sub-directories and files inside letters directory, type the following command:

Warning : All files including subdirectories will be deleted permanently when executed the following commands.

$ rm -rf letters/
Sample session:

Where,

  • -r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively remove subdirectories and files from the specified directory.
  • -f : Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions

Are you getting permission denied error message while removing directories?

Only owners can delete their directories. However, a sysadmin can delete any directories created by anyone on the system. The syntax is:
sudo rmdir /path/to/dir/
sudo rm -rf dir2
When prompted, you need to provide root user or sudo user password.

Use find command to delete unwanted directories

Say you want to find out all directories named ‘session’ and delete them in the current directory, run:
find . -type d -iname ‘session’ -delete

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard
Читайте также:  Потерян файл windows system32 config system

Join Patreon

How to find and remove all empty directories

Run:
find . -type d -iname ‘session’ -empty -delete
Where,

  • -type d : Only search for directories and ignore all other files.
  • -iname ‘session’ : Search directory named ‘session’. You can use wildcards here too. For example, -iname ‘dir*’ .
  • -empty : Only match empty directories
  • -delete : Deletes all found empty directories only

To delete all ‘.DS_store’ directories stored in /var/www/html, run:
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> \;
OR
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> +
The -exec option to the find command run an external command named rm to delete all files. The “ rm <> +/ ” is a better option as it uses one rm command to delete all .DS_Store directories.

Conclusion

This page showed how to delete a directory when it is empty. Further, it showed, how to remove folders using the rm and rmdir commands. See rm help page page for more info:

  • For more information read man pages: rm(1)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How do I force delete a directory in Linux?

How to force delete a directory in Linux

Here is how to forcefully delete a folder in Linux:

  1. Open the terminal application on Linux.
  2. The rmdir command removes empty directories only. Hence you need to use the rm command to remove files on Linux.
  3. Type the command rm -rf dirname to delete a directory forcefully.
  4. Verify it with the help of ls command on Linux.

Removing a directory that contains other files or directories

The syntax is as following for deleting a directory:
rm -rf dirName
Say you have a directory named /tmp/data/ that contains two files and one directory as follows:
ls -l /tmp/data/
If you run the rmdir command, you will get an error as follows:
rmdir /tmp/data/
As explained earlier rmdir only delete the DIRECTORIES, if they are empty. Therefore you must use the rm command to remove a full directory in Linux:
rm -rf /tmp/data/
Verify it:
ls -l /tmp/data/

Use rm command to delete a non-empty directory in Linux terminal

How do I remove a full directory in Linux with verbose output?

Pass the -v option to the rm command as follows:
rm -rfv dirname
For example, delete a full directory named /tmp/bar in Linux and note down the output on the screen:
rm -rfv /tmp/bar/

Where,

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

  • -r : Recursive delete
  • -f : Force delete a directory
  • -v : Verbose output

Delete a non-empty directory in Linux terminal

In case if you don’t have the permission to delete the folder run rm command as a root user. Otherwise, you will see permission denied message on the screen. Therefore to avoid that prefix sudo at the beginning of the rm command :
sudo rm -rf dirName
OR
sudo rm -rf /somedir/
To remove a folder whose name starts with a ‘ — ‘, for example ‘-backups’, use one of these commands:
rm -rfv — -backups/
OR
rm -rfv ./-bacups/

Источник

Оцените статью