Remove all empty directories linux

Linux / Unix: Find and Delete All Empty Directories & Files

H ow do I find out all empty files and directories on a Linux / Apple OS X / BSD / Unix-like operating systems and delete them in a single pass?

You need to use the combination of find and rm command. [donotprint]

Tutorial details
Difficulty level Easy
Root privileges No
Requirements find command
Est. reading time 5m

[/donotprint]GNU/find has an option to delete files with -delete option. Please note that Unix / Linux filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or newlines are incorrectly processed by many utilities including rm command. To avoid problems you need to pass the -print0 option to find command and pass the -0 option to xargs command, which prevents such problems.

Method # 1: Find and delete everything with find command only

The syntax is as follows to find and delete all empty directories using BSD or GNU find command:

Find and delete all empty files:

Delete empty directories

In this example, delete empty directories from

  • 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

Delete empty files

In this example, delete empty files from

Fig.01: Delete empty directories and files.

How to count all empty files or directories?

The syntax is as follows:

  • -empty : Only find empty files and make sure it is a regular file or a directory.
  • -type d : Only match directories.
  • -type f : Only match files.
  • -delete : Delete files. Always put -delete option at the end of find command as find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.

This is useful when you need to clean up empty directories and files in a single command.

Method # 2: Find and delete everything using xargs and rm/rmdir command

The syntax is as follows to find and delete all empty directories using xargs command:

Источник

Linux how do I remove all empty directories?

You can use program called cleanlinks. The cleanlinks program searches the directory tree descended from the current directory for symbolic links whose targets do not exist, and removes them. It then removes all empty directories in that directory tree. It was originally created for symbolic links based directories but works with normal directories too.

For example if you want to remove all empty directories from /tmp directory, type the command:
$ cd /tmp
$ cleanlinks

Please note that cleanlinks command is part of XFree86 project. Another method is to use combination of shell commands in script:

Save and execute a script:
$ script.sh dir1

You can also try out tmpreaper command which recursively searches for and removes files and empty directories which haven’t been accessed for a given number of seconds. Normally, it’s used to clean up directories which are used for temporary holding space, such as “/tmp”. Syntax is as follows:
tmpreaper TIME-FORMAT DIRS

  • TIME-FORMAT : Defines the age threshold for removing files. The TIME-FORMAT should be a number, defaulting to hours, optionally suffixed by one character: d for days, h for hours, m for minutes, or s for seconds.
  • DIRS : Directory name for example /tmp

For example, remove all files accessed 24h before:

# tmpreaper 24h /tmp

  • 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

Please note that tmpreaper command is not installed by default you may need to install it using apt-get or rpm command.

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

Источник

Читайте также:  Windows phone downloading files
Оцените статью