Skip to Content

How to Delete All Files and Folders in Directory on Linux Using Commnad Line

This article will show you how to delete all files and folders in a directory on Linux, including the hidden ones. You will learn different methods to achieve this task, using commands like rmfind, and shopt. You will also see some examples and tips to avoid common errors.

What is the Problem?

Sometimes, you may want to delete all the contents of a directory, but not the directory itself. For example, you may have a directory called images that contains many files and subdirectories, and you want to clear it out without deleting the images directory.

However, if you use the rm command with the -r option, which means recursive, you will also delete the directory itself. For example, if you run:

rm -r images

You will delete the images directory and everything inside it. This is not what you want.

What is the Solution?

There are different ways to delete all files and folders in a directory on Linux, depending on whether you want to include the hidden ones or not. Hidden files and folders are those whose names start with a dot (.) character, such as .hidden-file or .hidden-directory.

Method 1: Using rm with a Wildcard

The simplest way to delete all files and folders in a directory, excluding the hidden ones, is to use the rm command with a wildcard (*) character. The wildcard matches any file or folder name, except those that start with a dot. For example, if you run:

rm -r images/*

You will delete all the non-hidden files and folders in the images directory, but not the images directory itself.

Using rm with a Wildcard to Delete All Files and Folders in Directory on Linux

However, this method has some drawbacks:

  • It will show an error for each subdirectory, because in this mode, rm is only allowed to delete files, not directories. To avoid this, you can use the -f option, which means force, to suppress the error messages. For example:
rm -rf images/*
  • It will not delete the hidden files and folders in the images directory. To delete them, you need to use another method.

Method 2: Using rm with Dotglob

If you want to delete all files and folders in a directory, including the hidden ones, you can use the rm command with the dotglob option. Dotglob is a shell option that makes the wildcard (*) match the dot (.) character as well. This way, you can delete all files and folders, regardless of their names. For example, if you run:

shopt -s dotglob # enable dotglob
rm -r images/* # delete everything in images
shopt -u dotglob # disable dotglob

You will delete all the files and folders in the images directory, but not the images directory itself.

Using rm with Dotglob to Delete All Files and Folders in Directory on Linux

However, this method has some drawbacks:

  • It will affect the behavior of the wildcard (*) for the rest of the session, unless you disable the dotglob option after using it. This may cause unexpected results if you use the wildcard for other purposes later. To avoid this, you can run the commands in a subshell, using parentheses. For example:
(shopt -s dotglob && rm -r images/*) # run in a subshell
  • It will still show an error for each subdirectory, because in this mode, rm is only allowed to delete files, not directories. To avoid this, you can use the -f option, which means force, to suppress the error messages. For example:
(shopt -s dotglob && rm -rf images/*) # run in a subshell

Method 3: Using find

Another way to delete all files and folders in a directory, including the hidden ones, is to use the find command. The find command can search for files and folders that match certain criteria, and then perform an action on them. For example, if you run:

find images -mindepth 1 -delete

You will delete all the files and folders in the images directory, but not the images directory itself. The -mindepth 1 option means to exclude the images directory from the search, and the -delete option means to delete the matching files and folders.

However, this method has some drawbacks:

  • Not all versions of find support the -delete option. If your find does not have this option, you can use the -exec option instead, which means to execute a command on each matching file or folder. For example:
find images -mindepth 1 -exec rm -rf {} +

The {} represents the name of the matching file or folder, and the + means to group as many names as possible into one command. The rm -rf command means to delete the file or folder recursively and forcefully.

  • The find command may be slower than the rm command, especially if there are many files and folders in the directory.

Method 4: Using rm and mkdir

The last way to delete all files and folders in a directory, including the hidden ones, is to use the rm command with the -r option to delete the directory itself, and then use the mkdir command to recreate it. For example, if you run:

rm -r images # delete images directory and everything inside it
mkdir images # create a new empty images directory

You will delete all the files and folders in the images directory, and then create a new empty images directory with the same name.

However, this method has some drawbacks:

  • It will change the permissions and ownership of the images directory, unless you specify them explicitly with the mkdir command. For example, if you want to preserve the original permissions and ownership, you can run:
rm -r images # delete images directory and everything inside it
mkdir -m $(stat -c %a images) -o $(stat -c %u images) -g $(stat -c %g images) images # create a new images directory with the same permissions, owner, and group

The stat command can display various information about a file or directory, such as the permissions (%a), the owner (%u), and the group (%g). The mkdir command can set these attributes with the -m-o, and -g options, respectively.

  • It will break any links or references to the images directory, because it is a new directory with a different inode number. An inode is a data structure that stores information about a file or directory, such as its size, type, and location. Each file or directory has a unique inode number, which is used to identify it. If you delete and recreate a directory, it will have a different inode number, and any links or references to it will become invalid.

Conclusion

This article has shown you how to delete all files and folders in a directory on Linux, including the hidden ones. You have learned different methods to achieve this task, using commands like rmfind, and shopt. You have also seen some examples and tips to avoid common errors.